连续求和(Python/Numpy) [英] Sum elements in a row (Python/Numpy)
问题描述
致力于使我们可以自由支配使用什么项目的项目.所以我决定要为此学习python.
Working on a project that gives us free reign on what to use. So I decided I'd learn python for it.
为简短起见,我想对我正在读取的矩阵的行"中的所有元素求和.
To make this short, I want sum all the elements in a "row" of a matrix I'm reading in.
这是我从文本文件中读取表后的2D数组的样子.
This is what my 2D array looks like after I read in my table from my text file.
['0000' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '1']
['0001' '0' '1' '0' '1' '0' '0' '0' '0' '1' '0' '0' '0' '0' '1']
['0010' '0' '1' '0' '1' '0' '0' '0' '0' '1' '0' '0' '0' '0' '1']
['0011' '0' '1' '0' '1' '0' '0' '0' '0' '1' '0' '0' '0' '0' '1']
['0100' '0' '0' '0' '0' '0' '1' '0' '1' '0' '0' '1' '0' '0' '1']
['0101' '0' '0' '1' '0' '0' '0' '1' '0' '0' '1' '0' '1' '1' '0']
['0110' '0' '0' '1' '0' '1' '0' '0' '0' '0' '1' '0' '1' '1' '0']
['0111' '0' '0' '1' '0' '0' '0' '0' '0' '0' '1' '0' '0' '1' '0']
['1000' '0' '0' '0' '0' '0' '1' '0' '1' '0' '0' '1' '0' '0' '1']
['1001' '1' '0' '0' '0' '0' '0' '1' '0' '0' '1' '0' '1' '1' '0']
['1010' '1' '0' '0' '0' '1' '0' '0' '0' '0' '1' '0' '1' '1' '0']
['1011' '1' '0' '0' '0' '0' '0' '0' '0' '0' '1' '0' '0' '1' '0']
['1100' '0' '0' '0' '0' '0' '1' '0' '1' '0' '0' '1' '0' '0' '1']
['1101' '0' '0' '0' '0' '0' '0' '1' '0' '0' '0' '0' '1' '1' '0']
['1110' '0' '0' '0' '0' '1' '0' '0' '0' '0' '0' '0' '1' '1' '0']
['1111' '0' '0' '0' '0' '0' '0' '0' '0' '0' '1' '0' '1' '1' '0']
我想对每一行的所有元素求和,但不包括索引0(4位数字).然后将这些总和存储在列表中.
I want to sum all elements of each row excluding index 0 (the 4 digit numbers). And then store those sums in a list.
这是我的总和清单应为:
This is what my list of sums should look like:
[1, 4, 4, 4, 4, 4, 5, 5, 3,.......,3] (Imagine it was all filled with the right sums)
但是,这是我的代码输出:
However, this is what my code outputs:
number of rows: 16
number of cols: 15
num1s before: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
num1s after : [3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 7, 9, 7]
我不确定错误是什么,但是我认为这与字符串/整数转换有关.由于我的表是字符串形式的,但我将其转换为整数以求和.对其进行调试可以显示正确的结果,所以我不确定错误在哪里.
I'm not sure what the error is, but I think it has to do with string/int conversion. Since my table is in strings, but I convert it to ints for summing. Debugging it shows the correct results, so I'm not sure where the error is.
这是我的代码:
import numpy
print ("Reading..")
txtfile = open("test1.txt", "r")
print(txtfile.readline())
txtfile.close()
r= numpy.genfromtxt('test1.txt',dtype=str,skiprows=1)
for x in range (0,len(r)):
print(r[x])
allTested = [0] * (len(r[0]) - 1)
num1s = [0] * (len(r[0]) - 1)
print("number of rows:", len(r))
print("number of cols:", len(r[0]))
print("num1s before:",num1s)
for x in range (0,len(r)):
for y in range(1,len(r[0])):
num1s[y-1] += int(r[x][y])
print("num1s after :",num1s)
推荐答案
您过于复杂了
np.sum(array,axis=1).tolist()
这应该返回一个包含所有行之和的列表
this should return a list which contain the sum of all rows
例如:
import numpy as np
array = np.array([range(10),range(10),range(10),range(10)])
sum_ = np.sum(array,axis=1).tolist()
print sum_
print type(sum_)
>> [45, 45, 45, 45]
>> <type 'list'>
这篇关于连续求和(Python/Numpy)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!