如何有效地过滤词典列表中的数据

时间:2016-11-08 13:19:59

标签: python python-2.7 dictionary

我有tuple_listdictionaries_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]]

请提出一些改进方法。

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]]