我们知道将两个列表连接起来可以直接使用 + 操作或者extend方法。当时会保持列表原有的样式,当我们想实现下面的操作的时候,该用何种方式呢?
["a","b"] ["c","d"] => ["a", "b", "c", "d"]
我们可以直接使用sum实现:
In [41]: test_lists = [["a","b"],["c","d"]]
In [42]: sum(test_lists,[])
Out[42]: ['a', 'b', 'c', 'd']
查看sum函数:
sum(...)
sum(sequence[, start]) -> value
Return the sum of a sequence of numbers (NOT strings) plus the value
of parameter 'start' (which defaults to 0). When the sequence is
empty, return start.