I'm hoping somebody a little more versed in list comprehension can offer some suggestions.
Consider the following data set:
Onions,copper,manganese,magnesium,phosphorus
Tomatoes,copper,manganese,potassium
Garlic,manganese
Celery,manganese,potassium,sodium,salt
Bell Peppers,copper,manganese
Butter,sodium,salt
Eggplant,copper,manganese
Grapes,copper,manganese,potassium
I need to formulate a dictionary, such that the key is the mineral, and the value is a set of foods which contain that mineral- like this:
{'copper': {'Tomatoes', 'Onions', 'Bell Peppers', 'Eggplant'}, 'maganeese': {'Onions', 'Tomatoes', 'Garlic', 'Celery', 'Bell Peppers', 'Eggplant', 'Grapes'}... etc.}
You'll notice that the food is in the first positioin, followed by the minerals it contains.
I figure that I'll probably need to separate the foods and minerals into two lists, list of foods and list of minerals. Logically, I am stuck with how one could accomplish this task at all.
with open ('file.txt', 'r') as fp:
D = dict()
food_list = []
mineral_list = []
for line in fp:
line = line.strip().split(",")
line = [x for x in line if x]
food_list.append(line[0])
print(food_list)
Can anybody provide a push in the right direction here?
You could do something like this:
import pprint
mineral_table = {}
with open("ip.txt") as infile:
for line in infile:
# split the line into vegetable and minerals
vegetable, *minerals = line.strip().split(',')
# for each mineral add the vegetable to the mineral list
for mineral in minerals:
mineral_table.setdefault(mineral, []).append(vegetable)
pprint.pprint(mineral_table)
Output
{'copper': ['Onions', 'Tomatoes', 'Bell Peppers'],
'magnesium': ['Onions'],
'manganese': ['Onions', 'Tomatoes', 'Garlic', 'Celery', 'Bell Peppers'],
'phosphorus': ['Onions'],
'potassium': ['Tomatoes', 'Celery'],
'salt': ['Celery'],
'sodium': ['Celery']}
The line:
# split the line into vegetable and minerals
vegetable, *minerals = line.strip().split(',')
uses extended iterable unpacking . The for loop usessetdefault , from the documentation:
If key is in the dictionary, return its value. If not, insert key with a value of default and return default. default defaults to None.
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.