Crossing a Python List with Unique Elements

I have two lines, and I would like to have an intersection on them, including duplicates:

str_a = "aabbcc" str_b = "aabd" list(set(str_a) & set(str_b)) >> "ab" 

I would like to return it:

 >> "aab" 

Any ideas?

+8
python intersection multiset
source share
2 answers

Multi-links are implemented in python 2.7 or later as (mutable) Counter . You can perform many of the operations that you can perform for sets such as union, intersection, difference (although the number may become negative), etc.:

 from collections import Counter as mset 

Decision:

 (mset("aabbcc") & mset("aabd")).elements() 

More details:

 >>> intersection = mset("aabbcc") & mset("aabd") Counter({'a': 2, 'b': 1}) >>> list(intersection.elements()) ['a', 'a', 'b'] >>> ''.join(intersection.elements()) 'aab' 

You can use ''.join if you need a string or list() if you need a list, although I would just save it in the iteration format intersection.elements() .

+12
source share

Use collections.Counter for each word and use them as sets:

 >>> from collections import Counter >>> str_a, str_b = 'aabbcc', 'aabd' >>> Counter(str_a) & Counter(str_b) Counter({'a': 2, 'b': 1}) >>> ''.join((Counter(str_a) & Counter(str_b)).elements()) 'aab' 

Counter is a subclass of dict , but one that counts all the elements of the sequence with which you initialize it. Thus, "aabbcc" becomes Counter({'a': 2, 'b': 2, 'c': 2}) .

Counters act as multisets, in the case when you use 2 at the intersection, as described above, their counts are set to the minimum values ​​found in any of them, ignoring all, the number of which drops to 0. If you had to calculate their union, maximum values ​​will be used instead.

+8
source share

All Articles