0
0

More than 1 year has passed since last update.

2次元配列の列ごとの和の配列

Last updated at Posted at 2023-02-14

アウトプットの練習も兼ねてメモ

プログラミングを一から学ぶつもりで色々やっています。趣味って言えるくらい楽しめるようになりたいです。
初歩的なことばかりですが温かい目でご覧ください。

map関数を用いて

l = [[1, 0, 1],
     [1, 1, 1],
     [0, 0, 1]]

#各列の合計
columns_sum = list(map(sum, zip(*l)))
print(columns_sum)

#出力
#[2, 1, 3]

リスト内包表記を用いて

l = [[1, 0, 1],
     [1, 1, 1],
     [0, 0, 1]]

#各列の合計
columns_sum = [sum(i) for i in zip(*l)]
print(columns_sum)

#出力
#[2, 1, 3]

書きやすそうなのは後者かも。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0