2017-08-23 7 views
-8

リストの各行の要素の合計を計算し、行の合計の新しいリストを返すコードを記述したいと思います。最初の行の合計は、10に等しい2行目の和がように26に等しい例としてリスト内の行の合計を計算するPython

def row_sums(square): 

    square = [ 
     [1, 2, 3, 4], 
     [5, 6, 7, 8], 
     [9, 10, 11, 12], 
     [13, 14, 15, 16] 
    ] 
    print(row_sums(square)) 

ためこれは[10, 26, 42, 58]の出力を与えます。しかし、私はこれを行うには、組み込み関数を使用しないしたいと思います。これをどうやってやりますか?前もって感謝します。あなたは本当にカントsum()機能を使用する場合は

+0

* "私はこれを行うにはsum関数で構築を使用しないしたいと思います" *なぜいけないのでしょうか? – vaultah

+0

私はあなたのコードが '[10、26、42、58]'を生成するとは思わない。 – stamaimer

+0

1行の合計をどのように見つけるか知っていますか? –

答えて

1

は、ここに行を合計する機能ですが、私はそれが明示的に手順を表示するために書かれましたが、あなたは何が起こっているか理解すれば、それはリストの内包表記を見て価値があるだろう。

def row_sums(square): 

    # list to store sums 
    output = [] 

    # go through each row in square 
    for row in square: 

     # variable to store row total 
     total = 0 

     # go through each item in row and add to total 
     for item in row: 
      total += item 

     # append the row's total to the output list 
     output.append(total) 

    # return the output list 
    return output 

これは、その後、そのまま使用することができます。

square = [ 
    [1, 2, 3, 4], 
    [5, 6, 7, 8], 
    [9, 10, 11, 12], 
    [13, 14, 15, 16] 
] 

row_totals = row_sums(square) 

EDIT:あなたのコメントへの答えでは

、私が何かするだろうこのようなG:ある時点でまあ

def sum_columns(square): 

    # as before have a list to store the totals 
    output = [] 

    # assuming your square will have the same row length for each row 
    # get the number of columns 
    num_of_columns = len(square[0]) 

    # iterate over the columns 
    for i in xrange(0, num_of_columns): 

     # store the total for the column (same as before) 
     total = 0 

     # for each row, get the value for the column and add to the column total 
     # (value at index i) 
     for row in square: 
      total += row[i] 

     # append the total to the output list 
     output.append(total) 

    # return the list of totals 
    return output   
+0

ご返信ありがとうございますが、私は別の質問があります。行ではなく各列に要素を追加するには、このコードをどのように調整しますか? – Hoist

+0

私の質問を答えで編集しました。前にも触れましたが、ロジックを見ることができるように段階的に書きました。いったん動作したらうれしいです。それをより簡潔にする答え:) – RHSmith159

1

あなたは()のいずれか(和としてまたは「+」)和機能を使用する必要がありますが、あなたはあなたを書く

list(map(sum, square)) 
1

のようなマップを使用することができます独自の合計機能...

モジュールfunctoolsは、あなた自身の合計機能を書くのに便利なreduce functionを持っています。

lst = [0,1,2,3,4,5] 

15としてsum(lst)を与えるだろう:あなたはlambda functionsに慣れている場合は、このようにそれを行うことができます。 15を与えるもwoudld

from functools import reduce 

reduce(lambda x,y: x + y, l) 

:しかしreduceを使用して独自のSUM関数は次のように見えるかもしれません。残りの部分は自分で書き込むことができます(つまり、行を操作する別のリスト内に)。

1

あなたはまたcomprehensionreduceでそれを行うことができます。

[reduce(lambda x, y: x + y, item) for item in square] 
1

あなたはあなたの既存の機能への行追加することができます。

result = [] 
for row in square: # iterates trough a row 
    line = 0 #stores the total of a line 

    for num in row: #go trough every number in row 
     line += num #add that number to the total of that line 

    result.append(line) #append to a list the result 

print(result) #finally return the total of every line sum's in a list 
1

要素の和を計算するためのコードのシンプルな作品をリストの各行に

square = [ 
    [1, 2, 3, 4], 
    [5, 6, 7, 8], 
    [9, 10, 11, 12], 
    [13, 14, 15, 16] 
] 
su=[sum(i) for i in square] 
print (su) 

出力:

[10, 26, 42, 58] 
関連する問題