python - 元组概念:TypeError:“NoneType”对象不可迭代
问题描述
代码 :
input_tuple = ('Monty Python', 'British', 1969)
y = list(input_tuple)
z = y.append("Python")
tuple_2 = tuple(z)
print(tuple_2)
预期 o/p:
('Monty Python', 'British', 1969, 'Python')
但得到:
TypeError Traceback (most recent call last)
<ipython-input-30-037856922f23> in <module>
3 y = list(input_tuple)
4 z = y.append("Python")
----> 5 tuple_2 = tuple(z)
6 # Make sure to name the final tuple 'tuple_2'
7 print(tuple_2)
TypeError: 'NoneType' object is not iterable
解决方案
这不是 append 的工作方式。您不需要在 z 中保存 y.append 值,它将直接在 y 中更新,因此创建修改后 y 的元组
看看这个,这对你有用..
input_tuple = ('Monty Python', 'British', 1969)
y = list(input_tuple)
y.append("Python")
tuple_2 = tuple(y)
print(tuple_2)
推荐阅读
- regex - 意大利语标签的正则表达式
- mysql - MySQL将来自两个不同表的id依赖字段插入第三个表
- ios - 如何从包含特定标题的日历中获取事件?
- python - 我如何在没有终端命令的情况下运行 vscode
- javascript - 使用 React Router 根据 React 中的 JSON 返回值路由到特定视图
- mysql - 在mysql中保存base64字符串
- html - 向下滚动时,可以在我的侧边栏上方看到一个空格
- javascript - React useState() 数组未更新
- ansible - Ansible 和 Terraform 调试
- python - 使用 pandas groupby 计算列