我有tuple_list
和dictionaries_list
中的数据,两个列表中都有数千条记录。
我想根据dictionaries_list
的记录来过滤tuple_list
的数据。目前我编写了以下代码,但是对于元组列表中的每个元素,它必须花费大量时间来迭代整个字典列表。
# tuple_list can be like [(('s', 45), ('t', 30)), (('s', 5), ('t', 3))]
for target_tuple in tuple_list:
# target_tuple can have data like (('s', 45), ('t', 30))
# dictionaries_list can have data like [{'a': 5, 's': 45, 't': 10}, {}, {}]
if some_parameter == 'something':
m1_dicts = [d for d in dictionaries_list if d['s'] == target_tuple[0][1]]
else:
m1_dicts = [d for d in dictionaries_list if d['t'] == target_tuple[0][1]]
请提出一些改进方法。
答案 0 :(得分:0)
根据我的理解,如果找到任何tuple_list在字典中匹配,你想保留字典。 它确实在运行时有所改进,因为它只运行一次tuple_list,但可能不是最好的优化,因为它仍然需要迭代字典列表
# taking small dataset as example
tuple_list=[(('s', 45), ('t', 30)), (('s', 5), ('t', 6))]
dictionaries_list = [{'a': 5, 's': 45, 't': 10}, {'s':5,'t':6}, {'a':2,'s':22,'t':30}]
tuple_dict = {}
tuple_dict['s'] = [i[0][1] for i in tuple_list]
tuple_dict['t'] = [i[1][1] for i in tuple_list]
if some_parameter == 'something':
# check for 's'
m1_dicts = [d for d in dictionaries_list if d['s'] in tuple_dict['s']]
# m1_dicts = [{'a': 5, 's': 45, 't': 10}, {'s':5,'t':6}]
else:
# check for 't'
m1_dicts = [d for d in dictionaries_list if d['t'] in tuple_dict['t']]
# m1_dicts = [{'s':5,'t':6}, {'a':2,'s':22,'t':30}]
而不是some_parameter ='something',建议使用some_patameter作为检查的关键
some_parameter = 's' # check for 's'
m1_dicts = [d for d in dictionaries_list if d[some_parameter] in tuple_dict[some_parameter]]