在文本fi中查找与特定键对应的最大/最小值

2024-10-04 01:22:29 发布

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

我试图在文本文件中找到与某些类别相对应的值的最大值和最小值。我的数据文件可以按列和行建模,如下所示:

catA 50 80。。。在

有线电视40-90。。。在

catC 90 100。。。在

中航20 30。。。在

我想找出第三列和第二列之间的最大和最小差值。在

我有密码:

mydict = {"catA":0, "catB":0, "catC":0} #...Dictionary is defined

for line in mydocument: 
    variable = line.split("\t")

    intvalue = int(variable[1])
    intvalue2= int(variable[2])
    intvalue_new = intvalue2-intvalue

    if variable[0] in mydict and mydict[variable[0]] < intvalue_new:
        mydict[variable[0]] = intvalue_new

print mydict

“如果不是所有的代码都是0,那么我的字段就没有填充了”。在

我做错什么了?在

谢谢你!在


Tags: innew数据文件line建模类别variablemydict