私は、リストを展開したい場合は、私は以下のような方法を見つけた:組み込み関数sumはsum(list、[])と何をするのですか?
>>> a = [[1, 2], [3, 4], [5, 6]]
>>> a
[[1, 2], [3, 4], [5, 6]]
>>> sum(a, [])
[1, 2, 3, 4, 5, 6]
私はこれらの行に何が起こったのか知っている、とthe documentation状態はありません:
sum(iterable[, start])
Sums
start
and the items of aniterable
from left to right and returns the total.start
defaults to0
. The iterable's items are normally numbers, and thestart
value is not allowed to be a string.For some use cases, there are good alternatives to
sum()
. The preferred, fast way to concatenate a sequence of strings is by calling''.join(sequence)
. To add floating point values with extended precision, seemath.fsum()
. To concatenate a series of iterables, consider usingitertools.chain()
.New in version 2.3.
ないあなたはスタートは数字でなければならないと思いますか?なぜ[]
と書くことができますか?
(sum(a, []))
'sum'は実際に文字列を拒否するようにコード化されています。 – interjay
@interjayちょっと、OPを編集してそれに応じて修正したときに見つけた – jonrsharpe
良い答えは、新しい何かを学びました:それはエラーをタイプエラー:sum()は文字列を合計することはできません[ ) – Noxeus