Python—在嵌套列表中查找_any_uuu和_all_uuu匹配项,并返回索引

Python—在嵌套列表中查找_any_uuu和_all_uuu匹配项,并返回索引,python,python-2.7,nested-lists,renpy,Python,Python 2.7,Nested Lists,Renpy,好的,这是Python2.7和Ren'Py的一部分,请容忍我(我已经生锈了,所以我可能只是在做一些非常愚蠢的事情) 我有一个意见: 输入默认“0”长度20值变量InputValue('playstore\u search') 然后运行一个函数来检查嵌套列表(当前为一个)中的匹配项: if playstore_search.strip(): $ tempsearch = playstore_search.strip() text tempsearch: color

好的,这是Python2.7和Ren'Py的一部分,请容忍我(我已经生锈了,所以我可能只是在做一些非常愚蠢的事情)

我有一个意见:

输入默认“0”长度20值变量InputValue('playstore\u search')

然后运行一个函数来检查嵌套列表(当前为一个)中的匹配项:

if playstore_search.strip():
    $ tempsearch = playstore_search.strip()
    text tempsearch:
        color "#000"
        yalign .5 # this is just temporary to show me what the tempsearch looks like
    $ realtimesearchresult = realtime_search(tempsearch,playstore_recommended)
    if realtimesearchresult:
        text "[realtimesearchresult]":
            color "#000"
            yalign .6
这将继续调用此函数:

def realtime_search(searchterm=False,listname=False):
    if searchterm and listname:
        indices = [i for i, s in enumerate(listname) if searchterm in s]
        if indices:
            return indices
myListOfLists = [
            ['HSS','Studio Errilhl','hss'],
            ['Making Movies','Droid Productions','makingmovies'],
            ['Life','Fasder','life'],
            ['xMassTransitx','xMTx','xmasstransitx'],
            ['Parental Love','Luxee','parentallove'],
            ['A Broken Family','KinneyX23','abrokenfamily'],
            ['Inevitable Relations','KinneyX23','inevitablerelations'],
            ['The DeLuca Family','HopesGaming','thedelucafamily'],
            ['A Cowboy\'s Story','Noller72','acowboysstory']
]

searchFor = 'hss'
result = [ [ l.lower().find(searchFor) == 0 for l in thisList ] for thisList in myListOfLists ]
这是它搜索内容的修改列表:

default playstore_recommended = [
            ['HSS','Studio Errilhl','hss'],
            ['Making Movies','Droid Productions','makingmovies'],
            ['Life','Fasder','life'],
            ['xMassTransitx','xMTx','xmasstransitx'],
            ['Parental Love','Luxee','parentallove'],
            ['A Broken Family','KinneyX23','abrokenfamily'],
            ['Inevitable Relations','KinneyX23','inevitablerelations'],
            ['The DeLuca Family','HopesGaming','thedelucafamily'],
            ['A Cowboy\'s Story','Noller72','acowboysstory']
]
现在,如果我搜索
hss
,它会发现-如果我搜索
makingmovies
,它会发现-但是,如果我搜索
droid
(或者
droid
,因为它目前不区分大小写),它不会找到任何东西

因此,这至少是一个双重问题: 1.我如何使整个事情不区分大小写 2.如何使其匹配部分字符串

编辑:

好了,现在一切都正常了。然而,仍然存在一些问题。要匹配的完整列表比上面发布的要复杂得多,而且它似乎不匹配“在字符串中间”的字符串命中率,而只是第一个单词。所以,如果我有这样的东西:

[
 ['This is a test string with the words game and move in it'],
 ['This is another test string, also containing game']
]

我搜索“游戏”,一个会有两个结果。但是我得到了0。但是,如果我搜索“this”,我会得到两个结果。

我建议先将嵌套列表中的条目转换为小写,然后使用
find()
搜索术语。考虑以下功能:

def realtime_search(searchterm=False,listname=False):
    if searchterm and listname:
        indices = [i for i, s in enumerate(listname) if searchterm in s]
        if indices:
            return indices
myListOfLists = [
            ['HSS','Studio Errilhl','hss'],
            ['Making Movies','Droid Productions','makingmovies'],
            ['Life','Fasder','life'],
            ['xMassTransitx','xMTx','xmasstransitx'],
            ['Parental Love','Luxee','parentallove'],
            ['A Broken Family','KinneyX23','abrokenfamily'],
            ['Inevitable Relations','KinneyX23','inevitablerelations'],
            ['The DeLuca Family','HopesGaming','thedelucafamily'],
            ['A Cowboy\'s Story','Noller72','acowboysstory']
]

searchFor = 'hss'
result = [ [ l.lower().find(searchFor) == 0 for l in thisList ] for thisList in myListOfLists ]
使用上述代码,
结果的值为:

[[True, False, True],
 [False, False, False],
 [False, False, False],
 [False, False, False],
 [False, False, False],
 [False, False, False],
 [False, False, False],
 [False, False, False],
 [False, False, False]]
如果希望从整个列表中仅查找一个布尔值,请执行以下操作:

any([any(r) for r in result])
如果使用
searchFor='droid'
,它也应该返回
True


要查找
True
的索引,我建议使用
numpy中的
where
命令

import numpy as np
idx = np.where(result)
例如,
searchFor='life'
,idx的值将是:

(array([2, 2], dtype=int64), array([0, 2], dtype=int64))
要查找索引而不使用
numpy
(没有那么优雅):

这将给出与发生匹配的索引对应的正值,否则将给出
-1

[[-1, -1, -1],
 [-1, -1, -1],
 [0, -1, 2],
 [-1, -1, -1],
 [-1, -1, -1],
 [-1, -1, -1],
 [-1, -1, -1],
 [-1, -1, -1],
 [-1, -1, -1]]

希望有帮助

好吧,这也许有点帮助,但它所做的是返回一个布尔值-这对我来说是完全无用的,除非我可以获取那些布尔值所在的索引,或者整个内部列表项感谢更新-我将进行测试,看看我从中得到了什么:)和。。。numpy不可用,那么有没有不使用numpy的方法?@junkfoodjunkie已经添加了一个非numpy版本,但它没有
where
那么优雅。是的,我自己发现了这一点(但numpy在Renpy中不是一个模块,所以不是一个选项)。然而,我发现了另一个问题——似乎字符串匹配只匹配字符串的第一个单词——我将稍微更新我的问题