Python - 来自未排序文本的字典,列表理解?

标签 python python-3.x dictionary list-comprehension

我希望更精通列表理解的人可以提供一些建议。

考虑以下数据集:

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

我需要制定一个字典,其中键是矿物质,值是一组含有该矿物质的食物,如下所示:

{'copper': {'Tomatoes', 'Onions', 'Bell Peppers', 'Eggplant'}, 'maganeese': {'Onions', 'Tomatoes', 'Garlic', 'Celery', 'Bell Peppers', 'Eggplant', 'Grapes'}...  etc.}

您会注意到食物位于第一位,其次是它所含的矿物质。

我认为我可能需要将食物和矿物质分成两个列表,即食物列表和矿物质列表。从逻辑上讲,我根本不知道如何完成这项任务。

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)

有人可以在这里插入正确的方向吗?

最佳答案

你可以这样做:

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)

输出

{'copper': ['Onions', 'Tomatoes', 'Bell Peppers'],
 'magnesium': ['Onions'],
 'manganese': ['Onions', 'Tomatoes', 'Garlic', 'Celery', 'Bell Peppers'],
 'phosphorus': ['Onions'],
 'potassium': ['Tomatoes', 'Celery'],
 'salt': ['Celery'],
 'sodium': ['Celery']}

行:

# split the line into vegetable and minerals
vegetable, *minerals = line.strip().split(',')

使用extended iterable unpacking 。 for循环使用setdefault ,来自文档:

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.

关于Python - 来自未排序文本的字典,列表理解?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64790703/

相关文章:

python - Pandas 选择带有空行的日期范围不起作用

python-3.x - IMAP fetch() 返回命令错误 : BAD [b' Command Argument Error. 12']

for-loop - Julia:具有大数据移动的并行 For 循环

c++ - std::map 超出需要的比较

python - 如何从 InlineFormset 中的图像字段获取图像 URL?

python - 如何在 Graphite 中汇总一秒内的数据?

python - python Sprite 列表是如何工作的?我可以将 Sprite 坐标添加到列表中吗?

python-3.x - 如何找到矩阵中点之间的所有距离而不重复?

java - 从 Clojure 中的集合( map 列表)中获取偶数/奇数索引元素

Python3 为 PyPi 准备包,不包括子模块