首页 > 解决方案 > 如何剥离 Python 列表中的元组

问题描述

my_list = [(101)]
Output: my_list = [101]

如果 my_list 中的元素数量大于 1,那么我可以运行 for 循环并可以从列表中删除元组,但是当元素数量为 1 时,for 循环也会给出错误。错误:int 对象不可迭代。

标签: pythonarrayslisttuples

解决方案


因为 single被has typeint包围。()int

>>> t = (1)
>>> type(t)
<class 'int'>

您可以使用,它来制作元组。(如果它只有一个值。)

>>> t = (1,)
>>> type(t)
<class 'tuple'>

推荐阅读