基于python中的单个列表排序多个列表

Zac*_*ach 11 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):
        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)
Run Code Online (Sandbox Code Playgroud)

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

因此,如果:

filename = ['f3','f1','f2']
human_rating = ['1','2','3']
etc.
Run Code Online (Sandbox Code Playgroud)

然后排序将返回:

filename = ['f1','f2','f3']
human_rating = ['2','3','1']
etc.
Run Code Online (Sandbox Code Playgroud)

Dav*_*ver 13

我会拉链然后排序:

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

如果你真的想要获得个人列表,我会按照上面的方式对它们进行排序,然后解压缩它们:

filename, human_rating, … = zip(*zipped)
Run Code Online (Sandbox Code Playgroud)

  • Python 3 注意: zip 在 Python 3 中返回一个迭代器,使用 list 查看其内容,`zipped = list(zip(filename, human_rating, ...))` (4认同)

ori*_*rip 8

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

l = zip(filename, human_rating, ...)
l.sort()
# 'unzip'
filename, human_rating ... = zip(*l)
Run Code Online (Sandbox Code Playgroud)

或者在一行中:

filename, human_rating, ... = zip(*sorted(zip(filename, human_rating, ...)))
Run Code Online (Sandbox Code Playgroud)

示例运行:

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)
Run Code Online (Sandbox Code Playgroud)