如何仅检索与用户输入匹配的列表元素?

How to retrieve only those elements of list which matches user input?

我需要从用户那里获取输入,只有输入字符串出现的那组词应该 return 给我。例如,如果我搜索人,那么只有出现人的那些词组应该被检索为输出。

这是我的示例输出:

 [(0, '0.897*"allah" + 0.120*"indeed" + 0.117*"lord" + 0.110*"said" + 0.101*"people" + 0.093*"upon" + 0.083*"shall" + 0.082*"unto" + 0.072*"believe" + 0.070*"earth"'), (1, '0.495*"lord" + 0.398*"said" + -0.377*"allah" + 0.252*"shall" + 0.241*"people" + 0.236*"unto" + 0.195*"indeed" + 0.131*"upon" + 0.117*"come" + 0.109*"thou"'), (2, '-0.682*"lord" + 0.497*"shall" + 0.350*"unto" + 0.125*"thee" + 0.125*"thou" + -0.098*"indeed" + -0.092*"said" + 0.092*"come" + 0.091*"people" + 0.080*"truth"'), (3, '-0.615*"shall" + 0.520*"people" + -0.395*"lord" + 0.259*"said" + 0.227*"indeed" + 0.103*"would" + 0.081*"sent" + 0.078*"among" + -0.059*"deeds" + -0.053*"good"'), (4, '0.675*"unto" + -0.425*"shall" + -0.335*"indeed"  + 0.214*"thou" + 0.180*"thee" + 0.161*"lord" + -0.105*"said" + 0.099*"hath" + -0.075*"upon"'), (5, '-0.760*"said" + 0.356*"indeed" + 0.261*"upon" + 0.157*"would" + -0.130*"shall" + 0.109*"earth" + -0.108*"allah" + 0.105*"lord" + 0.100*"truth" + 0.096*"good"')

这是我的预期输出:

 [(0, '0.897*"allah" + 0.120*"indeed" + 0.117*"lord" + 0.110*"said" + 0.101*"people" + 0.093*"upon" + 0.083*"shall" + 0.082*"unto" + 0.072*"believe" + 0.070*"earth"'), (1, '0.495*"lord" + 0.398*"said" + -0.377*"allah" + 0.252*"shall" + 0.241*"people" + 0.236*"unto" + 0.195*"indeed" + 0.131*"upon" + 0.117*"come" + 0.109*"thou"'), (2, '-0.682*"lord" + 0.497*"shall" + 0.350*"unto" + 0.125*"thee" + 0.125*"thou" + -0.098*"indeed" + -0.092*"said" + 0.092*"come" + 0.091*"people" + 0.080*"truth"'), (3, '-0.615*"shall" + 0.520*"people" + -0.395*"lord" + 0.259*"said" + 0.227*"indeed" + 0.103*"would" + 0.081*"sent" + 0.078*"among" + -0.059*"deeds" + -0.053*"good"')]

Use a function with two parameter , one is your desired string and second is your list :

数据是:

data=[(0,
  '0.897*"allah" + 0.120*"indeed" + 0.117*"lord" + 0.110*"said" + 0.101*"people" + 0.093*"upon" + 0.083*"shall" + 0.082*"unto" + 0.072*"believe" + 0.070*"earth"'),
 (1,
  '0.495*"lord" + 0.398*"said" + -0.377*"allah" + 0.252*"shall" + 0.241*"people" + 0.236*"unto" + 0.195*"indeed" + 0.131*"upon" + 0.117*"come" + 0.109*"thou"'),
 (2,
  '-0.682*"lord" + 0.497*"shall" + 0.350*"unto" + 0.125*"thee" + 0.125*"thou" + -0.098*"indeed" + -0.092*"said" + 0.092*"come" + 0.091*"people" + 0.080*"truth"'),
 (3,
  '-0.615*"shall" + 0.520*"people" + -0.395*"lord" + 0.259*"said" + 0.227*"indeed" + 0.103*"would" + 0.081*"sent" + 0.078*"among" + -0.059*"deeds" + -0.053*"good"'),
 (4,
  '0.675*"unto" + -0.425*"shall" + -0.335*"indeed"  + 0.214*"thou" + 0.180*"thee" + 0.161*"lord" + -0.105*"said" + 0.099*"hath" + -0.075*"upon"'),
 (5,
  '-0.760*"said" + 0.356*"indeed" + 0.261*"upon" + 0.157*"would" + -0.130*"shall" + 0.109*"earth" + -0.108*"allah" + 0.105*"lord" + 0.100*"truth" + 0.096*"good"')]

Detailed solution :

def search_strin(stri,list_1):
    final_list=[]
    for tup in list_1:
        for item in tup:
            if isinstance(item,str):
                if stri in item:
                    final_list.append(tup)

    return final_list

print(search_strin('people',data))

输出:

它只返回那些在字符串中有 'people' 的组。

[(0, '0.897*"allah" + 0.120*"indeed" + 0.117*"lord" + 0.110*"said" + 0.101*"people" + 0.093*"upon" + 0.083*"shall" + 0.082*"unto" + 0.072*"believe" + 0.070*"earth"'), (1, '0.495*"lord" + 0.398*"said" + -0.377*"allah" + 0.252*"shall" + 0.241*"people" + 0.236*"unto" + 0.195*"indeed" + 0.131*"upon" + 0.117*"come" + 0.109*"thou"'), (2, '-0.682*"lord" + 0.497*"shall" + 0.350*"unto" + 0.125*"thee" + 0.125*"thou" + -0.098*"indeed" + -0.092*"said" + 0.092*"come" + 0.091*"people" + 0.080*"truth"'), (3, '-0.615*"shall" + 0.520*"people" + -0.395*"lord" + 0.259*"said" + 0.227*"indeed" + 0.103*"would" + 0.081*"sent" + 0.078*"among" + -0.059*"deeds" + -0.053*"good"')]

Just for fun one line solution if you want to try:

search='people'

print([tup for tup in data for item in tup if isinstance(item,str) if search in item])

正如您评论的那样,您得到的是空列表,您应该检查您传递的列表是否正确。你可以在这里查看 live running code :