相关疑难解决方法(0)

如何创建只有一个元素的元组

在下面的例子中,我希望所有元素都是元组,为什么元组只包含一个字符串时转换为字符串?

>>> a = [('a'), ('b'), ('c', 'd')]
>>> a
['a', 'b', ('c', 'd')]
>>> 
>>> for elem in a:
...     print type(elem)
... 
<type 'str'>
<type 'str'>
<type 'tuple'>
Run Code Online (Sandbox Code Playgroud)

python

88
推荐指数
3
解决办法
4万
查看次数

标签 统计

python ×1