遍历元组列表

时间:2019-05-19 06:47:58

标签: python list tuples

我正在尝试从主题模型中提取热门单词,并按如下所示打印单词

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   [('pizza',0.13345005),('notch',0.08421454),('weekend',0.049728252),('fair',0.035808913),('thank',0.034821175),('girlfriend',0.03274733),( 'seen',0.029821698),('患者',0.026760893),('吸',0.026622303),('跳过',0.026458882),('san',0.024171583),('幸运',0.021163197),('神',0.020423584),('stellar',0.016307),('improve',0.01599736)]

print(type(item))返回 int

有人可以告诉我我要去哪里了吗?

编辑:

问题的背景是从yelp审查语料库中提取主题。我正在使用LdaModel.show_topics来分配主题以及我想了解的热门术语。所以我得到的实际上是一个list of {str, tuple of (str, float)}

4 个答案:

答案 0 :(得分:1)

test_topic中的第一项是0,即int。您无法对其进行迭代。

如果我理解正确,则您具有以下嵌套集合:

(0, [(t1, w1), (t2, w2)...])
        ^ you want ^ these

因此,您应该跳过第一个元素(0),这将为您提供一个包含tuple list的{​​{1}}的单元素(term, weight) 。然后,您可以采用该元素并对其进行遍历:

tuples

输出:

for i, (term, weight) in enumerate(test_topic[1:][0]):
    # Note that you don't actually use i here...
    print(term)

答案 1 :(得分:0)

之所以得到它,是因为列表中的第一个元素为零:

for i, item in enumerate(test_topic[1:]):

答案 2 :(得分:0)

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

现在您不能这样做:

weight, term = 0

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

weight, term = ('pizza', 0.13345005)

您没有提及所需的输出,但是我不确定您是否需要enumarate

顺序似乎很奇怪,不是term, weight吗?

所以我们可以这样做:

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,只需编写:

for term, weight in test_topic[1]:
    print(term, weight)

但是,如果您确实需要enumerate(由于您未提及的某些原因),则可以这样做:

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
...

答案 3 :(得分:0)

假设您正在尝试执行以下操作

test_topic = [('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)]
for i, tup in enumerate(test_topic):
    print(i ,tup[0], tup[1])

在您的情况下,元组(0,[[)])->具有第一个元素0的第二个元组第二个元素的元组列表 因此,当您在第一遍中枚举时, 我== 0,物品== 0