Getting #39;int#39; object is not iterable(获取“int对象是不可迭代的)
问题描述
cat_sums[cat] += value
TypeError: 'int' object is not iterable
我的输入是这样的:
defaultdict(<type 'list'>, {'composed': [0], 'elated': [0], 'unsure': [0], 'hostile': [0], 'tired': [0], 'depressed': [0], 'guilty': [0], 'confused': [0], 'clearheaded': [0], 'anxious': [0], 'confident': [0], 'agreeable': [0], 'energetic': [0]})
这被分配给一个叫做 catnums 的东西
And this is assigned to something called catnums
accumulate_by_category(worddict, catnums, categories)
def accumulate_by_category(word_values, cat_sums, cats):
for word, value in word_values.items():
for cat in cats[word]:
cat_sums[cat] += value
现在据我所知,我并不是要迭代一个整数.我正在尝试将一个值添加到 catnums 中的另一个值.
Now as far as I can tell, I'm not trying to iterate over an integer. I'm trying to add a value to another value inside catnums.
是否有可能是我的 accumulate_by_category() 函数中的cats"参数有问题?
Is it possible that it is having trouble with the "cats" argument inside my accumulate_by_category() function?
推荐答案
每个值都是一个列表.+
运算符在应用于列表时会向列表添加 iterable.它不附加单个值:
Each of your values is a list. The +
operator, when applied to lists adds an iterable to a list. It doesn't append a single value:
>>> [1,2] + [3,4]
[1, 2, 3, 4]
>>> [1,2] + 3
TypeError: can only concatenate list (not "int") to list
看起来你想做 cat_sums[cat].append(value)
.
这篇关于获取“int"对象是不可迭代的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:获取“int"对象是不可迭代的


基础教程推荐
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 筛选NumPy数组 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01