python中有多个字典,然后取最大值

Python020

python中有多个字典,然后取最大值,第1张

d1 = {'ser':'0001','name':'Tom','sex':'m','score':'76'}

d2 = {'ser':'0002','name':'Jak','sex':'m','score':'87'}

d3 = {'ser':'0003','name':'Alic','sex':'f','score':'86'}

max_score = float('-inf')

min_score = float('inf')

max_student = None

min_student = None

for d in [d1, d2,d3]:

    score = int(d['score'])

    if score > max_score:

        max_score = score

        max_student = d

    if score < min_score:

        min_score = score

        min_student = d        

print('min score student info',min_student)

print('max score student info',max_student)

应该能够满足你的需求

Python编程将多个字典文件合并成一个字典文件,代码如下:

//例子:合并a.txt和b.txt两个字典文件

def readf(filename):

    lines = file(filename).readlines()

    dic = {}

    for i in lines:

        i_ = i.split()

        dic[i_[0]] = int(i_[1])

    return dic

 

dica = readf('a.txt')

dicb = readf('b.txt')

 

lines = []

for i in dica:

    percent = str(float(dicb[i])*100/dica[i])+'%'

    s = ' '.join([i, str(dica[i]), str(dicb[i]), percent])

    s += '\n'

    lines.append(s)

//合并成一个字典文件c.txt 

with open('c.txt', 'w') as f:

    f.writelines(lines)

    f.close()