首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将元组列表转换为字典有2种不同的方法

将元组列表转换为字典有2种不同的方法
EN

Stack Overflow用户
提问于 2019-02-27 15:18:32
回答 7查看 236关注 0票数 3

我有下面的元组列表。

代码语言:javascript
运行
复制
[('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号设置为键,将兴趣设置为值,然后将兴趣号设置为键,将员工id号设置为值。基本上,作为一个快速示例,我需要最终结果的一个元素看起来像这样:

代码语言:javascript
运行
复制
{'0': ['Hadoop', 'Big Data', 'HBas', 'Java', 'Spark', 'Storm', 'Cassandra'],
 '1' ... etc]}

然后,下一个将如下所示:

代码语言:javascript
运行
复制
{'Hadoop': [0,9]...}

我尝试了default dict,但似乎不能让它工作。有什么建议吗?

EN

Stack Overflow用户

发布于 2019-02-27 15:23:02

您可以使用collections.defaultdict

Ex:

代码语言:javascript
运行
复制
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)

输出:

代码语言:javascript
运行
复制
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']})
票数 5
EN
查看全部 7 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54900025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档