在下面的代码中(用于按降序打印工资,按专业排序),
reader = csv.DictReader(open('salaries.csv','rb'))
rows = sorted(reader)
a={}
for i in xrange(len(rows)):
if rows[i].values()[2]=='Plumbers':
a[rows[i].values()[1]]=rows[i].values()[0]
t = [i for i in sorted(a, key=lambda key:a[key], reverse=True)]
p=a.values()
p.sort()
p.reverse()
for i in xrange(len(a)):
print t[i]+","+p[i]
当我在条件语句中输入“管道工”时,管道工工资中的输出结果为:
Tokyo,400
Delhi,300
London,100
当我将“律师”置于相同的“如果”条件下时,输出为:
Tokyo,800
London,700
Delhi,400
CSV 的内容如下:
City,Job,Salary
Delhi,Lawyers,400
Delhi,Plumbers,300
London,Lawyers,700
London,Plumbers,100
Tokyo,Lawyers,800
Tokyo,Plumbers,400
当我从程序中删除 --> if rows[i].values()[2]=='Plumbers': <-- 时,它应该打印所有输出,但它只打印这 3 个:
Tokyo,400
Delhi,300
London,100
虽然输出应该类似于:
Tokyo,800
London,700
Delhi,400
Tokyo,400
Delhi,300
London,100
问题究竟出在哪里?