Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9105865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:12:14+00:00 2026-06-17T02:12:14+00:00

Here are the basic steps to an app I’m writing in Python: Generate a

  • 0

Here are the basic steps to an app I’m writing in Python:

  1. Generate a list of random colors
  2. Create a mapping of each of those colors, indexed by that color’s distance to a “source color” in 3D (r, g, b) space. (For instance, orange (255, 150, 0) is closer to red(255, 0, 0) than dark blue (0, 0, 100). By now, I have a list of tuples of the format (distance, color).
  3. Sort that list of tuples based on the distances I had calculated (from lowest, to highest).
  4. Retrieve a list of the sorted colors

Here is my function, and I get the following error: TypeError: ‘int’ object has no attribute ‘_getitem_’ on the line sorted_by_dist = sorted(colorMap, key=lambda tup: tup[0])

# Sorts a list of colors according to distance from the source color
def sort_colors(colors, source):
    colorMap = ()
    sortedColors = list()
    for i in range(len(colors)):
        dist = dist_3d(colors[i], source)
        colorMap = colorMap + (dist, colors[i])

    sorted_by_dist = sorted(colorMap, key=lambda tup: tup[0])

    for (d, c) in sorted_by_dist:
        sortedColors.append(c)

    return sortedColors

Assuming my dist_3d() function is correct and returns an integer value (it is, and does), what am I doing wrong? I don’t understand.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T02:12:15+00:00Added an answer on June 17, 2026 at 2:12 am

    You are building your colorMap as a big single dimensional tuple, with the first index being an int. So your lambda is being passed an int and then you try to index into it.

    You probably want a list of tuples:

    colorMap = []
    ...
        dist = dist_3d(colors[i], source)
        colorMap.append((dist, colors[i]))
    

    In terms of an approach to sorting colors, I have actually used a kdtree module for this, loaded up with all my RGB tuples. Then I can ask it for N closest colors to a given color tuple:

    from kdtree import KDTree
    
    colors = [(10,10,10),(250,50,5),(100,50,20)]
    query_color = (175, 25, 50)
    
    tree = KDTree.construct_from_data(data)
    # query the 2 closest colors
    nearest = tree.query(query_point=query_color, t=2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to create an iOS 5 app with some very basic FTP functionality
So I went through the steps to build a basic roo application here: http://static.springsource.org/spring-roo/reference/html/beginning.html
I am missing something embarrassingly basic here in my Merge Sort implementation: # include
I'm sure I'm doing something really dumb and basic here but I can't seem
Here's the basic UI i intend: There is central circle:CENTER. As the view is
Here is the basic I am using FB.ui to post a flash application to
Here's a basic view of what my html looks like: <form> <div id=part1> //
Here's the basic situation: I have an OpenGL VBO that draws a 2D symbol
Here's the basic information: I've been studying Rob Miles' C# Yellow Book 2011 extensively.
Here are my basic assumptions: Wcf executes my service operation methods on IOCP threads

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.