0

好的,这是 Python 2.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”,我会得到两个结果。

4

1 回答 1

1

我建议先将嵌套列表中的条目转换为小写,然后使用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,我建议使用where来自的命令numpy

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]]

希望有帮助!

于 2018-08-21T20:05:05.313 回答