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?