如何排列三个列表,使对应元素的和(如果大于)则首先出现? [英] How to arrange three lists in such a way that the sum of corresponding elements if greater then appear first?

查看:69
本文介绍了如何排列三个列表,使对应元素的和(如果大于)则首先出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手.我有三个长度相同的花车清单.这些数字随机分布在所有列表中.但是一个列表中元素的位置与其他列表中相同位置的其他元素相对应.假设列表是

I am new in python. I have three lists of floats of same length. The numbers are randomly distributed in all lists. But the positions of elements in one list corresponds to the other elements of the same positions in other lists. Let's say the lists are,

a=[1,5,3,2,4] b=[20,30,50,40,10] c=[400,500,100,300,200]

a=[1,5,3,2,4] b=[20,30,50,40,10] c=[400,500,100,300,200]

现在,这三个列表中相同位置的元素彼此对应.像1、20和400彼此对应; 3、50和100相互对应,依此类推. 我必须以这样的方式排列数字:对于这三个列表中的任何位置i,如果相应数字的总和更大,则这三个数字应首先出现在列表中.喜欢, e=[500, 400, 300, 200, 100] f=[30,20,40,10,50] g=[5,1,2,4,3]

Now, elements of same positions in these three lists correspond each other. Like 1, 20 and 400 correspond each other; 3, 50 and 100 correspond each other and so on. I have to arrange the numbers in such a way that for any position i of these three lists if the sum of the corresponding numbers are greater then those three numbers should appear first in the lists. Like, e=[500, 400, 300, 200, 100] f=[30,20,40,10,50] g=[5,1,2,4,3]

因此,相应数字的位置已完全更改.否则他们的信件一定不能中断.作为一个初学者,我已经尝试了许多方法,但都徒劳无功.请帮助我.

So the positions of corresponding numbers have changed altogether. Or their correspondence MUST NOT BREAK. As a beginner I have tried in many ways but all in vain. Please help me.

推荐答案

作为原始问题的答案(预编辑)

As an answer to the original question (pre edit)

尝试一下.

a=[1,5,3,2,4]
b=[20,30,50,40,10]
c=[400,500,100,300,200]

x = sorted(zip(a,b,c), key=lambda x: sum(x), reverse=True)

e, f, g = map(list, zip(*x))

print e
print f
print g

输出

[5, 1, 2, 4, 3]
[30, 20, 40, 10, 50]
[500, 400, 300, 200, 100]

使用zip创建一个tuples列表,然后使用它们的总和作为键对它们进行排序,然后反向进行排列,以便最大的总和首先出现.如果您使用的是python 2.7,则可以使用izip而不是zip来减轻大型列表的内存负担,但是仍然需要创建完整的排序列表.在python 3中,zip等同于2.7的izip.

Use zip to create a list of tuples, then sort them using their sum as the key, reversed so the largest sum is first. If you are using python 2.7 you could use izip instead of zip to somewhat reduce the memory burden with large lists but the full sorted list still needs to be created. In python 3 zip is equivalent of 2.7's izip.

sorted函数将为您返回元组列表,因此您现在想将它们返回为原始的3列表格式.最快,最干净的方法(由@JohnClements提供).

The sorted function will return you list of tuples, so you want to now turn them back into your original 3 list format. The fastest and cleanest way to do this (courtesy of @JohnClements).

e, f, g = map(list, zip(*x))

为解释这一说法,请记住x是每个长度为3的tuples的列表.

To explain this statement, remembering that x is a list of tuples each of length 3.

  • 第一遍从x中解压缩后的tuples(使用表达式*x).这将导致一个包含3 tuples的新列表.第一个tuple包含x中元组的所有第一个元素,第二个tuple包含x中元组的所有第二个元素,依此类推.
  • 现在您有了tuples的列表,但是我们希望我们的迭代器包含lists,因此map是用于执行转换的正确函数.它以本机C代码执行转换的速度很快.结果是listlist s.
  • 最后,我们可以将listslist解压缩为3个单独的列表变量efg.
  • First pass zip the unpacked tuples from x (using expression *x). This results in a new list containing 3 tuples. The first tuple contains all the first elements from the tuples in x, the second tuple contains all the second elements from x and so on.
  • Now you have a list of tuples, but we want our iterable to contain lists, so map is the correct function to use to do the conversion. It will be fast as its performing the conversion in native c code. The result is a list of lists.
  • finally we can unpack the list of lists to 3 separate list variables e, f and g.

如果您需要对代码有更详细的了解,建议您在中间阶段打印输出.

If you require more detailed understanding of the code I suggest you print the output at intermediate stages.

这篇关于如何排列三个列表,使对应元素的和(如果大于)则首先出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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