【问题标题】:How to retrieve only those elements of list which matches user input? [closed]如何仅检索与用户输入匹配的列表元素? [关闭]
【发布时间】:2017-12-15 19:26:19
【问题描述】:

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

这是我的示例输出:

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

【问题讨论】:

  • 你的问题不清楚
  • 你真的应该把formatted=False传递给model.show_topics

标签: python string arraylist lda lsa


【解决方案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))

输出:

它只返回那些在字符串中包含“人”的组。

[(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 :

【讨论】:

  • 它给了我一个空列表。
  • @Nisa 传递了正确的列表。将完整代码粘贴到 pastebin 中。
  • @Nisa 在这里查看实时运行代码:repl.it/@aadi1111/test1
  • 它没有给出任何东西..我检查了。
  • @Nisa 你检查过这个链接并点击运行了吗? repl.it/@aadi1111/test1 只需点击该链接,然后点击运行按钮,查看输出
猜你喜欢
  • 1970-01-01
  • 2020-07-05
  • 2021-08-21
  • 1970-01-01
  • 2019-02-16
  • 1970-01-01
  • 2022-12-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多