我知道这是一种方式,通过放置一个逗号:
>>> empty = ()
>>> singleton = 'hello', # <-- note trailing comma
>>> len(empty)
0
>>> len(singleton)
1
>>> singleton
('hello',)
来源:http://docs.python.org/tutorial/datastructures.html
是否有更多方法来定义只有一项的元组?
最佳答案
>>> tuple(['hello'])
('hello',)
但是内置语法是有原因的。
关于python - 是否有更多方法来定义只有一项的元组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2281409/