[英]converting singletone tuple to list
a=(3)
b=list(a)
typeError: 'int' object is not iterable typeError:“ int”对象不可迭代
but 但
a=[3]
b=tuple(a)
c=list(b)
runs without error. 运行没有错误。
please explain this. 请解释一下。
That's because (3)
is just 3
. 这是因为
(3)
只是3
。 tuple
s are defined by the comma, not the parentheses. tuple
s由逗号而不是括号定义。 If you want a tuple
with one element, add a comma: (3,)
. 如果您想要一个包含一个元素的
tuple
,请添加一个逗号: (3,)
。
(3)
is the number 3
in parentheses and (3,)
is a tuple: (3)
是括号中的数字3
, (3,)
是元组:
>>> a = (3)
>>> type(a)
<type 'int'>
>>> a = (3,)
>>> type(a)
<type 'tuple'>
There are no ambiguity with [3]
so it's a list: [3]
没有任何歧义,因此它是一个列表:
>>> a = [3]
>>> type(a)
<type 'list'>
The list
constructor accepts a tuple
or a list
but not a int
. list
构造函数接受tuple
或list
但不接受int
。
Define tuple by using comma , 使用逗号定义元组,
type( (3,) ) its of type 'tuple' type( (3,) )类型为'tuple'
type(3) its of type 'int' type(3)类型为“ int”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.