[英]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.