【发布时间】:2019-10-30 14:32:11
【问题描述】:
我有一个只有 4 个键 (mydictionary) 和一个列表 (mynodes) 的字典,如下所示。
mydictionary = {0: {('B', 'E', 'G'), ('A', 'E', 'G'), ('A', 'E', 'F'), ('A', 'D', 'F'), ('C', 'D', 'F'), ('C', 'E', 'F'), ('A', 'D', 'G'), ('C', 'D', 'G'), ('C', 'E', 'G'), ('B', 'E', 'F')},
1: {('A', 'C', 'G'), ('E', 'F', 'G'), ('D', 'E', 'F'), ('A', 'F', 'G'), ('A', 'B', 'G'), ('B', 'D', 'F'), ('C', 'F', 'G'), ('A', 'C', 'E'), ('D', 'E', 'G'), ('B', 'F', 'G'), ('B', 'C', 'G'), ('A', 'C', 'D'), ('A', 'B', 'F'), ('B', 'D', 'G'), ('B', 'C', 'F'), ('A', 'D', 'E'), ('C', 'D', 'E'), ('A', 'C', 'F'), ('A', 'B', 'E'), ('B', 'C', 'E'), ('D', 'F', 'G')},
2: {('B', 'D', 'E'), ('A', 'B', 'D'), ('B', 'C', 'D')},
3: {('A', 'B', 'C')}}
mynodes = ['E', 'D', 'G', 'F', 'B', 'A', 'C']
我正在检查mynodes 列表中的每个节点在mydictionary 的每个键中出现了多少次。例如,考虑上面的字典和列表。
输出应该是;
{'E': [(0, 6), (1, 8), (2, 1), (3, 0)],
'D': [(0, 4), (1, 8), (2, 3), (3, 0)],
'G': [(0, 5), (1, 10), (2, 0), (3, 0)],
'F': [(0, 5), (1, 10), (2, 0), (3, 0)],
'B': [(0, 2), (1, 9), (2, 3), (3, 1)],
'A': [(0, 4), (1, 9), (2, 1), (3, 1)],
'C': [(0, 4), (1, 9), (2, 1), (3, 1)]}
例如,考虑E。在0键中出现6次,在1键中出现8次,在2键中出现2次,在3键中出现0次。
我目前的代码如下。
triad_class_for_nodes = {}
for node in mynodes:
temp_list = []
for key, value in mydictionary.items():
temp_counting = 0
for triad in value:
#print(triad[0])
if node in triad:
temp_counting = temp_counting + 1
temp_list.append(tuple((key, temp_counting)))
triad_class_for_nodes.update({node: temp_list})
print(triad_class_for_nodes)
这适用于小字典值。
但是,在我的真实数据集中,我的字典中的 4 个键中的每一个的值列表中都有数百万个元组。因此,我现有的代码效率非常低,需要几天才能运行。
当我搜索如何提高效率时,我遇到了这个问题 (Fastest way to search a list in python),它建议将值列表设置为一组。我也试过这个。但是,它也需要几天的时间才能运行。
我只是想知道在 python 中是否有更有效的方法来执行此操作。 我很高兴将我现有的数据格式转换为不同的结构(例如pandas dataframe)以提高效率。
下面附上mydictionary 和mynodes 的小样本用于测试目的。 https://drive.google.com/drive/folders/15Faa78xlNAYLPvqS3cKM1v8bV1HQzW2W?usp=sharing
-
mydictionary:见 triads.txt
with open("triads.txt", "r") as file:mydictionary = ast.literal_eval(file.read)
mynodes:参见nodes.txt
with open("nodes.txt", "r") as file:
mynodes = ast.literal_eval(file.read)
如果需要,我很乐意提供更多详细信息。
【问题讨论】: