I have two strings and I would like to have the intersection on them including duplicate items:
str_a = "aabbcc"
str_b = "aabd"
list(set(str_a) & set(str_b))
>> "ab"
I would like to have it return:
>> "aab"
Any ideas?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Multisets are implemented in python 2.7 or later as (mutable)
Counterobjects. You can perform many of the same operations as you can for sets, such as union, intersection, difference (though counts can become negative), etc.:Solution:
More details:
You can use
''.joinif you want a string, orlist()if you want a list, though I would just keep it in iterable format asintersection.elements().