Python - 创建一个 csv 文件字典,在不使用任何模块的情况下使用相同的键收集所有值
Python - creating a dictionary of csv file thath collects all values with the same key without using anymodules
你好,我参加了生物学家的 python 课程,并且掌握了使用 csv 文件的知识,
我陷入了关于用 csv 文件创建字典的最后一个问题
键作为 (c, b) 的元组,值是所有 (first, finish, fix/ext) 值的元组列表,它们具有相同的 c,b 值。
csv 看起来像这样:
N,c,g,b,first,finish,fix/ext
1000,1.0,0.02,3.0,602,1661,0
1000,0.8,0.02,3.0,42,911,0
1000,1.0,0.02,3.0,945,2164,0
1000,1.0,0.02,3.0,141,954,0
结果需要如下所示:
: {(1, 3) : [(602, 1661, 0), (945, 2164, 1), (687, 716, 1)], (1, 1.5) : [(35, 287, 0), (803, 1402, 0), …], …}
现在我设法做到了:
def read_results(full_file_path):
csv_dict = {}
with open(full_file_path,'r') as t:
table = t.readlines()[1:]
for line in table:
line = line.replace('\n', '')
line = line.split(',')
csv_dict[line[0]] = line[1:]
print(csv_dict)
这给了我这个:
{'1000': ['1.0', '0.02', '3.0', '602', '1661', '0']}
但似乎无法理解如何使用我需要的键以及如何将所有对应值的列表放入元组中
哦,我不能使用 import csv 或其他导入
你几乎完成了,唯一剩下的就是设置,你的键是什么,你的值是什么。这里我写了一个代码来完成它:
def read_results(full_file_path):
csv_dict = {}
with open(full_file_path,'r') as t:
table = t.readlines()[1:]
for line in table:
line = line.replace('\n', '')
line = line.split(',')
line = list(map(float, line)) # optional, if you want to have numbers as floats and not strings
'''
line[1] - c
line[3] - b
KEY: (c,b) - tuple of c and b
TUPLE OF CORRESPONDING VALUES: (line[4], line[5], line[6]) - tuple of first, finish, fix/ext
'''
key = (line[1],line[3])
if key in csv_dict:
# append a new value if key is already in the dict
csv_dict[key].append((line[4], line[5], line[6]))
else:
# create a new value by the key if it is not in the dict
csv_dict[key] = [(line[4], line[5], line[6])]
return csv_dict
dict = read_results("file.csv")
print(dict)
# try to get the values of dict by some value:
key = (1.0, 3.0) #key = ("0.8", "3.0") if strings were not converted to floats
print(dict[key])
希望我通过相应的评论使它易于理解。如果您有更多问题 - 写下来,我会尽量回答。
你好,我参加了生物学家的 python 课程,并且掌握了使用 csv 文件的知识, 我陷入了关于用 csv 文件创建字典的最后一个问题 键作为 (c, b) 的元组,值是所有 (first, finish, fix/ext) 值的元组列表,它们具有相同的 c,b 值。 csv 看起来像这样:
N,c,g,b,first,finish,fix/ext
1000,1.0,0.02,3.0,602,1661,0
1000,0.8,0.02,3.0,42,911,0
1000,1.0,0.02,3.0,945,2164,0
1000,1.0,0.02,3.0,141,954,0
结果需要如下所示:
: {(1, 3) : [(602, 1661, 0), (945, 2164, 1), (687, 716, 1)], (1, 1.5) : [(35, 287, 0), (803, 1402, 0), …], …}
现在我设法做到了:
def read_results(full_file_path):
csv_dict = {}
with open(full_file_path,'r') as t:
table = t.readlines()[1:]
for line in table:
line = line.replace('\n', '')
line = line.split(',')
csv_dict[line[0]] = line[1:]
print(csv_dict)
这给了我这个:
{'1000': ['1.0', '0.02', '3.0', '602', '1661', '0']}
但似乎无法理解如何使用我需要的键以及如何将所有对应值的列表放入元组中
哦,我不能使用 import csv 或其他导入
你几乎完成了,唯一剩下的就是设置,你的键是什么,你的值是什么。这里我写了一个代码来完成它:
def read_results(full_file_path):
csv_dict = {}
with open(full_file_path,'r') as t:
table = t.readlines()[1:]
for line in table:
line = line.replace('\n', '')
line = line.split(',')
line = list(map(float, line)) # optional, if you want to have numbers as floats and not strings
'''
line[1] - c
line[3] - b
KEY: (c,b) - tuple of c and b
TUPLE OF CORRESPONDING VALUES: (line[4], line[5], line[6]) - tuple of first, finish, fix/ext
'''
key = (line[1],line[3])
if key in csv_dict:
# append a new value if key is already in the dict
csv_dict[key].append((line[4], line[5], line[6]))
else:
# create a new value by the key if it is not in the dict
csv_dict[key] = [(line[4], line[5], line[6])]
return csv_dict
dict = read_results("file.csv")
print(dict)
# try to get the values of dict by some value:
key = (1.0, 3.0) #key = ("0.8", "3.0") if strings were not converted to floats
print(dict[key])
希望我通过相应的评论使它易于理解。如果您有更多问题 - 写下来,我会尽量回答。