Python在嵌套列表中查找\u any和\u all匹配项,并返回索引

2024-06-25 22:51:43 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我有一个建议:

input default "0" length 20 value VariableInputValue('playstore_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

这是一个修改后的搜索列表:

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”,我会得到两个结果。你知道吗


Tags: 函数字符串indefault列表searchifis
1条回答
网友
1楼 · 发布于 2024-06-25 22:51:43

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

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 ]

使用上述代码,result的值为:

[[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的索引,我建议使用来自numpywhere命令

import numpy as np
idx = np.where(result)

例如,searchFor = 'life'idx的值将是:

(array([2, 2], dtype=int64), array([0, 2], dtype=int64))

要查找索引而不使用numpy(没有那么优雅):

indices = [ [idx if val else -1 for idx, val in enumerate(r) ] for r in result ]

这将给出与发生匹配的索引对应的正值,否则将给出-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]]

希望有帮助!你知道吗

相关问题 更多 >