私はlist_of_lists
を持っています。すべての内部リストのすべての要素を含む単一のリストを作成します。私はなぜこれを達成するためにsum
を使用することができないのか分からない。リストのリストでsum()を使用するとエラーが発生する
list_of_lists = [['element 1','element 2'],['element 1']]
print(sum(list_of_lists))
>> TypeError: unsupported operand type(s) for +: 'int' and 'list'
私はreduce
を使用している場合は、それが正常に動作します:
from functools import reduce
print(reduce(lambda x,y: x+y, list_of_lists))
>> ['element 1', 'element 2', 'element 1']
リストには文字列と数値ではないため、合計を使用できません。いずれにしても、それが機能していることを見て減らすのはなぜですか?使用後に重複しないようにするには、list(set(your_list))を使用してください。https://docs.python.org/2/tutorial/datastructures.html –
[sum'のドキュメント](https ://docs.python.org/3/library/functions.html#sum)。 – poke