[英]create a dictionary from .txt file with each line as values and serial num as key
i have a dataset which is a.txt file and each line has items separated by spaces.我有一个数据集,它是一个 .txt 文件,每一行都有用空格分隔的项目。 each line is a different transaction.
每一行都是不同的交易。
the dataset looks like this:数据集如下所示:
data.txt file数据.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
i created a dictionary with keys as serial num.我用键作为序列号创建了一个字典。 starting from 0 and each line values seperated by commas as values like this
从 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'}
but i am not able to iterate through each value in dict, is there any way i can convert it into a list of values for each key但我无法遍历 dict 中的每个值,有什么方法可以将它转换为每个键的值列表
i want to find the frequency of each time in the whole dictionary and create a table我想在整个字典中找到每个时间的频率并创建一个表
item![]() |
frequency![]() |
---|---|
1 ![]() |
1 ![]() |
2 ![]() |
1 ![]() |
20 ![]() |
2 ![]() |
41 ![]() |
3 ![]() |
like the above像上面的
my_dict = {}
with open('text.csv', 'r') as file:
lines = file.readlines()
for line in lines:
my_dict[lines.index(line)] = line.strip()
this is the code i used to create the dictionary but i am not sure what i should change, also i need to find frequency of each value.这是我用来创建字典的代码,但我不确定我应该更改什么,我还需要找到每个值的频率。
Any help would be appreciated.任何帮助,将不胜感激。 thank u.
感谢你。
Since you're really just counting numbers over the entire file, you can just:由于您实际上只是在计算整个文件的数字,因此您可以:
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)
Result:结果:
{'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}
That just counts the strings representing numbers, you can turn them into actual numbers:这只是计算代表数字的字符串,您可以将它们转换为实际数字:
with open('data.txt', 'r') as file:
for number in file.read().split():
my_dict[int(number)] = my_dict.get(int(number), 0) + 1
Result:结果:
{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}
Or:或者:
my_dict[i] = my_dict.get(i := int(number), 0) + 1
An alternate solution would be to use collections.Counter
which is intended for counting:另一种解决方案是使用
collections.Counter
用于计数:
from collections import Counter
with open("data.txt", "r") as file:
counts = Counter(f.read().split())
If you want to convert the values to integers,如果要将值转换为整数,
from collections import Counter
with open("data.txt", "r") as file:
counts = Counter(map(int, f.read().split()))
This works by reading the entire file into a string at once, calling str.split()
on the string since your data are all separated by whitespace, and passing the resulting list straight to Counter()
.这是通过一次将整个文件读入一个字符串,调用字符串上的
str.split()
来实现的,因为你的数据都是用空格分隔的,并将结果列表直接传递给Counter()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.