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

时间:2017-12-15 19:26:19

标签: python string arraylist lda lsa

我需要从用户那里获取输入,只有那组单词应该返回到输入字符串出现的位置。例如,如果我搜索人员,那么只应该检索人们出现的那些单词组作为输出。

这是我的示例输出:

 [(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"')]

1 个答案:

答案 0 :(得分:1)

  

使用带有两个参数的函数,一个是您想要的字符串和   第二个是你的清单:

数据是:

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"')]
  

详细解决方案:

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"')]
  

如果你想尝试,只是为了有趣的一线解决方案:

search='people'

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

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