簡體   English   中英

如何在Python 2.7中從csv讀取的數據中找到最大數量?

[英]How to find maximum number from csv-read data in Python 2.7?

有一個名為vic_visitors.csv的CSV文件,其中包含以下數據:

Victoria's Regions,2004,2005,2006,2007
Gippsland,63354,47083,51517,54872
Goldfields,42625,36358,30358,36486
Grampians,64092,41773,29102,38058
Great Ocean Road,185456,153925,150268,167458
Melbourne,1236417,1263118,1357800,1377291

還有一個問題問:

問:編寫程序以從CSV數據vic_visitors.csv中查找維多利亞州的最大訪客人數。 您的程序應將結果打印為“在'z'年中,'y'中最大的訪客人數是'x'。

直到這里我都可以訪問數據,以便data_2d可以二維地向我提供信息,其中data_2d[i]=rowdata_2d[i][j]=column

import csv
visitors=open("vic_visitors.csv")
data=csv.reader(visitors)
data_2d=list(data)

但是在如何獲取最大人數及其對應的地區和年份方面,我迷失了。

您有4個問題需要解決:

  • 您需要保留列標題,以便可以正確報告年份
  • csv以字符串形式提供所有內容,而您需要以數字方式比較這些值
  • 您需要找到每一行的最大值。
  • 您需要根據給定行的最大值找到最大行。

您可以使用DictReader()解決第一部分。 您可以在讀取文件時將值轉換為整數,或者在確定最大值時將其轉換。 您可以一口氣確定閱讀時或最后一步時每行的最大值。

在讀取時,我會做盡可能多的事情,丟棄過程中不需要的任何數據:

import csv

maximum_value = None
with open("vic_visitors.csv", 'rb') as visitors:
    reader = csv.DictReader(visitors)
    for row in reader:
        count, year = max((int(row[year]), year) for year in reader.fieldnames[1:])  # skip the first column
        if not maximum_value or count > maximum_value[0]:
            maximum_value = (count, row[reader.fieldnames[0]], year)

print "The greatest visitornumber was {} in {} in the year {}.".format(
    *maximum_value)

max(...)行在每個行字典(使用CSV的第一行作為鍵)中的鍵值對上循環,選擇年份列(因此除第一列外的所有字段)。 通過將數值放在第一位,您可以獲取該行的最大列值以及年份。

然后,我們存儲到目前為止找到的最大行信息(僅是計數,區域和年份); 無需保留任何其他行。 然后,通過將這3個值插入模板來格式化該元組。

通過使用DictReader.fieldnames列表,我們可以保持這種靈活性。 只要第一列是一個區域,其余的是年份,則代碼將適應任何更改。

演示:

>>> import csv
>>> sample = '''\
... Victoria's Regions,2004,2005,2006,2007
... Gippsland,63354,47083,51517,54872
... Goldfields,42625,36358,30358,36486
... Grampians,64092,41773,29102,38058
... Great Ocean Road,185456,153925,150268,167458
... Melbourne,1236417,1263118,1357800,1377291
... '''.splitlines(True)
>>> maximum_value = None
>>> reader = csv.DictReader(sample)
>>> for row in reader:
...     count, year = max((int(row[year]), year) for year in reader.fieldnames[1:])  # skip the first column
...     if not maximum_value or count > maximum_value[0]:
...         maximum_value = (count, row[reader.fieldnames[0]], year)
... 
>>> print "The greatest visitornumber was {} in {} in the year {}.".format(
...     *maximum_value)
The greatest visitornumber was 1377291 in Melbourne in the year 2007.

您可以使用以下方法掃描每個條目,並在每次條目超過當前最大值時分配max和max參數。

import csv
with open('vic_visitors.csv') as f:
    reader = csv.DictReader(f)
    max = 0     
    for row in reader:
        if(float(row['2004'])>max):
            max = float(row['2004'])
            maxyear = '2004'
            maxloc = row["Victoria's Regions"]
        if(float(row['2005'])>max):
            max = float(row['2005'])
            maxyear = '2005'
            maxloc = row["Victoria's Regions"]
        if(float(row['2006'])>max):
            max = float(row['2006'])
            maxyear = '2006'
            maxloc = row["Victoria's Regions"]  
        if(float(row['2007'])>max):
            max = float(row['2007'])
            maxyear = '2007'
            maxloc = row["Victoria's Regions"]          

print("The greatest visitornumber was "+ str(max) +" in " +maxloc+ " in  the year "+maxyear)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM