如何在Python/Spyder 2.7中比较字符串和列表

如何在Python/Spyder 2.7中比较字符串和列表,python,string,list,comparison,spyder,Python,String,List,Comparison,Spyder,我有一份清单: myList = [['first'],['second'], ['third']] 和一个比较字符串,myStr='first' 如果myList中存在myStr,我想返回True,否则在python2.7中返回False: str = "first" array = [["first"], ["second"], ...] def isInArray(string, array): for subarray in array: for element

我有一份清单:

myList = [['first'],['second'], ['third']]
和一个比较字符串,
myStr='first'

如果myList中存在myStr,我想返回
True
,否则在python2.7中返回
False

str = "first"
array = [["first"], ["second"], ...]
def isInArray(string, array):
    for subarray in array:
        for element in subarray:
            if element == string:
                return True
    return False
print isInArray(str, array)
food = [["apples", "prunes", "peaches"], ["tea", "coffee", "milk"], ["biscuits", "bread", "chips"]]

*You can try different strings here to check True/False values*

find = raw_input("What do you want in food?")

def str_in_list_of_lists(a, b):
    for i in food:
        if find in i:
            return True
    return False
print str_in_list_of_lists(find, food)
无论如何,数组没有任何意义:如果每个子数组只有一个元素,则应使其更容易:

array = ["first", "second", ...]

对于您展示的简单示例,运行

[myStr] in myList
但是,如果每个子列表只包含一项-
myList=['first'、'second'、'third'],则可能应该将
myList
设置为一个平面列表

那你只需要

myStr in myList

您只需要使用for循环迭代列表一次,以便访问子列表

myStr = 'first'
myList = [['first'],['second'], ['third']]

def str_in_list_of_lists(myStr, myList):
    for i in myList:
        if myStr in i:
            return True
    return False
print str_in_list_of_lists(myStr, myList)
Python 2.7中的示例:

str = "first"
array = [["first"], ["second"], ...]
def isInArray(string, array):
    for subarray in array:
        for element in subarray:
            if element == string:
                return True
    return False
print isInArray(str, array)
food = [["apples", "prunes", "peaches"], ["tea", "coffee", "milk"], ["biscuits", "bread", "chips"]]

*You can try different strings here to check True/False values*

find = raw_input("What do you want in food?")

def str_in_list_of_lists(a, b):
    for i in food:
        if find in i:
            return True
    return False
print str_in_list_of_lists(find, food)

我甚至猜不出你的语言。如果你想在某个时候得到一个答复,就贴上标签……我同意。数组是天真地生成的:open('csvFileName.csv','rb')为f:data=list(csv.reader中rec的list(rec)(f,分隔符=','))。我是新手,所以请告诉我如何在csv行中阅读您的更简单的格式。谢谢你。@BLevy如果还没有副本的话,你应该作为另一个问题问这个问题