繁体   English   中英

从 .txt 文件创建一个字典,每行作为值,序列号作为键

[英]create a dictionary from .txt file with each line as values and serial num as key

我有一个数据集,它是一个 .txt 文件,每一行都有用空格分隔的项目。 每一行都是不同的交易。

数据集如下所示:

数据.txt文件

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
20 12 5 41 65
41 6 11 27 81 21
65 15 27 8 31 65 20 19 44 29 41

我用键作为序列号创建了一个字典。 从 0 开始,每行值用逗号分隔,就像这样

{0: '1,2,3,4,5,6,7,8,9,10,11,12,13,14,15', 1:'20,12,5,41,65', 2:'41,6,11,27,81,21', 3: '65,15,27,8,31,65,20,19,44,29,41'} 

但我无法遍历 dict 中的每个值,有什么方法可以将它转换为每个键的值列表

我想在整个字典中找到每个时间的频率并创建一个表

物品 频率
1个 1个
2个 1个
20 2个
41 3个

像上面的

my_dict = {}

with open('text.csv', 'r') as file:
    lines = file.readlines()
    for line in lines:
        my_dict[lines.index(line)] = line.strip()

这是我用来创建字典的代码,但我不确定我应该更改什么,我还需要找到每个值的频率。

任何帮助,将不胜感激。 感谢你。

由于您实际上只是在计算整个文件的数字,因此您可以:

my_dict = {}

with open('data.txt', 'r') as file:
    for number in file.read().split():
        my_dict[number] = my_dict.get(number, 0) + 1

print(my_dict)

结果:

{'1': 1, '2': 1, '3': 1, '4': 1, '5': 2, '6': 2, '7': 1, '8': 2, '9': 1, '10': 1, '11': 2, '12': 2, '13': 1, '14': 1, '15': 2, '20': 2, '41': 3, '65': 3, '27': 2, '81': 1, '21': 1, '31': 1, '19': 1, '44': 1, '29': 1}

这只是计算代表数字的字符串,您可以将它们转换为实际数字:

with open('data.txt', 'r') as file:
    for number in file.read().split():
        my_dict[int(number)] = my_dict.get(int(number), 0) + 1

结果:

{1: 1, 2: 1, 3: 1, 4: 1, 5: 2, 6: 2, 7: 1, 8: 2, 9: 1, 10: 1, 11: 2, 12: 2, 13: 1, 14: 1, 15: 2, 20: 2, 41: 3, 65: 3, 27: 2, 81: 1, 21: 1, 31: 1, 19: 1, 44: 1, 29: 1}

或者:

        my_dict[i] = my_dict.get(i := int(number), 0) + 1

另一种解决方案是使用collections.Counter用于计数:

from collections import Counter

with open("data.txt", "r") as file:
    counts = Counter(f.read().split())

如果要将值转换为整数,

from collections import Counter

with open("data.txt", "r") as file:
    counts = Counter(map(int, f.read().split()))

这是通过一次将整个文件读入一个字符串,调用字符串上的str.split()来实现的,因为你的数据都是用空格分隔的,并将结果列表直接传递给Counter()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM