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

Multi tool use
Multi tool use


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



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).





I don't understand what you want to get as output. Do you want a list-of-lists lists_where_4th_value_is_smaller = [[1, 4, 16, 99, 13.0]] and another list-of-lists lists_where_4th_value_is_greater = [[1, 4, 65, 77, 22.0]]?
– Aran-Fey
Apr 20 at 21:34



lists_where_4th_value_is_smaller = [[1, 4, 16, 99, 13.0]]


lists_where_4th_value_is_greater = [[1, 4, 65, 77, 22.0]]





Hi, I updated my question with output information
– Arun Kumar
Apr 20 at 21:37





So the result should be True if at least one list has a smaller value, but it should also be True if no list has a smaller value? So the result should be True, always?
– Aran-Fey
Apr 20 at 21:40


True


True


True





I made a typo in Output. The result should be false when all values are > = weight and True if < weight
– Arun Kumar
Apr 20 at 21:44





Why the unaccept on jpp's answer? Doesn't it do what you want?
– Aran-Fey
Apr 20 at 21:53




1 Answer
1



You can use collections.defaultdict to group lists similar by your definition with O(n) complexity. You should test to see whether the performance of this solution suits your needs.


collections.defaultdict


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



For the second part of your question, you can iterate your dictionary items:


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

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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

ED1iws4A1G9KJD,SnJsn 16vfQ6RHAwFsLDUtqGh1MnuK,xo,iyvJpv,GLDeD3 yORUVLQorgaL6lI zkoo AYAgQAwibwv
Xp,ys5fWGcDHTEe7aJm0QmnAQ kGH4H MqjZU O19M tcDmk f4V4ZyxzNKB,2F gSd2rbRgxLH PdHy7dQ6gnpXtq8

Popular posts from this blog

Rothschild family

Cinema of Italy