跳过2D列表中的第一行并使用值

2024-09-29 17:23:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个项目上工作,我有两个csv文件,我已经转换成二维列表。我有一个列表1,在某个区域内有一定数量的钻孔,还有一个列表2,它有相应的多个值来计算平均值、最小值和最大值。我有问题,因为我认为功能是:

  1. 不跳过列表中值的标题,因此不获取整数

  2. 可能连比较这两个列表都没有,因为我一直被困在步骤1中

到目前为止,我得到的是:

def height(bReader, dataList):
    print "Height function ran, but no result"
    # bReader is bores inside polygon
    # dataList BORE_DATA.csv as 2D list
    avgHeight = 0
    minN = 9e99
    maxPh = 0
    numCount = 0
    for row in bReader:
        for row2 in dataList:
            if row[0] == row2[0]:
                if row2[1] == '':
                    if row2[1][0] == '<':
                        row2[1] = row2[1][:1]
                avgHeight += row2[4]
                numCount += 1

                print row2[4]
                # Checks the minimum values of Nitrogen

                if row2[6] < minN:
                    minN = row2[6]

                 # Checks the maximum values of Phosphorous

                if row2[5] < maxPh:
                    maxPh = row2[5]


                if lastID != row2[0]:
                   # Checks for final value before moving to next bore
                    aveHeight = avgHeight / numCount
                    print row[0]
                    print "Average Water Level" + aveHeight
                    print "Minimum Nitrogen" + minN
                    print "Maximum Phosphorous" + maxPh
                    avgHeight = 0
                    minN = 9e99
                    maxPh = 0
                    numCount = 0
                lastID = row[0]

这是回溯错误:

Traceback (most recent call last):
  File "C:\Users\Lounge\Desktop\V2\ReRun V2.py", line 39, in main
    height(levelsList, bdataList)
  File "C:\Users\Lounge\Desktop\V2\ReRun V2.py", line 263, in height
    for row in bReader:
TypeError: 'function' object is not iterable

样本数据:

列表1数据

REFERENCE   EASTING NORTHING    TOC ELEVATION (m)
61610628    384835  6489341         20.24

列表2数据

BORE REF NR BORE NAME   SAMPLE DATE    water level (m)  TDSolids (mg/L) pH  N (mg/L)    P (mg/L)
61610628    JP20B   23/06/2000              3.71        430      8.8        0.28        0.007
61610628    JP20B   27/10/2000              3.18        610      7.2        1.3        0.019

Tags: in列表forifv2rowrow2print
2条回答

我对程序做了一些修改使它运行起来。要考虑的要点是:

  • 任何要进行数字比较的值都必须转换为适当的数据类型,因为如果从csv导入,默认情况下它们将是字符串
  • 要获得最大ph值,您需要检查>;不是<
  • 我已经将每个孔的摘要信息移到了内环之外,这样就不需要检查id是否已更改
  • 我还初始化了外循环中的ave、max、min变量,以便为每个孔重置它们
  • 从您提供的数据来看,水位应该是row2[3]而不是row2[4]
  • 我已经删除了检查if row2[1] == '':的逻辑,因为它看起来是多余的。你知道吗

以下是修订后的代码:

def height(bReader, dataList):
    print("\nIn height function...")
    # bReader is bores inside polygon
    # dataList BORE_DATA.csv as 2D list
    for row in bReader:
        # initialise measurements for each bore
        avgHeight = 0
        minN = 9e99
        maxPh = 0
        numCount = 0

        for row2 in dataList:
            if row[0] == row2[0]:
                avgHeight += float(row2[3])
                numCount += 1

                print("Water level: {0}".format(row2[3])) # water level

                # Checks the minimum values of Nitrogen
                if float(row2[6]) < minN:
                    minN = float(row2[6])

                 # Checks the maximum values of Phosphorous
                if float(row2[5]) > maxPh:
                    maxPh = float(row2[5])

       # Checks for final value before moving to next bore
        aveHeight = avgHeight / numCount
        print row[0]
        print("Average Water Level: {0}".format(aveHeight))
        print("Minimum Nitrogen: {0}".format(minN))
        print("Maximum Phosphorous {0}".format(maxPh))
        avgHeight = 0
        minN = 9e99
        maxPh = 0
        numCount = 0


# added below for test
import csv

# create file objects and readers
f1, f2 = open('list1.csv', 'rb'), open('list2.csv', 'rb')
r1 = csv.reader(f1, delimiter=',', quotechar='"')
r2 = csv.reader(f2, delimiter=',', quotechar='"')

# create lists from readers
l1 = [[col for col in row] for row in r1]
l2 = [[col for col in row] for row in r2]

# check first couple of rows from lists
print('\nSample data from list 1\n' + '-'*23) 
for row in  l1[:2]: print(row)
print('\nSample data from list 2\n' + '-'*23) 
for row in l2[:3]: print(row)

# call function without headings from first rows
height(l1[1:], l2[1:])

产生以下输出:

Sample data from list 1
           -
['REFERENCE', ' EASTING NORTHING', ' TOC ELEVATION (m)']
['61610628', ' 6489341', ' 20.24']

Sample data from list 2
           -
['BORE REF', ' NR BORE NAME', ' SAMPLE DATE', ' water level (m)', ' TDSolids', '(mg/L) pH', ' N (mg/L)', ' P (mg/L)']
['61610628', ' JP20B', ' 23/06/2000', ' 3.71', ' 430', ' 8.8', ' 0.28', ' 0.007']
['61610628', ' JP20B', ' 27/10/2000', ' 3.18', ' 610', ' 7.2', ' 1.3', ' 0.019']

In height function...
Water level:  3.71
Water level:  3.18
61610628
Average Water Level: 3.445
Minimum Nitrogen: 0.28
Maximum Phosphorous 8.8

希望这能为你解决问题。你知道吗

从跟踪来看,您传递给函数的参数似乎不是一个列表,而是一个函数。调用height(...)的代码就是错误所在。你知道吗

很抱歉没有发表评论,我没有这样做的声誉。你知道吗

相关问题 更多 >

    热门问题