在Python中将行分组为字典

2024-09-25 18:27:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个如下的输入文件:

5181        zzzzzzzzzzzzzzzz
6220        aaaaaaaa
1498        bbbbbbbbb 
1498        ccccccccccc

我想对具有相同id号的行进行分组。所以会是这样的:

5181        zzzzzzzzzzzzzzzz 
6220        aaaaaaaa
1498        bbbbbbbbbb
1498        ccccccccccc

我试图创建一个有3个键的字典,但无法附加所有值

这是我写的:

for i in package_ids:
    dict2[i] = (x for x in textt if int(i) in textt is True)

Tags: 文件inididspackagefor字典ccccccccccc
2条回答

您可以使用collections.defaultdict

from collections import defaultdict
d = defaultdict(list)
file_data = [[int(a), b] for a, b in [i.strip('\n').split('\t') for i in open('filename.txt')]]
for a, b in file_data:
   d[a].append(b)

for a, b in sorted(d.items(), key=lambda x:x[0]):
    print(a, b)

没有defaultdict

d = {}
for a, b in file_data:
   if a not in d:
       d[a] = [b]
   else:
       d[a].append(b)

假设id数字和行的文本用tab(s)\t分隔,简单排序:

with open('yourfile.txt', 'r') as f:
    lines = f.read().splitlines()
    result = sorted(lines, key=lambda x: x[:x.find('\t')])
    for l in result:
        print(l)

输出:

1498        Pursuing it with eager feet, 
1498        And I must follow, if I can, 
1498        Until it joins some larger way
1498        The Road goes ever on and on
5181        not a penny-piece or a glass bead was given away.
5181        as well as the books, pictures were left in his possession.  
6220        written in golden ink. 
6220        There were many Bagginses and Boffins,
6220        of them had hardly ever been in Hobbiton before.

相关问题 更多 >