rim*_*ire 1 python tuples list python-3.x
>>> list=['a','b']
>>> tuple=tuple(list)
>>> list.append('a')
>>> print(tuple)
('a', 'b')
>>> another_tuple=tuple(list)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object is not callable
Run Code Online (Sandbox Code Playgroud)
为什么我不能将列表'list'转换为元组?
难道不是名称类别后的变量.在您的示例中,您使用list和执行此操作tuple.
您可以重写如下:
lst = ['a', 'b']
tup = tuple(lst)
lst.append('a')
another_tuple = tuple(lst)
Run Code Online (Sandbox Code Playgroud)
按行说明
您发布的代码无法按预期工作,因为:
another_tuple=tuple(list),Python会尝试将您tuple在第二行中创建的内容视为函数.tuple 变量不是调用.TypeError: 'tuple' object is not callable.| 归档时间: |
|
| 查看次数: |
6441 次 |
| 最近记录: |