>>> l=range(3)
>>> t=(3,4,5)
>>> l
[0,
1, 2]
>>> t
(
3, 4, 5)
>>> l.extend(t)
>>> l
[0,
1, 2, 3, 4, 5]
>>> l+=t
>>> l
[0,
1, 2, 3, 4, 5, 3, 4, 5]

看来list对象的+=操作和extend方法有异曲同工之处.

如果我们直接l+t,就会报错,因为+不会对不同类型的object进行操作.

>>> l+t

Traceback (most recent call last):
File
"<pyshell#17>", line 1, in <module>
l
+t
TypeError: can only concatenate list (
not "tuple") to list

相关文章:

  • 2022-12-23
  • 2021-05-19
  • 2022-12-23
  • 2022-12-23
  • 2021-05-30
  • 2021-12-22
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-10
  • 2021-06-05
  • 2021-07-20
  • 2022-12-23
  • 2022-12-23
  • 2022-02-15
相关资源
相似解决方案