l1=[1,2,3,4,5]
l2=[1,1,2,3,3,4]
answer={1:'3',2:'2',3:'3',4:'1'}
it should return a dictionary with the above output
if 1 is a duplicate than it should have 1 as the key and the numbers of 1's in a list as the value
CodePudding user response:
l1=[1,2,3,4,5]
l2=[1,1,2,3,3,4]
l3=l1 l2
s=set(l3)
dic={}
for i in s:
dic.update({i:str(l3.count(i))})
output={1:'3',2:'2',3:'3',4:'1'}
CodePudding user response:
Try this:
l1=[1,2,3,4,5]
l2=[1,1,2,3,3,4]
{l : str((l1 l2).count(l)) for l in set(l1)&set(l2)}
# {1: '3', 2: '2', 3: '3', 4: '2'}
Or use collections.Counter
like below:
from collections import Counter
dct_cnt = Counter(l1 l2)
{l : str(dct_cnt(l1 l2)[l]) for l in set(l1)&set(l2)}
# {1: '3', 2: '2', 3: '3', 4: '2'}
CodePudding user response:
I think there is an error in your output. Here my solution
print({i: str(l2.count(i) 1) for i in l1 if i in l2})
Output
{1: '3', 2: '2', 3: '3', 4: '2'}
CodePudding user response:
l1=[1,2,3,4,5]
l2=[1,1,2,3,3,4]
answer={1:'3',2:'2',3:'3',4:'1'}
Please explain the above example a bit more, how exactly you are choosing the values.?