对数组 2d python 中的每一行求和 [英] sum each rows in array 2d python

查看:48
本文介绍了对数组 2d python 中的每一行求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 python 中创建二维数组,它接受用户的输入以询问要制作多少个二维数组.我的程序还对数组 2d 中每一行的值求和,并打印每个数组的最大值.

I try to create 2d array in python which accept input from user to ask how many 2d array to be made. and my program also sum value number in every rows in array 2d and print greatest value per array.

示例:

输入:

2 - for how much array need to create

2 -> for how much size array(rows default = 3)
1 2 3
4 5 6
3
0 0 0
2 3 4
5 6 7

输出:

in array 1 is : 2 (represent row) -> (4+5+6) is greatest than (1+2+3)
in array 2 is : 3 (represent row) -> (5+6+7) is greatest

我的程序堆栈如何求和并打印正确的输出.并且对于 +:. 的一些不受支持的操作数类型也有一些问题.也许有人想帮我修复它.这是我的代码

my program stack in how to sum it and print the right output. and also have some problem with some unsupported operand type(s) for +:.maybe anyone wants help me to fixed it. this my code

A = int(input("enter how many matrix create: "))
for i in range(A):
    B = int(input("enter size : ")) 
    matrix = []
    print("enter number: ")
    for j in range(B):          
        a =[]
        for k in range(B):
            a.append(input())
        matrix.append(a) 
    rows = len(matrix)
    cols = len(matrix[0])
    total=0
    for r in range(0, rows):
        rowtotal=0
        for s in range(0, cols):
            rowtotal=rowtotal+int(matrix[r][s])
        print(rowtotal)
    total=total+rowtotal
print(total)

我需要你的意见.谢谢.

i need your opinion about this. thanks.

推荐答案

不支持的操作类型为 +"当您尝试添加 string 类型和 int 类型或不同类型和不同类型时引发错误.您只能连接相同的类型

"Unsupported operant type for +" error raised when you tried to add string type and int type or different type and different type. You can only concatenate the same type

在您代码的这一部分中,我确实理解了 rowtotal 和 total 的用法.

In this part of your code, I did understand the use of rowtotal and total.

for r in range(0, rows):
    rowtotal=0
    for s in range(0, cols):
        rowtotal=rowtotal+int(matrix[r][s])
    print(rowtotal)
total=total+rowtotal

这是我根据你的问题写的示例代码.

Here is the sample code I wrote according to your question.

# accept the number of martix
a = int(input("Enter how many matrix to create: "))
sizes_of_array = []
largest_rows = []

for i in range(a):      
      b = int(input("\nEnter size: "))
      sizes_of_array.append(b)
      List = []

      # accept input rows
      for j in range(b):
            Input = input('Enter {}th row separated with blank: '.format(j+1))
            temp = Input.split()
            List.append([int(x) for x in temp])

      # print the matrix you wrote
      print('\nYour matrix: ')
      for k in range(b):
            for l in range(b):
                  print(List[k][l],end=' ')
            print()

      # find the row with largest sum value
      largest_sum = max(sum(x) for x in List)
      for x in List:
            if sum(x) == largest_sum:
                  largest_rows.append(x)
                  break

# final results
print('\n')
for i in range(a):
      print('In array {}, there is {} rows and  {}  is the greatest.'.format(i+1,sizes_of_array[i],largest_rows[i]))

根据行数接受输入;每次一排.
我在输入后打印了整个矩阵.
没有得到的部分代码请随时询问.

It accepts inputs according to number of rows; one row per times.
I made to print the whole matrix after the inputs.
Feels free to ask the part of the code you didn't get.

这篇关于对数组 2d python 中的每一行求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆