如何对一个列表进行更改会影响另一个不同的列表? [英] How can I have changes made to one list affect another, different list?

查看:66
本文介绍了如何对一个列表进行更改会影响另一个不同的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有两个列表,其中包含:

So, I have two lists which contain:

[10, 10, 2, 9, 10]
['Jem', 'Sam', 'Sam', 'Jem', 'Jem']

我想使用.sort()函数对第二个列表进行排序,并且我希望对第二个列表进行的更改会影响第一个列表,即

I want to sort the second list using the .sort() function, and I want the changes made to the second list to affect the first list, i.e.

['Jem', 'Jem', 'Jem', 'Sam', 'Sam']
[10, 9, 10, 10, 2]

如果不清楚,则将其视为链接的两个列表.两个列表([0],[1]等)中每个点的字符串/整数都连接在一起,因此,一个列表中元素位置的每次更改都由列表中位置的相等更改反映出来.另一个列表中的对应元素.我将如何实现呢?

If that wasn't clear, then think of it as the two lists being linked. The strings/integers at each point in the two lists ([0], [1] etc.) are joined together, so that every change to the position of an element of one list is reflected by an equal change in the position of the corresponding element in the other list. How would I achieve this?

推荐答案

只需将它们zip在一起并按名称排序:

Just zip them together and sort by name:

name = ['Jem', 'Sam', 'Sam', 'Jem', 'Jem']
nums = [10, 10, 2, 9, 10]

sort = sorted(zip(name, nums), key = lambda i:i[0])

name = [i[0] for i in sort] # split name again
nums = [i[1] for i in sort] # split nums again

这篇关于如何对一个列表进行更改会影响另一个不同的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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