简体   繁体   中英

Python how to iterate through a list and compare lists of strings found within

If I have a nested list that looks like this:

bigstringlist = [['rob', 'bob', 'sam', 'angie'], ['jim', 'angie', 'tom', 'sam'], ['sam', 'mary', 'angie', 'sally']]

How do I iterate through this list and extract a list of names that appear in all the nested lists? ie:

finallist = ['sam', 'angie']

Would this be better accomplished by typecasting this nested list as a set?

reduce(set.intersection, map(set , bigstringlist))

A variation on singularity's solution, maybe a little faster:

bigstringiter = iter(bigstringlist)
reduce(set.intersection, bigstringiter, set(next(bigstringiter)))

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