繁体   English   中英

Python 2 TypeError:“int”类型的参数不可迭代

[英]Python 2 TypeError : argument of 'int' type is not iterable

这是我正在运行的代码,从这里我得到这种类型的错误如何更改因此错误

file_path = os.path.join(folder_path,filename)
f = open(file_path,'a+')
index = -1
for slicelist in slicelists:
    index += 1
    sentences = slicelist.split('\n')
    if sentences[0] == '\r' or sentences[0] == '':
        del sentences[0]
    if sentences == []:
        continue
    if sentences[-1] == '':
        del sentences[-1]
    if sentences[-1] == '\r':
        del sentences[-1]
    labellist = labellists[index]
    for labels in labellist:
        if 1 in labels:
            label = 1
        else:
            label = 0

这是输出:

Traceback (most recent call last):
  File "data_preprocess.py", line 43, in <module>
    if 1 in labels:
TypeError: argument of type 'int' is not iterable

这个问题怎么解决???

当您在if statement中使用"In" ,您必须具有可迭代项。 但它只有一项,因为您在for statement处理了迭代

你可以这样做,很简单

if 1 in lablelist:
    lable=1
else:
    label = 0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM