1

I am trying to map values between dictionary and list in Python. I am trying to count the number of objects I found in the image: For example I found: Squares:3 Rectangles:4 Oval=2 Triangle=1

Now I append all of them to a list in descending order.

The list becomes:[4,3,2,1]

Now I somehow wanna say that '4' in the list corresponds to 'rectangle', '2' corresponds to 'Oval' I am trying to use a dictionary but struggling.

Since , I am doing this for multiple images, the output will be different. For example, the next image gives the results:

Squares:4 Rectangles:3 Oval=1 Triangle=2

Now the List becomes [4,3,1,2]

Therefore, it should map '4' to Squares and not rectangle

6
  • 1
    Can you clarify exactly what you're asking? You say you are mapping the dict's values to a list, so you are losing the keys at that point. But then you say you are wanting to use the keys again. Why do you need the list at all? Commented Apr 22, 2016 at 23:15
  • You should invert your key-value pairs, and really question your data structures. If you have a dictionary of mydict = {4: 'Rectangles'}, then mydict[4] will give you 'Rectangles'. But why use both when you just need a dictionary? You seem to have unnecessary objects. Commented Apr 22, 2016 at 23:16
  • You might want to use a Counter and its most_common method. docs.python.org/2/library/… Commented Apr 22, 2016 at 23:16
  • I don''t wanna use list, but then I want to sort the dictionary in descending order based not the integer value ('number of objects') Commented Apr 22, 2016 at 23:20
  • "I want to sort the dictionary in descending order based not the integer value ('number of objects')". What does this mean? Commented Apr 22, 2016 at 23:24

1 Answer 1

2

I'd use a dictionary:

# Squares:3 Rectangles:4 Oval=2 Triangle=1

shapes = {}
shapes["Square"]    = 3
shapes["Rectangle"] = 4
shapes["Oval"]      = 2
shapes["Triangle"]  = 1

print(shapes)               # {'Square': 3, 'Oval': 2, 'Triangle': 1, 'Rectangle': 4}

# Sort list of key,value pairs in descending order
pairs = sorted(shapes.items(), key=lambda pair: pair[1], reverse=True)
print(pairs)                # [('Rectangle', 4), ('Square', 3), ('Oval', 2), ('Triangle', 1)]

# Get your list, in descending order
vals = [v for k,v in pairs]
print(vals)                 # [4, 3, 2, 1]

# Get the keys of that list, in the same order
keys = [k for k,v in pairs] # ['Rectangle', 'Square', 'Oval', 'Triangle']
print(keys)

Output:

{'Square': 3, 'Oval': 2, 'Triangle': 1, 'Rectangle': 4}          # shapes
[('Rectangle', 4), ('Square', 3), ('Oval', 2), ('Triangle', 1)]  # pairs
[4, 3, 2, 1]                                                     # vals
['Rectangle', 'Square', 'Oval', 'Triangle']                      # keys

For observant readers, the dictionary isn't necessary at all -- however I imagine there is more to the goal that we don't know about, where a dictionary would make the most sense.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.