简体   繁体   中英

Extracting values from dictionary

I would need to extract the second elements from this dictionary:

{'books': ['100', '76', '400-500', '360', '400-500', '400-500', '400-500', '400-500', '400-500', '76', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '500', '400-500', '400-500', '100-300', '100-300', '100-300', '100-300', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '160', '160', '160', '160', '160', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '160', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '500', '1.300', '400-500', '76', '400-500', '76'], 'nm': ['600', '500', '400-500', '80-160', '1', '80-160', '1', '80-160', '1', '400-500', '400-500', '400-500', '80-160', '1', '400-500', '400-500', '400-500', '130', '130', '400-500', '400-500', '400-500', '400-500', '400-500', '02', '400-500', '400-500', '400-500', '235', '400-500', '235', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '80-160', '1', '80-160', '1', '75', '1.1', '1.1', '27-30', '32', '2004', '20-500', '900', '60', '400-500', '400-500', '400-500', '400-500', '400-500', '400', '11', '27', '400', '1', '100', '500', '400-500', '140', '140', '140', '140', '100', '0.10', '10', '140', '140', '400', '400', '480', '400', '595', '0466', '400', '400', '87', '33'], 'book': ['1', '1', '1', '1', '1', '1'], 'papers': ['400-500', '400-500', '400-500', '400-500', '400-500'], 'paper': ['400-500']}

from print({ k : v for k, v in my_dict.items() }) .

I would need a list of unique items included in the above, in order to run some code on that. I am not familiar with extracting data from dictionary but only from lists so any help would be appreciated it. I have tried as follows: my_dict.get() but it does not give me my expected output, which would be:

['100', '76', '400-500', '360','100-300','160','80-160', '1', ...]

Alex's answer is correct. Could also try this one

result = set()
for k, v in my_list.items():
    result.update(set(v))
print(result)

Two steps here:

  1. Iterate over the keys in a dictionary
  2. Get all of the unique items from the values bound to the keys
your_question = {'books': ['100', '76', '400-500', '360', '400-500', '400-500', '400-500', '400-500', '400-500', '76', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '500', '400-500', '400-500', '100-300', '100-300', '100-300', '100-300', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '160', '160', '160', '160', '160', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '160', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '500', '1.300', '400-500', '76', '400-500', '76'], 'nm': ['600', '500', '400-500', '80-160', '1', '80-160', '1', '80-160', '1', '400-500', '400-500', '400-500', '80-160', '1', '400-500', '400-500', '400-500', '130', '130', '400-500', '400-500', '400-500', '400-500', '400-500', '02', '400-500', '400-500', '400-500', '235', '400-500', '235', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '400-500', '80-160', '1', '80-160', '1', '75', '1.1', '1.1', '27-30', '32', '2004', '20-500', '900', '60', '400-500', '400-500', '400-500', '400-500', '400-500', '400', '11', '27', '400', '1', '100', '500', '400-500', '140', '140', '140', '140', '100', '0.10', '10', '140', '140', '400', '400', '480', '400', '595', '0466', '400', '400', '87', '33'], 'book': ['1', '1', '1', '1', '1', '1'], 'papers': ['400-500', '400-500', '400-500', '400-500', '400-500'], 'paper': ['400-500']}

output = set()
for key in your_question:
  output = output.union(
    your_question[key]
  )

print(output)

# Or you can cast the set output back to a list:
# list(output)

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