python - 如何剥离 Python 列表中的元组
问题描述
my_list = [(101)]
Output: my_list = [101]
如果 my_list 中的元素数量大于 1,那么我可以运行 for 循环并可以从列表中删除元组,但是当元素数量为 1 时,for 循环也会给出错误。错误:int 对象不可迭代。
解决方案
因为 single被has typeint
包围。()
int
>>> t = (1)
>>> type(t)
<class 'int'>
您可以使用,
它来制作元组。(如果它只有一个值。)
>>> t = (1,)
>>> type(t)
<class 'tuple'>
推荐阅读
- c# - 使用 c# 将集合并行拆分为相等的批次
- r - 获取未过滤向量中元素的索引
- docker - 从批处理脚本调用 cmake(在 docker 内)
- string - 如何在指定字符串后通过 CMD 在文件中添加一行?
- django - Nginx:将 django 部署到 AWS EC2 实例时出现 502 BAD GATEWAY
- javascript - 在复选框检查始终使用角度 JS 获取警报中的第一行值
- angular - 无法在使用 hapi 开发的 api 和使用 angular 5 开发的前端的应用程序中实现 csrf 保护的 crumb
- javascript - 为什么 webpack 在生产模式下会忽略块?
- mysql - 每组 ID 仅获得 1 个结果
- python - keras 中的自定义 keras.applications 模型