'int' object has no attribute 'items'
时间: 2023-11-21 21:59:21 浏览: 409
这个错误通常是因为你尝试在一个整数类型的变量上调用字典方法items(),而整数类型没有items()方法。items()方法是字典类型的方法,它返回一个包含字典所有项的列表,每个项都是一个(key, value)对。因此,如果你想使用items()方法,你需要将其应用于字典类型的变量。以下是一个例子:
```python
person = {'name': 'zhangsan', 'age': '21', 'city': 'TianJin'}
for x in person.items():
print(x)
```
输出结果为:
```
('name', 'zhangsan')
('age', '21')
('city', 'TianJin')
```
如果你想要将字典中的键和值分别赋值给两个变量,你可以使用以下代码:
```python
person = {'name': 'zhangsan', 'age': '21', 'city': 'TianJin'}
for key, value in person.items():
print('key=', key, ',value=', value)
```
输出结果为:
```
key= name ,value= zhangsan
key= age ,value= 21
key= city ,value= TianJin
```
相关问题
AttributeError: 'int' object has no attribute 'items'
AttributeError: 'int' object has no attribute 'items'是Python中的一个属性错误。这个错误通常发生在你尝试将一个整数(int)对象作为一个字典(dict)来使用,但整数对象没有items()方法来返回键值对。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [AttributeError: ‘int‘ object has no attribute ‘items‘_Python中使用items()方法遍历字典的例子](https://blog.csdn.net/weixin_45928096/article/details/122358272)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [AttributeError: ‘int‘ object has no attribute ‘items](https://blog.csdn.net/May_JL/article/details/122746730)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [python报错: list object has no attribute shape的解决](https://download.csdn.net/download/weixin_38748721/13708867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
AttributeError: 'int' object has no attribute 'n'
报错信息中指出了错误的位置和原因。具体来说,错误出现在`r.zadd('week_charm:2022-4:542:zset', a, x)`这行代码,错误原因是`'int' object has no attribute 'items'`,意味着`int`类型没有`items`属性。为了解决这个问题,可以将`a`和`x`的数据类型转换为`str`类型。
阅读全文