本文介绍了关于 list.extend(tuple)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码在这里:
>>>a = ['xyz']>>>b = (5,)>>>a.扩展(b)>>>一种['xyz', 5]我查了很多资料,但他们都告诉我:
list.extend(seq)seq -- 这是元素列表
我想知道为什么列表可以扩展元组.
谢谢.
解决方案
如果查看 docstring
for extend
,您会发现它可以通过任何方式扩展可迭代:
""" L.extend(iterable) -> None -- 通过附加来自可迭代对象 """ 的元素来扩展列表
这意味着您不仅限于list
s 或tuple
s,还包括string
s、set
>s 和其他可迭代对象:
for iterable in ('ab', set([1, 2, 3]), {1:2, 3:4}, (0, 1)):l = [1]l.extend(可迭代)打印(升)
打印:
[1, 'a', 'b'][1, 1, 2, 3][1, 1, 3] # Note 字典默认遍历键[1, 0, 1]
the code is here:
>>> a = ['xyz']
>>> b = (5,)
>>> a.extend(b)
>>> a
['xyz', 5]
I checked a lot of information,but they both told me that:
list.extend(seq)
seq -- This is the list of elements
I want know why list can extend a tuple.
thx.
解决方案
If you look at the docstring
for extend
, you will see that it can be extended by any iterable:
""" L.extend(iterable) -> None -- extend list by appending elements from the iterable """
This means you are not only limited to list
s or tuple
s, but also string
s, set
s, and other iterables:
for iterable in ('ab', set([1, 2, 3]), {1:2, 3:4}, (0, 1)):
l = [1]
l.extend(iterable)
print(l)
This prints:
[1, 'a', 'b']
[1, 1, 2, 3]
[1, 1, 3] # Note dictionaries iterate over keys by default
[1, 0, 1]
这篇关于关于 list.extend(tuple)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!