soy-curd's blog

へぼプログラマーです [https://twitter.com/soycurd1]

pythonのreduceについて

python複数の集合を以下のように作成して、

a = set([1,2,3])
b = set([2,3,4])
c = set([3,4,5])
set_list = [a, b, c]

これらの和集合を取り出す時、

x = reduce(set.union,set_list)    # {1, 2, 3, 4, 5}

みたいにreduceにunionを書けることを知った。どうしてもreduceで書きたい時は便利かもしれない。