列表中的第一个值等于另一个列表中的第一个值。 python2.7
first value in the list is equal to the first value in the other list. python2.7
想象一下我有一个子列表
exList = [
['green', 'apple', 'NO'],
['red','apple','nO'],
['red','watermellon','no'],
['yellow','honeymellon','yes']
]
所以我想检查列表中的第一个值是否等于另一个列表中的第一个值。
所以exlist是一个子列表,它有4个不同的列表。所以我想检查第一个列表中的第一个值,并检查它是否等于另一个列表中的任何其他值。所以 Green 是第一个值,而 green 没有在另一个列表中使用,所以它应该 return False。但是,如果在其他列表中使用了绿色,则它应该 return 正确。
for i in exList:
if i+1[1] == i-[1]:
print True
我该怎么做?
所以如果我理解你的请求,你想检查 exList 中第一个列表的第一个值是否存在于每个剩余列表中?如果是这样,您可以按如下方式使用列表理解:
check = [True if (exList[0][0] in subList) else False for subList in exList[1:]]
或者如果您不理解列表理解,您可以循环执行此操作:
check = []
checkItem = exList[0][0] #Pulls the value for the first item in the first list of exList
for i in range(1,len(exList)): #Starts at 1 to skip the first list in exList
check.append(checkItem in exList[i])
print check
如果您需要遍历第一个列表中的每个项目来检查:
for eachListItem in exList[0]:
print "Search Text: %s" % (eachListItem)
curCheck = [True if (eachListItem in subList) else False for subList in exList[1:]]
print "Search Results: %s" % (True in curCheck)
注意:其中的 None 会考虑字符串中的空格或不匹配的情况(即 NO 和 No 是两个不同的值,并且会 return 错误。类似地 "No " 和 "No" 不同)
想象一下我有一个子列表
exList = [
['green', 'apple', 'NO'],
['red','apple','nO'],
['red','watermellon','no'],
['yellow','honeymellon','yes']
]
所以我想检查列表中的第一个值是否等于另一个列表中的第一个值。
所以exlist是一个子列表,它有4个不同的列表。所以我想检查第一个列表中的第一个值,并检查它是否等于另一个列表中的任何其他值。所以 Green 是第一个值,而 green 没有在另一个列表中使用,所以它应该 return False。但是,如果在其他列表中使用了绿色,则它应该 return 正确。
for i in exList:
if i+1[1] == i-[1]:
print True
我该怎么做?
所以如果我理解你的请求,你想检查 exList 中第一个列表的第一个值是否存在于每个剩余列表中?如果是这样,您可以按如下方式使用列表理解:
check = [True if (exList[0][0] in subList) else False for subList in exList[1:]]
或者如果您不理解列表理解,您可以循环执行此操作:
check = []
checkItem = exList[0][0] #Pulls the value for the first item in the first list of exList
for i in range(1,len(exList)): #Starts at 1 to skip the first list in exList
check.append(checkItem in exList[i])
print check
如果您需要遍历第一个列表中的每个项目来检查:
for eachListItem in exList[0]:
print "Search Text: %s" % (eachListItem)
curCheck = [True if (eachListItem in subList) else False for subList in exList[1:]]
print "Search Results: %s" % (True in curCheck)
注意:其中的 None 会考虑字符串中的空格或不匹配的情况(即 NO 和 No 是两个不同的值,并且会 return 错误。类似地 "No " 和 "No" 不同)