I see that the code below can check if a word is
list1 = 'this'
compSet = [ 'this','that','thing' ]
if any(list1 in s for s in compSet): print(list1)
现在我想检查一个列表中的单词是否在另一个列表中,如下所示:
list1 = ['this', 'and', 'that' ]
compSet = [ 'check','that','thing' ]
如何最好地检查列表1中的单词是否在compSet中,并对不存在的元素进行操作,例如将'and'添加到compSet中或从list1中删除'and'?
__________________更新___________________
我发现对sys.path进行相同操作并不总是有效。下面的代码有时可用于将路径添加到sys.path中,有时则无法添加。
myPath = '/some/my path/is here'
if not any( myPath in s for s in sys.path):
sys.path.insert(0, myPath)
为什么这个不起作用?另外,如果我想在一组路径上执行相同的操作,该怎么办?
myPaths = [ '/some/my path/is here', '/some/my path2/is here' ...]
我该如何做到这一点?
myPath
变量中使用反斜杠? - brianpcksys.path
,如果这是一个问题,我建议提出另一个问题,因为它实际上是一个独立的问题。 - brianpck