简体   繁体   中英

How to find if all the list of strings exits in another list in Python

I was trying out to find a way to find if all the elements of a list exists in another list. Tried the below code but still didn't work out.

#!/usr/intel/bin/python2.7

# Global Import Variables
import Tkinter, Tkconstants, tkFileDialog
import warnings
import os.path

search_line = []
search_line.append('APPLE')
search_line.append('MANGO_REQUEST')
search_line.append('SeaPort')

line = []
linez = "helo how are APPLE helloo.log.gz you MANGO_REQUEST life is cool and as usual SeaPort"
linez = linez.split()
print linez

print search_line
print linez

if all(x in search_line for x in linez):
    print "final result"
    print linez

But it never satisfied the equation. Is there anything getting missed ? Please share in your thoughts.

Thanks !

The problem is in your condition:

if all(x in search_line for x in linez):

you're checking if all 'words' from linez exist in your search_line list which is obviously False , you want to do it the other way around:

if all(x in linez for x in search_line):

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