What is the quickest way in Python to identify lists with common elements A & B in indices [i] & [j] without using expensive for loops?

时间:2018-04-20 21:27:44

标签: python list filter nested-lists

Context: I went through many similar questions on the internet for list filters and tried all suggested solutions. But they all seem to be fine for comparing two different lists. But they do not give desired results for a my list-of-lists.

Problem: I have a list of lists and two different numbers that I want to check if they are located in the first two index elements (index[0] and index[1]) of the nested lists within the list of lists. I wish to identify all such lists and then if such lists exist, compare the fourth index member of all those similar lists against a fixed number.

My Sample list:

[[1, 4, 65, 77, 22.0], [3, 2, 12, 55, 77.0], [1, 4, 16, 99, 13.0]]

Numbers to check:

`index[0] == 1 and index[1] == 4.`

The above list of lists has two such nested lists where the first index member is 1 and second index member is 4.

Hence we we now compare the fourth index member of each similar list against our reference weight = 17.

Thus there is one list where the number 22 is greater than our reference number 17; and another list where the number 13 is lesser than our reference number 17.

My Output should be :

return True if there is at least one list where the fourth index is < reference weight

return False if all values in fourth index are > = reference weight

What I am looking for is an efficient way of quickly identifying the similar lists within the list of lists. I know for loop is an option, but performance becomes an issue since my list-of-lists can get very big over time (during every iteration new list members get added to the list of lists).

1 个答案:

答案 0 :(得分:0)

您可以使用collections.defaultdict按照您的定义将类似于O(n)复杂度的列表分组。您应该测试以确定此解决方案的性能是否符合您的需求。

from collections import defaultdict

lst = [[1, 4, 65, 77, 22.0], [3, 2, 12, 55, 77.0], [1, 4, 16, 99, 13.0]]

d = defaultdict(list)

for item in lst:
    d[tuple(item[:2])].append(item)

# defaultdict(list,
#             {(1, 4): [[1, 4, 65, 77, 22.0], [1, 4, 16, 99, 13.0]],
#              (3, 2): [[3, 2, 12, 55, 77.0]]})

对于问题的第二部分,您可以迭代字典项:

res = {k: any(i[4]<17 for i in v) for k, v in d.items()}

# {(1, 4): True, (3, 2): False}