【问题标题】:Creating two dictionaries and assigning keys and values from text file创建两个字典并从文本文件中分配键和值
【发布时间】:2019-07-20 08:28:07
【问题描述】:

我必须创建两个字典并分配键和值。当键是员工 ID 时,值是兴趣。然后,当键是兴趣时,值将是员工 ID。 然后我必须打印这些字典。 我必须先打开/读取文本文件。

到目前为止,我已经得到了:

file = open("interests.txt", "r")

people = {}

for row in file:
    employee_id = int(row[0])
    people[employee_id] = {
        'interests': row[2:]

        }

from pprint import pprint
pprint (people)

结果我只有这样:

{0: {'interests': 'Cassandra\n'},
 1: {'interests': 'Postgres\n'},
 2: {'interests': 'pandas\n'},
 3: {'interests': 'probability\n'},
 4: {'interests': 'libsvm\n'},
 5: {'interests': 'programming languages\n'},
 6: {'interests': 'theory\n'},
 7: {'interests': 'neural networks\n'},
 8: {'interests': 'artificial intelligence\n'},
 9: {'interests': 'Big Data'}}

但我必须得到与employee_id匹配的所有兴趣。

请帮助我。

【问题讨论】:

    标签: python python-3.x dictionary


    【解决方案1】:

    你已经成功了一半。当您解析新行时,现在您正在用全新的兴趣替换键处字典中的兴趣值。取而代之的是,将该键处的值设为一个列表,将新的兴趣值附加到该列表中:

    for row in file:
        employee_id = int(row[0])
        interest = row[2:]
    
        if employee_id not in people:
            people[employee_id] = []
    
        people[employee_id].append(interest)
    

    这样,您将获得一个字典,其中每个 ID 都映射到相应的兴趣。要拥有一个字典,其中每个兴趣都映射到相应的 ID,您可以简单地反向执行相同的操作。 (我将把它留给你作为学习练习。:))

    【讨论】:

    • 我没有得到兴趣是关键的那个。我也得到 \n 每一端
    • '\n' 将成为文件每一行中的最后一个字符(可能除了最后一行)。如果您想将其过滤掉,则需要一些额外的工作。
    【解决方案2】:

    您正在使用 dicts 的 dict 覆盖同一键的先前值。您可以改为使用 dict.setdefault 用列表初始化 dict 的新键的每个条目,以便您可以继续向其附加项目:

    people = {}
    interests = {}
    for line in file:
        employee_id, interest = line.split(maxsplit=1)
        employee_id = int(employee_id)
        interest = interest.rstrip()
        people.setdefault(employee_id, []).append(interest)
        interests.setdefault(interest, []).append(employee_id)
    

    people 变为:

    {0: ['Hadoop', 'Big Data', 'HBas', 'Java', 'Spark', 'Storm', 'Cassandra'], 1: ['NoSQL', 'MongoDB', 'Cassandra', 'HBase', 'Postgres'], 2: ['Python', 'skikit-learn', 'scipy', 'numpy', 'statsmodels', 'pandas'], 3: ['R', 'Python', 'statistics', 'regression', 'probability'], 4: ['machine learning', 'regression', 'decision trees', 'libsvm'], 5: ['Python', 'R', 'Java', 'C++', 'Haskell', 'programming languages'], 6: ['statistics', 'probability', 'mathematics', 'theory'], 7: ['machine learning', 'scikit-learn', 'Mahout', 'neural networks'], 8: ['neural networks', 'deep learning', 'Big Data', 'artificial intelligence'], 9: ['Hadoop', 'Java', 'MapReduce', 'Big Data']}
    

    interests 变为:

    {'Hadoop': [0, 9], 'Big Data': [0, 8, 9], 'HBas': [0], 'Java': [0, 5, 9], 'Spark': [0], 'Storm': [0], 'Cassandra': [0, 1], 'NoSQL': [1], 'MongoDB': [1], 'HBase': [1], 'Postgres': [1], 'Python': [2, 3, 5], 'skikit-learn': [2], 'scipy': [2], 'numpy': [2], 'statsmodels': [2], 'pandas': [2], 'R': [3, 5], 'statistics': [3, 6], 'regression': [3, 4], 'probability': [3, 6], 'machine learning': [4, 7], 'decision trees': [4], 'libsvm': [4], 'C++': [5], 'Haskell': [5], 'programming languages': [5], 'mathematics': [6], 'theory': [6], 'scikit-learn': [7], 'Mahout': [7], 'neural networks': [7, 8], 'deep learning': [8], 'artificial intelligence': [8], 'MapReduce': [9]}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-26
      • 1970-01-01
      • 1970-01-01
      • 2012-04-10
      • 1970-01-01
      相关资源
      最近更新 更多