简体   繁体   中英

Append from dict to list?

I have this dictionary containing data:

{1000: 'brown', 4000: 'yellow', 2000: 'yellow', 3000: 'blue'}

My dictionary, however, has a lot more data, but this is the structure. The key is unique but the value is not as it may apply to several keys and i have used a loop earlier on to make sure no duplicate keys are in there.

What i want to to is to sort the dictionary into lists/dicts/tupels whatever works. So one list for all brown, one for all yellow, and one for all blue (there are only three colors, so i need three lists). Kinda like this:

brown_list = [1000, 5000, 7000]
yellow_list = [4000, 2000, 8000]
blue_list = [3000, 9000, 10000]

I attempted to do it this way (color_name is my original dictionary):

for color in color_name:
   if color_name == 'brown'
      brown_list.append color_name

Unfortunately this doesnt work. I have tried to read up on dictionaries and appending from them but im not really getting it. Any tips on how to go about splitting the dictionary into different lists depending on the value of the dict?

You should really reflect your structure with an easier access method: another dict, keyed by color:

>>> value_dict = {1000: 'brown', 4000: 'yellow', 2000: 'yellow', 3000: 'blue'}
>>> set(value_dict.values())
{'brown', 'blue', 'yellow'}
>>> color_dict = {}
>>> for color in set(value_dict.values()):
    color_dict[color] = [v for v, c in value_dict.items() if c == color]
>>> color_dict
{'brown': [1000], 'blue': [3000], 'yellow': [4000, 2000]}

If you want to get uber-Pythonic, you can collapse the transformation to a single line, a list comprehension inside a dict comprehension:

color_dict{color: [v for v, c in value_dict.items() if c == color]
              for color in set(value_dict.values())
          }
  • You need to loop through both keys and values of your dictionary
  • append is a method; method calls require parentheses
  • Put a colon after the if statement
for number, color in your_dictionary.items():
   if color == 'brown':
      brown_list.append(number)
   elif color == 'yellow':
      ...

You could create an output dict whose keys are the colors and whose associated values are the list of data values associated with each color.

For example:

dict1 = {1000: 'brown', 4000: 'yellow', 2000: 'yellow', 3000: 'blue'}
dict2 = {}

for k,v in dict1.items():
    dict2[v] = [*dict2.get(v, []), k]

print(dict2)

Result is: {'brown': [1000], 'yellow': [4000, 2000], 'blue': [3000]}

This scales better for additional colors. Instead of blue_list , you would use dict2['blue'] .

Store the numbers in a dictionary, and use collections.defaultdict to create it:

from collections import defaultdict

color_name = {1000: 'brown', 4000: 'yellow', 2000: 'yellow', 3000: 'blue', 5000: 'brown', }
color_to_nums = defaultdict(list)

for num, color in color_name.items():
    color_to_nums[color] += [num]

# Optional: convert back into regular dictionary
color_to_nums = dict(color_to_nums)

print(color_to_nums)
# {'brown': [1000, 5000], 'yellow': [4000, 2000], 'blue': [3000]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM