【发布时间】:2018-05-08 15:05:09
【问题描述】:
我有一个包含以下内容的文本文件:
20 23
0 16
1 2
1 6
1 7
1 8
2 11
2 16
2 17
3 14
3 16
3 17
4 7
4 13
4 17
我需要它在这样的 python dict 中:
{0:[16],1:[2,6,7,8],2:[11,16,17],3:[14,16,17],4:[7,13,17],20:[23]}
谢谢
【问题讨论】:
-
您可以使用
csv模块来读取您的数据,并使用collections.defaultsict()来创建您期望的字典。但最好添加您的代码并告诉我们您的代码有什么问题? -
@Kasramvd 你的意思是
defaultdict()? -
@BlackVegetable NO :-)
defaultsict是 python Easter egg。它可以为您提供您想要的任何格式的字典。但永远不要使用它!因为它可能会导致您的计算机被炸毁,哈哈。 -
@Kasramvd 啊,当然。我忘了那个! ;)
标签: python python-3.x dictionary text