gpt4 book ai didi

python - 类型错误 : 'int' object is not iterable in python

转载 作者:行者123 更新时间:2023-11-29 07:43:32 24 4
gpt4 key购买 nike

def export_csv_standard_units(name):
cursor.execute("select distinct(standard_units) from activities where standard_type='IC50' ")
result_standard_units_distinct = cursor.fetchall()

cursor.execute("select standard_units from activities where standard_type='IC50' ")
result_standard_units = cursor.fetchall()

field_standard_units = ()
cnt = []

for result in result_standard_units_distinct:
field_standard_units = field_standard_units + (result_standard_units_distinct,)

length = int(len(field_standard_units))

for i in result_standard_units:
for j in length:
if field_standard_units(j) == result_standard_units(9):
cnt[j] = cnt[j] + 1
field = (field_standard_units, cnt)

export_field = open('%s_standard_units.csv' %name, 'wb')
export_csv_field = csv.writer(export_field)
export_csv_field.writerows([field])

export_field.close()

我在运行 python 文件时遇到一些问题。我想制作 csv.file 用于数据计数像这样..

standard_units 计数标准单元1 123标准单元2 234

但是,它有这样的错误。

   File "manage.py", line 42
for j in length:
TypeError: 'int' object is not iterable

可迭代是什么意思?

最佳答案

就像你的错误消息所说:ints 是不可迭代的。使用range

for j in range(length):
...

如果length为10,range(length)将为您提供值0..9。

您不必提前实际计算长度 - 相反,您可以 enumerate field_standard_units:

for j, unit in enumerate(field_standard_units):
if unit == result_standard_units(9): # What's 9?
...

注意:使用xrange如果您的范围非常大,因为 range 创建了一个列表,因此它的所有成员都占用了内存,而 xrange 创建了一个生成器,就像 enumerate >.

关于python - 类型错误 : 'int' object is not iterable in python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28450126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com