[英]Python - find _any_ and _all_ matches in a nested list, and return the indices
好的,这是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”,则会得到两个结果。
我建议先将嵌套列表中的条目转换为小写,然后使用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
索引,我建议使用numpy
where
命令
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]]
希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.