简体   繁体   中英

python common list of values from two lists

a = [1, 2, 4]

b = [9, 2,2,2,3,3, 4,4]

c= set(a) & set(b)

print(c)

Result: {2, 4}

i need it like: {2,2,2,4,4}

Thank you

If you'd like the duplicates in the result to correspond to the duplicates in b . (as in your example)

c= [i for i in b if i in a]

output:

[2, 2, 2, 4, 4]

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