Ric*_*Chu 22 python tuples list concatenation
假设我有这些作业:
points = []
point = (1, 2)
Run Code Online (Sandbox Code Playgroud)
我怎么做的时候:
points += point
Run Code Online (Sandbox Code Playgroud)
它工作得很好,给我分数= [1,2].但是,如果我这样做:
points = points + point
Run Code Online (Sandbox Code Playgroud)
它给了我一个TypeError:只能将列表(不是"元组")连接到列表.这些陈述不是一回事吗?
Gar*_*tty 22
区别在于,list +=它相当于list.extend(),它采用任何可迭代并扩展列表,它作为一个元组是可迭代的.(并将列表扩展到原位).
另一方面,第二个分配一个新列表points,并尝试将列表连接到一个元组,这是不完成的,因为它不清楚预期结果是什么(列表或元组?).
| 归档时间: |
|
| 查看次数: |
1690 次 |
| 最近记录: |