Python:如何比较列表中的值?

我需要将一个csv转换为一个列表,并计算一行中'z'的数量,并打印出Z的最大数量的5行的行标题。另外,链接到更多帮助将非常棒!谢谢你

这是我当前的代码

economistData = open('C:/foo.csv','r')
economistDataList = []s
for line in economistData:
    economistDataList.append(line.split(','))


for row in economistDataList:
    rowcnt = row.count('z')
jrd1

由于您提到必须不使用csv模块才能执行此操作

z_counts = []
lines = []

with open('C://foo.csv', 'r') as f:
    lines = f.readlines() #This is your list of all the rows/lines (now populated with data)
    for index, line in enumerate(lines):
        #store the z counts and the index of the line as pairs using a tuple: `( ... )`
        z_counts.append((line.strip().split('z').count('z'), index))

    #Since you want the top 5 matches
    #Reverse is necessary since Python automatically sorts in ascending order
    print sorted(z_counts, reverse=True)[:5]

使用以下示例数据:

a,b,c,d,e,f,g,h,j,i,p
a,e,f,g,h,d,e,g,z,g,z
z,z,z,c,x,s,e,f,d,s,f
q,e,r,s,f,t,y,y,u,i,f
e,p,l,l,k,k,z,z,q,e,r
o,i,i,j,l,s,w,e,r,q,g
w,e,r,f,g,s,v,h,d,b,z
t,r,y,e,u,i,o,p,d,f,j

如果您观察到,则具有以下z计数:

0
2
3
0
2
0
1
0

我们获得以下输出(具有已排序的对应z计数以及该z计数的​​索引的元组列表):

[(3, 2), (2, 4), (2, 1), (1, 6), (0, 7)]

输出:

让我们打印出来看看它是什么样的:

for tpl in sorted(z_counts, reverse=True)[:5]:
    print lines[tpl[1]]

哪个输出:

z,z,z,c,x,s,e,f,d,s,f     #3 z's, line index 2 (3rd line)
e,p,l,l,k,k,z,z,q,e,r     #2 z's, line index 4 (5th line)
a,e,f,g,h,d,e,g,z,g,z     #2 z's, line index 1 (2nd line)
w,e,r,f,g,s,v,h,d,b,z     #1 z, line index 6 (7th line)
t,r,y,e,u,i,o,p,d,f,j     #0 z's, line index 7 (8th line)

如预期的那样。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章