Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中搜索二维数组中的特定文本_Python_Django - Fatal编程技术网

如何在Python中搜索二维数组中的特定文本

如何在Python中搜索二维数组中的特定文本,python,django,Python,Django,我是python新手。我在django有一个二维列表。现在我想检查给定的文本是否在列表中。但它不起作用。这是我的密码: newmessage = 'Bye' stockWords = [ ['hello', 'hi', 'hey', 'greetings'], ['Bye', 'Goodbye'] ] for i in range(0, len(stockWords)): if newmessage.lower() in stockWords[i]:

我是python新手。我在django有一个二维列表。现在我想检查给定的文本是否在列表中。但它不起作用。这是我的密码:

newmessage = 'Bye'

stockWords = [
    ['hello', 'hi', 'hey', 'greetings'],
    ['Bye', 'Goodbye']
]


for i in range(0, len(stockWords)):
    if newmessage.lower() in stockWords[i]: 
        return HttpResponse('Found')
    else:
        return HttpResponse('Not Found')
问题是它只对列表的第一个元素起作用,第二个元素不起作用


我做错了什么?有什么建议吗?

更新:您的代码检查
.lower()
字符串,该字符串不在任何列表中。('bye'和'bye'是两个不同的对象)我已经在没有它的情况下测试了我的代码,它可以工作:

>>> for i in stockWords:
    if newmessage in i:
        print 'found'


found
为了使其工作,您需要将列表中的所有字符串小写。
stocksLower=[stock.lower()用于库存列表中的库存词用于库存列表]

注意,它将创建一个列表,而不是列表列表


当您有一个序列(您案例中的列表)时,不需要迭代
范围
。你可以直接在上面迭代

for i in stockWords:
    if newmessage.lower() in i: 
        return HttpResponse('Found')
    else:
        return HttpResponse('Not Found')
i
现在包含
stockWords
列表中的一个元素,并测试是否包含

您的原始代码是在
stockWords
len
上迭代的,它是s2。所以它没有检查内部列表



您可以尝试一下,生成一个较低级别的扁平列表,然后在其中查找

import itertools

stockWords = [
    ['hello', 'hi', 'hey', 'greetings'],
    ['Bye', 'Goodbye']
]

stock = itertools.chain.from_iterable(stockWords)
stock_lower = [x.lower() for x in stock]
newmessage = 'Bye'
if newmessage.lower() in stock_lower: 
    return HttpResponse('Found')
else:
    return HttpResponse('Not Found')

您的stockWords是一个嵌套列表当您使用for循环时,它只包含2个元素(2个列表),您可以展开嵌套列表或更改循环逻辑,以便也可以在嵌套循环中遍历

试试这个(不要放平):

如果没有弄错的话,@tisuchi试图创建一个函数(想要返回一些东西),但没有定义。
您还可以使用列表理解。我已经修改了answer@WBM

这似乎是一个函数的一部分(您没有提供全部)。函数第一次点击
return
语句时,它将退出-因此,当列表的第一个元素中的值不匹配时,它将退出。还要注意,在Python中,您不需要迭代
范围(len(something))
。始终迭代该内容本身:
for i in stockWords
。感谢大家的提示。这仍然不起作用-如果它不在第一个元素中,它将退出函数。谢谢。那是我做的,不幸的是它对我不起作用。它总是签入列表的第一个元素,第二个元素在这里不起作用。@tisuchi这是因为您正在检查任何列表中都不存在的
.lower()
。我已经测试了代码,它可以正常工作。@Vinny,我用
.lower()
将它改成小写。@tisuchi如果你在一边使用
.lower()
,请确保你的列表也是小写的。我加了一个例子,你是个伟大的人。终于对我有用了。你节省了我的时间。还有一件事。我来自PHP背景。最近意识到,我必须学习Python。但不幸的是,我找不到任何好的Python/django社区来进行问答。例如,像Laravel/php一样,有一个很好的社区叫做laracast,那么,除了堆栈溢出之外,还有其他Python开发人员社区吗?我自己也找到了这样的社区,通过标记过滤
django
Thank@Bear Brown如果我需要在循环中获得迭代数怎么办?你能在代码中显示你的意思吗?不确定你想从我这里得到什么,请您重新表述一下您的问题。现在已经完成了,谢谢您的编辑:)
found = False
for i in stockWords:
    for j in i:
       if newmessage.lower() not in j:
           continue 
       else:
          found = True
          break
response = HttpResponse('Found') if found else HttpResponse('Not Found')
newmessage = 'Bye'

stockWords = [
                ['hello', 'hi', 'hey', 'greetings'],
                ['Bye', 'Goodbye']
            ]


for i in range(0, len(stockWords)):
    if newmessage.lower() in [str(x).lower() for x in stockWords[i]]: 
        print( 'Found')
    else:
        print( 'Not Found')
newmessage = 'Bye'

stockWords = [['hello', 'hi', 'hey', 'greetings'],['Bye', 'Goodbye']]

for r in stockWords:
    for c in r:
        if newmessage.lower() == c.lower():
            print("Found")
        else:
            print ('Not found')