Python用相应的字符串排列数字

时间:2014-07-15 09:16:36

标签: python-2.7

我想用相应的字符串按降序对浮点数进行排序。例如,

Mass=[10,45.5,56.7,34.7,12,8.2,56,78.5,5.5,21.5]
Name=['A1','A2','A3','A4','A5','A6','A7','A8','A9','A10']

现在我想按降序排列质量,以便它能给我

A8=78.5, A3=56.7, A7=56.....

我怎么能在python中做到这一点?

非常感谢

干杯,

-Viral

2 个答案:

答案 0 :(得分:1)

你可以zip那些,把质量放在首位,然后sort它们按相反的顺序排列。

for m, n in sorted(zip(Mass, Name), reverse=True):
    print n, m

输出:

A8 78.5
A3 56.7
A7 56
...

答案 1 :(得分:0)

此代码完全返回您想要的内容。

Mass=[10,45.5,56.7,34.7,12,8.2,56,78.5,5.5,21.5]
Name=['A1','A2','A3','A4','A5','A6','A7','A8','A9','A10']

for m,n in sorted(zip(Mass, Name), key=lambda pair: pair[0], reverse=True):
  print "%s=%s," % (n, m),
  

A8 = 78.5,A3 = 56.7,A7 = 56,A2 = 45.5,A4 = 34.7,A10 = 21.5,A5 = 12,A1 = 10,   A6 = 8.2,A9 = 5.5,

如果你不需要结肠,你可以使用加入。