简体   繁体   中英

Comparing two lists of strings and getting indices of duplicates

I want to get the indices of the values (strings) which are duplicated. For example:

a=['iii','jjj','rrr']
b=['iii','lll','yyy','ttt','jjj']    
s=numpy.where(a==b)

I want s to return [0,4] , but at the moment it just returns [0] as this is where they are the same value and in same position in the list.

Use numpy.where with numpy.in1d :

>>> np.where(np.in1d(b, a))[0]
array([0, 4]

You can use max and min function within a list comprehension :

>>> [i for i,j in enumerate(max(a,b,key=len)) if j in min(a,b,key=len)]
[0, 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