根据python中的单个列表对多个列表进行排序 [英] sorting multiple lists based on a single list in python

查看:916
本文介绍了根据python中的单个列表对多个列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在打印一些列表,但这些值未排序.

I'm printing a few lists but the values are not sorted.

for f, h, u, ue, b, be, p, pe, m, me in zip(filename, human_rating, rating_unigram, percentage_error_unigram, rating_bigram, percentage_error_bigram, rating_pos, percentage_error_pos, machine_rating, percentage_error_machine_rating):
        print "{:>6s}{:>5.1f}{:>7.2f}{:>8.2f} {:>7.2f} {:>7.2f}  {:>7.2f} {:>8.2f}  {:>7.2f} {:>8.2f}".format(f,h,u,ue,b,be,p,pe,m,me)

根据文件名"中的值对所有这些列表进行排序的最佳方法是什么?

What's the best way to sort all of these lists based on the values in 'filename'?

所以,如果:

filename = ['f3','f1','f2']
human_rating = ['1','2','3']
etc.

然后排序将返回:

filename = ['f1','f2','f3']
human_rating = ['2','3','1']
etc.

推荐答案

我先压缩然后排序:

zipped = zip(filename, human_rating, …)
zipped.sort()
for row in zipped:
     print "{:>6s}{:>5.1f}…".format(*row)

如果您真的想找回单个列表,我将按照上面的顺序对它们进行排序,然后将其解压缩:

If you really want to get the individual lists back, I would sort them as above, then unzip them:

filename, human_rating, … = zip(*zipped)

这篇关于根据python中的单个列表对多个列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆