如何从Python中的行和列中查找最小值/最大值? [英] How to find min/max values from rows and columns in Python?

查看:1078
本文介绍了如何从Python中的行和列中查找最小值/最大值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何从基本上是文本文件的数据集中找到最小值和最大值.它有50行50列.

I was wondering how can I find minimum and maximum values from a dataset, which is basically a text file. It has 50 rows, 50 columns.

我知道我可以设置一个控制循环(特定于循环)以使其读取每一行和每一列,并确定最小/最大值.但是,我不确定该怎么做.

I know I can set up a control loop (for loop to be specific) to have it read each row and column, and determine the min/max values. But, I'm not sure how to do that.

我认为必须先将行和列转换为列表,然后再使用split()函数.我尝试按以下步骤进行设置,但似乎不起作用:

I think the rows and columns need to be converted to list first and then I need to use the split() function. I tried setting something up as follows, but it doesn't seem to work:

for x in range(4,50): # using that range as an example
    x.split()
    max(4,50)
    print x

Python的新手.请原谅我的错误.

New to Python. Please excuse my mistakes.

推荐答案

尝试如下操作:

data = []
with open('data.txt') as f:
    for line in f:                   # loop over the rows
        fields = line.split()        # parse the columns
        rowdata = map(float, fields) # convert text to numbers
        data.extend(rowdata)         # accumulate the results
print 'Minimum:', min(data)
print 'Maximum:', max(data)

请注意,如果您想分割除空白以外的内容(例如逗号), split()将采用可选参数.

Note that split() takes an optional argument if you want to split on something other than whitespace (commas for example).

这篇关于如何从Python中的行和列中查找最小值/最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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