gpt4 book ai didi

python - 将元组列表转换为字典 2 种不同的方式

转载 作者:太空宇宙 更新时间:2023-11-03 12:43:33 26 4
gpt4 key购买 nike

我有以下元组列表。

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

左边的值是“员工工号”,右边的值是“兴趣”。我必须以两种不同的方式将它们变成字典:我必须将员工 ID 号作为键,将兴趣作为值,然后我必须将兴趣作为键,将员工 ID 号作为值。基本上,作为一个简单的例子,我需要我的最终结果的元素之一看起来像这样:

{'0': ['Hadoop', 'Big Data', 'HBas', 'Java', 'Spark', 'Storm', 'Cassandra'],
'1' ... etc]}

然后下一个看起来像这样:

{'Hadoop': [0,9]...}

我尝试了默认字典,但似乎无法正常工作。有什么建议吗?

最佳答案

你可以使用collections.defaultdict

例如:

from collections import defaultdict

lst = [('0', 'Hadoop'),
('0', 'Big Data'),
('0', 'HBas'),
('0', 'Java'),.....]

result = defaultdict(list)
for idVal, interest in lst:
result[idVal].append(interest)
print(result)

result = defaultdict(list)
for idVal, interest in lst:
result[interest].append(idVal)
print(result)

输出:

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

关于python - 将元组列表转换为字典 2 种不同的方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54900025/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com