TypeError:'int'对象在python中不可迭代 [英] TypeError: 'int' object is not iterable in python

查看:93
本文介绍了TypeError:'int'对象在python中不可迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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像这样..

I have some problem to run my python file. I want to make the csv.file for data counting like this..

standard_units 计数标准_unit1 123标准_unit2 234

standard_units count standard_unit1 123 standard_unit2 234

但是,它有这样的错误.

but, It has error like this.

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

可迭代是什么意思?

推荐答案

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

Like your error message said: ints are not iterable. Use a range

for j in range(length):
    ...

如果 length 是 10,range(length) 会给你值 0..9.

If length is 10, range(length) will give you the values 0..9.

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

You don't have to actually calculate the length in advance - instead you can enumerate the field_standard_units:

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

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

Note: Use xrange if your range is very large since range creates a list, and thus all of its members occupy the memory, whilst xrange creates a generator just like enumerate.

这篇关于TypeError:'int'对象在python中不可迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆