Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中基于单个列表对多个列表进行排序_Python_List - Fatal编程技术网

在python中基于单个列表对多个列表进行排序

在python中基于单个列表对多个列表进行排序,python,list,Python,List,我正在打印一些列表,但这些值没有排序 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

我正在打印一些列表,但这些值没有排序

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)
根据“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)
如果您真的想取回单个列表,我会按上面的方式对它们进行排序,然后解压缩它们:

filename, human_rating, … = zip(*zipped)

zip
返回元组列表,您可以根据元组的第一个值对其进行排序。因此:

for ... in sorted(zip( ... )):
    print " ... "

这个怎么样:
zip
放入一个元组列表,对元组列表进行排序,然后“解压”

或者在一行中:

filename, human_rating, ... = zip(*sorted(zip(filename, human_rating, ...)))
样本运行:

foo = ["c", "b", "a"]
bar = [1, 2, 3]
foo, bar = zip(*sorted(zip(foo, bar)))
print foo, "|", bar # prints ('a', 'b', 'c') | (3, 2, 1)

Python3注意:在Python3中,zip返回一个迭代器,使用list查看其内容,
zip=list(zip(文件名,人名,…)
@David Wolever非常好的解决方案。
foo = ["c", "b", "a"]
bar = [1, 2, 3]
foo, bar = zip(*sorted(zip(foo, bar)))
print foo, "|", bar # prints ('a', 'b', 'c') | (3, 2, 1)