基于一个列表对x列表进行排序 [英] Sorting x lists based on one list

查看:86
本文介绍了基于一个列表对x列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在寻找一个简单的算法 - 也许Python可以提供帮助:


我从X列表开始初始排序基于列表#1。


我想反向排序列表#1并相应地排序所有其他列表。


任意欢迎这个想法。


问候,


Philippe

Hi,

I''m looking for an easy algorithm - maybe Python can help:

I start with X lists which intial sort is based on list #1.

I want to reverse sort list #1 and have all other lists sorted accordingly.

Any idea is welcome.

Regards,

Philippe

推荐答案

l1 = ['''',''b'',''c'']

l2 = [''toto'',''titi'', ''tata'']#''toto''指的是''a'',''titi''到b''....

l3 = [''foo'', ''bar'',''doe'']#''foo''指的是''a''........


我要反向排序l1并且相应地遵循l2和l3。


问候,


Philippe



Philippe C. Martin写道:
l1 = [''a'',''b'',''c'']
l2 = [''toto'',''titi'',''tata''] # ''toto'' refers to ''a'', ''titi'' to b'' ....
l3 = [''foo'',''bar'',''doe''] # ''foo'' refers to ''a'' ........

I want to reverse sort l1 and have l2 and l3 follow accordingly.

Regards,

Philippe


Philippe C. Martin wrote:


我正在寻找一个y算法 - 也许Python可以提供帮助:

我从X列表开始,其中的初始排序基于列表#1。

我想反向排序列表#1并拥有所有其他清单相应排序


欢迎任何想法。

问候,

Philippe
Hi,

I''m looking for an easy algorithm - maybe Python can help:

I start with X lists which intial sort is based on list #1.

I want to reverse sort list #1 and have all other lists sorted
accordingly.

Any idea is welcome.

Regards,

Philippe






> Philippe C. Martin写道:
> Philippe C. Martin wrote:
我正在寻找一个简单的算法 - 也许Python可以提供帮助:
我从X列表开始,其中的初始排序是基于列表#1。
我想反向排序列表#1并相应地对所有其他列表进行排序。


一种方法,使用带索引的帮助列表:
I''m looking for an easy algorithm - maybe Python can help:
I start with X lists which intial sort is based on list #1.
I want to reverse sort list #1 and have all other lists sorted
accordingly.
One way, using a helper list with indices:
l1 = ['''',''b'',' 'c'']
l2 = [''toto'',''titi'',''tata'']#''toto''指的是''a'',''titi''到' b''....
l3 = [''foo'',''bar'',''doe'']#''foo''指的是''a''..... ...
indices = sorted(range(len(l1)),key = l1 .__ getitem __,reverse = True)
对于l1,l2,l3中的项目:
.... items [:] = [items [i] for i in indices]

.... l1
[''c'',''b'',''a'' ] l2
[''tata'',''titi'',''toto''] l3
l1 = [''a'',''b'',''c'']
l2 = [''toto'',''titi'',''tata''] # ''toto'' refers to ''a'', ''titi'' to b'' ....
l3 = [''foo'',''bar'',''doe''] # ''foo'' refers to ''a'' ........
indices = sorted(range(len(l1)), key=l1.__getitem__, reverse=True)
for items in l1, l2, l3: .... items[:] = [items[i] for i in indices]
.... l1 [''c'', ''b'', ''a''] l2 [''tata'', ''titi'', ''toto''] l3



[''doe '',''bar'',''foo'']


另一种方法是将三个列表合并为三元组之一,排序,

和unmerge,类似于DSU模式 - 提出了一个问题:为什么

你在使用首先是三个清单?


Peter


[''doe'', ''bar'', ''foo'']

Another way would be to merge the three lists into one of 3-tuples, sort,
and unmerge, similarly to the DSU pattern -- which raises the question: why
are you using three lists in the first place?

Peter


>另一种方法是将三个列表合并为三元组中的一个,排序,
> Another way would be to merge the three lists into one of 3-tuples, sort,
和unmerge,类似于DSU模式 - 这提出了一个问题:
你为什么使用三个列表首先?
and unmerge, similarly to the DSU pattern -- which raises the question:
why are you using three lists in the first place?




:-)谢谢,这些列表将会发展并以''csv''格式存储在
$ b $中b一次是外部文件。我不能使用词典,因为我需要

控制排序(哈希)。


在这个特定情况下,列表1代表学生的信息,

列表2表示具有重量,期限,最高等等信息的作业
等级......以及列表3实际等级。


此致,


Philippe


Peter Otten写道:



:-) Thanks, the lists will evolve and are also stored in ''csv'' format in
external files at one point. I cannot use dictionaries because I need to
control the sorting (hash).

In this specific case, list 1 represents students with their information,
list 2 represents assignments with information such as weight, term, max
grade ... and list 3 the actual grades.

Regards,

Philippe

Peter Otten wrote:

Philippe C. Martin写道:
Philippe C. Martin wrote:
我正在寻找一个简单的算法 - 也许Python可以提供帮助:
我从X列表开始,其中的初始排序基于列表#1。
我想反向排序列表#1并相应地对所有其他列表进行排序。
一种方法,使用带索引的帮助列表:
l1 = ['' a'',''b'',''c'']
l2 = [''toto'',''titi'',''tata'']#''toto''指的是' 'a'',''titi''到b''.... l3 = [''foo'',''bar'',''doe'']#''foo''指的是''a''........
指数= sorted(range(len(l1)),key = l1 .__ getitem __,reverse = True)
对于l1,l2,l3中的项目:... items [:] = [items [i] for i in指数]
... l1 [''c'',''b'',''a''] l2 [''tata'',''titi'',''toto''] l3
I''m looking for an easy algorithm - maybe Python can help:
I start with X lists which intial sort is based on list #1.
I want to reverse sort list #1 and have all other lists sorted
accordingly.
One way, using a helper list with indices:
l1 = [''a'',''b'',''c'']
l2 = [''toto'',''titi'',''tata''] # ''toto'' refers to ''a'', ''titi'' to b'' ....
l3 = [''foo'',''bar'',''doe''] # ''foo'' refers to ''a'' ........
indices = sorted(range(len(l1)), key=l1.__getitem__, reverse=True)
for items in l1, l2, l3: ... items[:] = [items[i] for i in indices]
... l1 [''c'', ''b'', ''a''] l2 [''tata'', ''titi'', ''toto''] l3


[''doe'',''bar'',''foo'']

另一种方法是将三个列表合并为一个类似于DSU模式的3元组,排序,
和unmerge - 提出了一个问题:
为什么你首先使用三个列表?

Peter


[''doe'', ''bar'', ''foo'']

Another way would be to merge the three lists into one of 3-tuples, sort,
and unmerge, similarly to the DSU pattern -- which raises the question:
why are you using three lists in the first place?

Peter






这篇关于基于一个列表对x列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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