【问题标题】:Still problem with 'int' object is not iterable'int' 对象的问题仍然是不可迭代的
【发布时间】:2019-06-21 23:11:12
【问题描述】:

感谢您的提示,我在 python3 中有以下代码,但我一直遇到相同的错误“int”不可迭代,您知道是什么问题吗?

formatRequiredNewH={(101, 1): [2, 3], (102, 3): [3], (103, 2): [2], (104, 3): [1], (105, 2): [777]}

fixedUniqueCompilation=[1,2,3,4,5]

try:
    for actClass in list(formatRequiredNewH.keys()):        
        for h in fixedUniqueCompilation:
            print(formatRequiredNewH[actClass])
            if 2 in formatRequiredNewH[actClass][0]:
                print("Works?")
except:
    print("not")

【问题讨论】:

  • formatRequiredNewH[actClass][0] 是一个整数。你的意思是formatRequiredNewH[actClass]

标签: python-3.x int iterable


【解决方案1】:
if 2 == formatRequiredNewH[actClass][0]:
    etc..

in 对 python 来说意味着它应该查看一个可迭代对象。 ;)

除非您正在寻找 102 中的 2,然后您需要从 int 转换为 string:

if '2' in str(formatRequiredNewH[actClass][0]):

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-22
    • 2022-01-06
    • 1970-01-01
    • 2013-08-02
    • 2012-06-21
    • 1970-01-01
    • 2012-11-15
    相关资源
    最近更新 更多