简体   繁体   中英

Find all values in a list which match in a dictionary?

lets say I have the following dictionary

dict1= {"a":[1,2,3],"b":[2,3,4]}

how would I make it so the output is:

[2,3]

where the values match in a dictionary.

you can refer to my code bellow:

arr1 = d['a']
arr2 = d['b']
arrResult = []
for i in arr1:
  if arr2.__contains__(i):
    arrResult.append(i)
print(arrResult)

and here is the result: [2, 3]

More advanced you can use:

x = set.intersection(*map(set, d.values()))    
print(x)

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