简体   繁体   English

如何在Python中获取每一行?

[英]How to get each row in Python?

I need to do this: 我需要这样做:

"""
Program a function

    def increasing (m)

that will be able, for any matrix m of positive integers, to check whether the sums of the rows in this array are increasing. 对于任何正整数矩阵m,它将能够检查该数组中行的总和是否在增加。

examples
1 3 2 5        The sums of the rows        2 8 4 1        The sums of the rows
7 9 4 1        are 11, 21 and 23.          6 2 8 5        are 15, 21 and 15.
3 5 6 9        --> increasing              8 4 2 1        --> not increasing
"""

So, I want to use sum() , which is perfectly doable, I guess. 所以,我想使用sum() ,这是完全可行的,我想。

I started like this: 我开始是这样的:

def increasing (m):
    for row in m:
        row[1]

But I know that row[1] will just output the numbers in index 1 of each row. 但我知道row [1]只会输出每行索引 1中的数字。 What I have in my mind is this: 我的想法是:

def increasing (m):
    for row in m:
        if sum(row)[first_row] > sum(row)[second_row]:
           return False

But that is just slicing it, so I don't know how to count the rows so that I can compare them. 但这只是切片,所以我不知道如何计算行数,以便我可以比较它们。

I don't want to use any module or whatsoever, just plain simple Python. 我不想使用任何模块或任何模块,只是简单的Python。 Can someone point me in the right direction? 有人能指出我正确的方向吗? I just need it to be as simple as possible. 我只需要它尽可能简单。

Input format sample: 输入格式示例:

increasing_l = [
    [1, 3, 2, 5],
    [7, 9, 4, 1],
    [3, 5, 6, 9]
]

not_increasing_l = [
    [2, 8, 4, 1],
    [6, 2, 8, 5],
    [8, 4, 2, 1]
]

test1 = increasing(increasing_l)
test2 = increasing(not_increasing_l)

print "should be True: %s" % test1
print "should be False: %s" % test2

You can do the following: 您可以执行以下操作:

def increasing(m):
    return all(sum(r1) < sum(r2) for r1, r2 in zip(m, m[1:]))

This uses zip to pair adjacent rows and all to efficiently do the pairwise sum comparison. 这使用zip来配对相邻的行,并且all有效地进行成对和比较。

Without zip : 没有zip

return all(sum(m[i-1]) < sum(m[i]) for i in range(1, len(m)))

Assuming that you have a function "sum" which returns the sum of given row. 假设你有一个函数“sum”,它返回给定行的总和。 You can use a temp variable to keep the sum of your current row and use it for verification. 您可以使用临时变量来保留当前行的总和并将其用于验证。 For exmaple: 例如:

def increasing (m):
    prevRow = 0
    currentRow = 0
    for row in m:
        currentRow = sum(row)
        if (currentRow <= prevRow):
           return False
        prevRow= currentRow
    else:
        return True

It is enough to store the latest sum in order to get an answer: 存储最新的金额就足以得到答案:

def increasing(m):
    last_sum = -float('inf')
    for row_sum in map(sum, m):
        if row_sum < last_sum:
            return False
        last_sum = row_sum

    return True

You could first create a list of the sums of the rows , and then check whether that list is increasing. 您可以先创建rows sumslist ,然后检查该list是否正在增加。

def increasing(m):
    sums = [sum(r) for r in m]
    return all(sums[i] < sums[i+1] for i in range(len(m)-1))

and we can test this with: 我们可以用以下方法测试:

m1  = [[1, 3, 2, 5],
       [7, 9, 4, 1],
       [3, 5, 6, 9]]

m2  = [[2, 8, 4, 1],
       [6, 2, 8, 5],
       [8, 4, 2, 1]]

which produces the correct results: 这会产生正确的结果:

>>> increasing(m1)
True
>>> increasing(m2)
False

just simply do 只是做

row[0]+row[1]+row[2]+row[3]

for summation process and the problem of not knowing line numbers is handled by iterating on rows, which you will not have any problem 求和过程和不知道行号的问题是通过迭代行来处理的,你不会有任何问题

from sklearn import preprocessing 来自sklearn导入预处理

import csv, sys import csv,sys

with open("m.txt") as f: 打开(“m.txt”)为f:

reader = csv.reader(f)
next(reader) # skip header
data = [r for r in reader]
data.pop(0)
print(type(data))
a=np.asarray(data)
print(np.nanvar(a,ddof=1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM