简体   繁体   中英

Efficient way to find similar items in a list in Python

I have a list of list as follows:

list_1 = [[[1,a],[2,b]], [[3,c],[4,d]], [[1,a],[5,d]], [[8,r],[10,u]]]

I am trying to find whether an element is this list is similar to another element. Right now, I'm looping it twice ie for each element, check against the rest. My output is:

[[[1,a],[2,b]], [[1,a],[5,d]]]

Is there a way to do this more efficiently?

Thanks.

You can use itertools.combinations and any functions like this

from itertools import combinations
for item in combinations(list_1, 2):
    if any(i in item[1] for i in item[0]):
        print item

Output

([[1, 'a'], [2, 'b']], [[1, 'a'], [5, 'd']])

I'm assuming that, by similar, you mean that the element has at least one matching pair within it. In this case, rather than do a nested loop, you could map each element into a dict of lists twice (once for each [number,str] pair within it). When you finish, each key in the dict will map to the list of elements which contain that key (ie, are similar).

Example code:

list_1 = [[[1,'a'],[2,'b']], [[3,'c'],[4,'d']], [[1,'a'],[5,'d']], [[8,'r'],[10,'u']]]

d = {}

for elt in list_1:
    s0 = '%d%s' % (elt[0][0], elt[0][1])
    if s0 in d:
        d[s0].append(elt)
    else:
        d[s0] = [elt]

    s1 = '%d%s' % (elt[1][0], elt[1][1])
    if s1 in d:
        d[s1].append(elt)
    else:
        d[s1] = [elt]

for key in d.keys():
    print key, ':', d[key]

Example output:

1a : [[[1, 'a'], [2, 'b']], [[1, 'a'], [5, 'd']]]
8r : [[[8, 'r'], [10, 'u']]]
2b : [[[1, 'a'], [2, 'b']]]
3c : [[[3, 'c'], [4, 'd']]]
5d : [[[1, 'a'], [5, 'd']]]
4d : [[[3, 'c'], [4, 'd']]]
10u : [[[8, 'r'], [10, 'u']]]

Any of the dict entries with length > 1 have similar elements. This will reduce the runtime complexity of your code to O(n), assuming you have a way to obtain a string representation of a, b, c, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM