【问题标题】:Create a dictionary in a dictionary and find the max value in python 3.x在字典中创建字典并在python 3.x中找到最大值
【发布时间】:2017-06-18 00:11:53
【问题描述】:

csv 文件:csv file

我有一个包含州名、作物类型和不同值的 csv 数据文件。我想在字典中创建一个字典,使输出看起来像

{'Corn': {'Illinois': ['93']}}
{'Soybeans': {'Illinois': ['94']}}

其中{'作物类型':{'state':['max_value']}}。

这是我当前的代码:

STATES = ['Alaska', 'Alabama', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming']

def open_file():
    fp = open('alltablesGEcrops.csv', 'r')
    return fp

def read_file(fp):
    fp.readline()
    dict1 = {}
    dict2 = {}
    for line in fp:
        line_lst = line.strip().split(',')
        state = line_lst[0]
        crop = line_lst[1]
        variety = line_lst[3]
        year = int(line_lst[4])
        value = line_lst[6]
        if variety == 'All GE varieties' and state == 'Illinois':
            max_value = max(value, key=int)
            dict1.setdefault(state,[]).append(max_value)
            dict2 = {crop:dict1}
            print(dict2)

def main():
    fp = open_file()
    data = read_file(fp)
    print(data)

if __name__ == "__main__":
    main()

它的输出如下所示: code output

我想知道如何修复我的代码,以便我只能打印出每种作物类型的最后一行?另外,当我找到最大值时,它总是打印出来

{'Soybeans': {'Illinois': ['7', '6', '2', '8', '3', '6', '5', '7', ...]}}

而不是

{'Soybeans': {'Illinois': ['94']}}

我该如何解决这个问题?

【问题讨论】:

  • 在您的代码中没有看到您正在寻找最大值的位置,还有'alltablesGEcrops.csv' 的样子,请提供示例数据......
  • @DmitryPolonskiy 很抱歉,我刚刚编辑了我的代码。
  • 那是你的问题,你说value 等于某个值,然后检查最大值并将其附加到列表中,而不是检查要附加到的列表的最大值跨度>
  • @DmitryPolonskiy 好的!谢谢!

标签: python dictionary


【解决方案1】:

你可以在没有 Pandas 的情况下做到这一点,但你为什么要这样做?

import pandas as pd

# load dataframe
df = pd.read_csv('alltablesGEcrops.csv', na_values={"Value": ("*", ".")})

# produce results
print(df.groupby(['State', 'Crop'])['Value'].max())

给了

State           Crop
Alabama         Upland cotton    98
Arkansas        Soybeans         99
                Upland cotton    99
California      Upland cotton     9
Georgia         Upland cotton    99
Illinois        Corn             93
                Soybeans         94
Indiana         Corn              9
                Soybeans         96
Iowa            Corn             95
                Soybeans         97
Kansas          Corn             95
                Soybeans         96
Louisiana       Upland cotton    99
Michigan        Corn             93
                Soybeans         95
Minnesota       Corn             93
                Soybeans         96
Mississippi     Soybeans         99
                Upland cotton    99
Missouri        Corn             93
                Soybeans         94
Missouri 2/     Upland cotton    99
Nebraska        Corn             96
                Soybeans         97
North Carolina  Upland cotton    98
North Dakota    Soybeans         98
North Dakota    Corn             97
Ohio            Corn              9
                Soybeans         91
Other States    Corn             91
                Soybeans         94
                Upland cotton    98
South Dakota    Corn             98
                Soybeans         98
Tennessee       Upland cotton    99
Texas           Upland cotton    93
Texas           Corn             91
U.S.            Corn             93
                Soybeans         94
                Upland cotton    96
Wisconsin       Corn             92
                Soybeans         95
Name: Value, dtype: object

【讨论】:

    【解决方案2】:

    你可以用字典试试这个:

     from collections import defaultdict
    
    f = open('alltablesGEcrops.csv').readlines()
    
    f = [i.strip('\n').split(',') for i in f]
    
    d = defaultdict(dict)
    
    
    for i in f[1:]:
        if i[0] in d[i[1]].keys():
    
            if i[-1] > max(d[i[1]][i[0]]):
    
                d[i[1]][i[0]] = [i[-1]]
    
         else:
             d[i[1]][i[0]] = [i[-1]]
    
    print dict(d)
    

    【讨论】:

      猜你喜欢
      • 2020-05-03
      • 2019-08-07
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多