我們知道將兩個(gè)列表連接起來(lái)可以直接使用 + 操作或者extend方法。當(dāng)時(shí)會(huì)保持列表原有的樣式,當(dāng)我們想實(shí)現(xiàn)下面的操作的時(shí)候,該用何種方式呢讼撒?
["a","b"] ["c","d"] => ["a", "b", "c", "d"]
我們可以直接使用sum實(shí)現(xiàn):
In [41]: test_lists = [["a","b"],["c","d"]]
In [42]: sum(test_lists,[])
Out[42]: ['a', 'b', 'c', 'd']
查看sum函數(shù):
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.