简体   繁体   中英

Find strings that occurs in every list

I have three lists and I want to find the strings that occurs in every list. How can I do that in smart and pythonic way?

list_a = ['KOMMUN', 'PERIOD', 'ALÖS', 'LAL_12', 'LAL_24', 'AK_TOT', 'PERIOD_Q',
       'andel_lal12_alös', 'andel_lal12_ak', 'andel_lal24_alös',
       'andel_lal24_ak', 'LAN', 'geo']

list_b=[['geo', 'PERIOD', 'ALÖS', 'LAL_12', 'LAL_24', 'AK_TOT', 'PERIOD_Q',
       'andel_lal12_alös', 'andel_lal12_ak', 'andel_lal24_alös',
       'andel_lal24_ak', 'LAN_namn', 'ROW_REGION']]

list_c= ['geo', 'PERIOD', 'ALÖS', 'LAL_12', 'LAL_24', 'AK_TOT', 'PERIOD_Q',
       'andel_lal12_alös', 'andel_lal12_ak', 'andel_lal24_alös',
       'andel_lal24_ak']


list_final=['geo', 'PERIOD', 'ALÖS', 'LAL_12', 'LAL_24', 'AK_TOT', 'PERIOD_Q',
       'andel_lal12_alös', 'andel_lal12_ak', 'andel_lal24_alös',
       'andel_lal24_ak']

You can use set.intersection :

final_list = list(set(list_a) & set(list_b[0]) & set(list_c))

Output:

['PERIOD', 'ALÖS', 'LAL_24', 'geo', 'andel_lal12_ak', 'LAL_12', 'andel_lal24_alös', 'AK_TOT', 'PERIOD_Q', 'andel_lal12_alös', 'andel_lal24_ak']

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