首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >遍历元组列表

遍历元组列表
EN

Stack Overflow用户
提问于 2019-05-19 14:47:58
回答 4查看 96关注 0票数 -2

我尝试从一个主题模型中提取最热门的单词,并按如下方式打印这些单词

代码语言:javascript
复制
test_topic = [(0, [('pizza', 0.13345005), ('notch', 0.08421454), ('weekend', 0.049728252), ('fair', 0.035808913), ('thank', 0.034821175), ('girlfriend', 0.03274733), ('seen', 0.029821698), ('patient', 0.026760893), ('sucked', 0.026622303), ('skip', 0.026458882), ('san', 0.024171583), ('luckily', 0.021163197), ('god', 0.020423584), ('stellar', 0.016307), ('improve', 0.01599736)]),(1, [('ingredients', 0.019390099), ('opening', 0.018882414), ('choice', 0.013553904), ('summer', 0.01068847), ('minute', 0.010665418), ('asian', 0.010231626), ('money', 0.010114605), ('near', 0.00918076), ('dined', 0.008954125), ('odd', 0.0087335445), ('14', 0.008653159), ('noise', 0.008145982), ('place', 0.008041287), ('live', 0.0075712656), ('definitely', 0.007468632)]),(2, [('pork', 0.022275768), ('chicken', 0.022122012), ('ribs', 0.021125246), ('strips', 0.018241541), ('green', 0.014933401), ('tomato', 0.013756915), ('cheese', 0.013535802), ('juice', 0.012698732), ('soup', 0.012126858), ('good', 0.011680452), ('sauce', 0.011264608), ('grilled', 0.010635098), ('favorite', 0.010507565), ('fat', 0.009539875), ('meat', 0.009525091)])]

for i, item in enumerate(test_topic):
    for weight, term in item:
        print(term)

然而,我得到了这个错误

TypeError:“int”对象是不可迭代的

尽管print(item)返回

0(‘披萨’,0.13345005),('notch',0.08421454),(‘周末’,0.049728252),(‘公平’,0.035808913),(‘谢谢’,0.034821175),(‘女友’,0.03274733),('seen',0.029821698),('patient',0.026760893),('sucked',0.026622303),('skip',0.026458882),('san',0.024171583),(‘幸运’,0.021163197),(‘上帝’,0.020423584),(‘明星’,0.016307),(‘改进’,0.01599736)

print(type(item))返回int

有人能指出我哪里出了问题吗?

编辑:

问题的背景是从yelp评论语料库中提取主题。我正在使用LdaModel.show_topics为我提供主题分布,并从这些分布中了解最热门的术语。所以我得到的实际上是一个list of {str, tuple of (str, float)}

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-05-19 14:53:40

test_topic中的第一项是0,它是一个int。你不能遍历它。

如果我没理解错的话,你有以下嵌套的集合:

代码语言:javascript
复制
(0, [(t1, w1), (t2, w2)...])
        ^ you want ^ these

因此,您应该跳过第一个元素( 0),它提供了一个包含(term, weight) tupleslist的单元素tuple。然后,您可以获取该元素并对其进行迭代:

代码语言:javascript
复制
for i, (term, weight) in enumerate(test_topic[1:][0]):
    # Note that you don't actually use i here...
    print(term)

输出:

代码语言:javascript
复制
pizza
notch
weekend
fair
thank
girlfriend
seen
patient
sucked
skip
san
luckily
god
stellar
improve
票数 1
EN

Stack Overflow用户

发布于 2019-05-19 14:51:08

你得到它是因为列表中的第一个元素是零:

代码语言:javascript
复制
for i, item in enumerate(test_topic[1:]):
票数 0
EN

Stack Overflow用户

发布于 2019-05-19 15:01:27

您正在枚举一个tuple,因此您的第一个item0

现在你不能这样做:

代码语言:javascript
复制
weight, term = 0

因为你需要一个像('pizza', 0.13345005)这样的元组,所以你可以这样做:

代码语言:javascript
复制
weight, term = ('pizza', 0.13345005)

您没有提到您想要的输出是什么,但是我不能完全确定您是否需要enumarate

顺序看起来很奇怪,不是应该是term, weight吗?

所以我们可以这样做:

代码语言:javascript
复制
test_topic = (
    0, 
    [
        ('pizza', 0.13345005), 
        ('notch', 0.08421454), 
        ('weekend', 0.049728252),
        ...
    ]
)

for item in test_topic[1]:
    term, weight = item
    print(term, weight)
output:
pizza 0.13345005
notch 0.08421454
weekend 0.049728252
...

你在这里实际上不需要item,你可以简单地写:

代码语言:javascript
复制
for term, weight in test_topic[1]:
    print(term, weight)

然而,如果你确实需要enumerate (出于某些你没有提到的原因),你可以这样做:

代码语言:javascript
复制
for i, item in enumerate(test_topic[1]):
    term, weight = item
    print(f'{i}. The weight of {term} is {weight}')

output:
0. The weight of pizza is 0.13345005
1. The weight of notch is 0.08421454
2. The weight of weekend is 0.049728252
...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56205411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档