"诠释"对象不可迭代 [英] "Int" object is not iterable
问题描述
我正在尝试运行for循环。以下是我遇到问题的代码部分:
aldurstengd_ororka = {(18,19,20,21,22 ,23,24):1,(25):0.95,(26):0.90,
(27):0.85,(28,29):0.75,(30,31):0.65,(32,33) ):0.55,(34,35):0.45,
(36,37):0.35,(40,41,42,43,44,45):0.15,(46,47,48,49,50) ):0.10,
(51,52,53,54,55):0.075,(56,57,58,59,60):0.05,(61,62,63,64,
65 ,66):0.025}
年龄为aldurstengd_ororka.keys():
为年龄项:
如果ororkualdur == item:
baetur = baetur + ororkulifeyrir * aldurstengd_ororka([年龄])
所以我打算通过aldurstengd_ororka,并为每个 age字典中的元组,我为元组内的每个item运行另一个for循环。我得到的错误是
TypeError:'int'对象不可迭代
如果 aldurstengd_ororka
是字典,则表达式为:
aldurstengd_ororka([年龄])
是一个错误。也许你的意思是:
aldurstengd_ororka [(年龄)]
编辑:您看到的错误非常有趣,我确实使用此片段重现了它:
<$ p $在aldurstengd_ororka.keys()中年龄为p>
:
打印'年龄:',年龄为
年龄项:
打印商品
代码的输出是:
年龄:(32,33)
32
33
年龄:(36,37)
36
37
年龄:(51, 52,53,54,55)
51
52
53
54
55
年龄:(61,62,63,64,65, 66)
61
62
63
64
65
66
年龄:(30,31)
30
31
年龄:25
------------------------------------ ---------------------------------------
TypeError Traceback(最近一次调用最后一次)
/home/ma/mak/Documents/t.py in< module>()
3 for aldurstengd_ororka.keys():
4打印'年龄:',年龄
----> 5表示年龄项目:
6打印项目
7
TypeError:'int'对象不可迭代
因此,当将它分配给年龄变量时,Python'解包'一个1元素的元组。所以年龄而不是(25)
,如你所料,只是 25
......这有点奇怪。解决方法是执行以下操作:
年龄在aldurstengd_ororka.keys()中:
#if if not tuple ,使它成为一个元组:
如果不是类型(年龄)==类型((0,1)):年龄=(年龄,)
打印'年龄:',年龄
项目年龄:
打印商品
I'm trying to run a for loop. Here's the section of my code I'm having trouble with:
aldurstengd_ororka = {(18, 19, 20, 21, 22, 23, 24):1, (25):0.95, (26):0.90,
(27):0.85, (28, 29):0.75, (30, 31):0.65, (32, 33):0.55, (34, 35):0.45,
(36, 37):0.35, (40, 41, 42, 43, 44, 45):0.15, (46, 47, 48, 49, 50):0.10,
(51, 52, 53, 54, 55):0.075, (56, 57, 58, 59, 60):0.05, (61, 62, 63, 64,
65, 66):0.025}
for age in aldurstengd_ororka.keys():
for item in age:
if ororkualdur == item:
baetur = baetur + ororkulifeyrir * aldurstengd_ororka([age])
So my intention is to run through aldurstengd_ororka, and for each "age" tuple in the dictionary, I run another for loop for each "item" inside the tuple. The error I get is
TypeError: 'int' object is not iterable
If aldurstengd_ororka
is a dictionary, then this expression:
aldurstengd_ororka([age])
is an error. Perhaps you meant something like:
aldurstengd_ororka[(age)]
EDIT: The error you were seeing is quite interesting, I did reproduce it with this snippet:
for age in aldurstengd_ororka.keys():
print 'age:', age
for item in age:
print item
The output of the code is:
age: (32, 33)
32
33
age: (36, 37)
36
37
age: (51, 52, 53, 54, 55)
51
52
53
54
55
age: (61, 62, 63, 64, 65, 66)
61
62
63
64
65
66
age: (30, 31)
30
31
age: 25
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/ma/mak/Documents/t.py in <module>()
3 for age in aldurstengd_ororka.keys():
4 print 'age:', age
----> 5 for item in age:
6 print item
7
TypeError: 'int' object is not iterable
So, what happens is Python 'unpacks' a tuple of 1 element when assigning it to the age variable. So age instead of (25)
, as you would expect, is just 25
... It's a bit strange. A workaround would be to do something like:
for age in aldurstengd_ororka.keys():
# if not tuple, make it a tuple:
if not type(age) == type( (0,1) ): age = (age,)
print 'age:', age
for item in age:
print item
这篇关于"诠释"对象不可迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!