按第一个列表的顺序对列表的各个部分进行排序。

huangapple go评论48阅读模式
英文:

Sorting separate lists of lists by the order of the first one

问题

我有三个列表的列表:

days = [[5,2,7],[5,2]]
km = [[2,3,4],[4,3]]
time = [[30,10,20],[15,25]]

我想要根据day中的相应列表对kmtime中的列表进行排序。因此,期望的输出将是:

days = [[2,5,7],[2,5]]
km = [[3,2,4],[3,4]]
time = [[10,30,20],[25,15]]

我可以使用list(map(sorted, days))来对列表进行排序,但是单独为每个列表的列表运行该命令不起作用。
总共有大约10个列表,第一个列表应该“指导”顺序,其余的应该根据它进行排序。如何使用Python实现这一点?

英文:

I have three lists of lists:

days = [[5,2,7],[5,2]]
km = [[2,3,4],[4,3]]
time = [[30,10,20],[15,25]]

I would like sort lists in km and time according to the corresponding list in day. So the expected output would be:

days = [[2,5,7],[2,5]]
km = [[3,2,4],[3,4]]
time = [[10,30,20],[25,15]]

I can sort list of lists using list(map(sorted, days)) but running the command for each list of lists separately will not work.
I have around 10 lists in total, first one should 'dictate' the order, the rest should be sorted according to it. How can I achieve that using Python?

答案1

得分: 1

对于仅有三个元素的情况:

days = [[5,2,7],[5,2]]
km = [[2,3,4],[4,3]]
time = [[30,10,20],[15,25]]

for d, k, t in zip(days, km, time):
    d[:], k[:], t[:] = zip(*sorted(zip(d, k, t)))

对于更多元素的情况:

days = [[5,2,7],[5,2]]
km = [[2,3,4],[4,3]]
time = [[30,10,20],[15,25]]
xsss = days, km, time

for xss in zip(*xsss):
    for xs, xs[:] in zip(xss, zip(*sorted(zip(*xss)))):
        pass

使用 itemgetter 的解决方案:

from operator import itemgetter as ig

days = [[5,2,7],[5,2]]
km = [[2,3,4],[4,3]]
time = [[30,10,20],[15,25]]

for lists in zip(days, km, time):
    ordered = ig(*map(ig(0), sorted(enumerate(lists[0]), key=ig(1))))
    for lst in lists:
        lst[:] = ordered(lst)

Try it online!

英文:

For just three:

days = [[5,2,7],[5,2]]
km = [[2,3,4],[4,3]]
time = [[30,10,20],[15,25]]

for d, k, t in zip(days, km, time):
    d[:], k[:], t[:] = zip(*sorted(zip(d, k, t)))

For more:

days = [[5,2,7],[5,2]]
km = [[2,3,4],[4,3]]
time = [[30,10,20],[15,25]]
xsss = days, km, time

for xss in zip(*xsss):
    for xs, xs[:] in zip(xss, zip(*sorted(zip(*xss)))):
        pass

An itemgetter solution:

from operator import itemgetter as ig

days = [[5,2,7],[5,2]]
km = [[2,3,4],[4,3]]
time = [[30,10,20],[15,25]]

for lists in zip(days, km, time):
    ordered = ig(*map(ig(0), sorted(enumerate(lists[0]), key=ig(1))))
    for lst in lists:
        lst[:] = ordered(lst)

[Try it online!](https://tio.run/##rVJNa8QgEL37KzzGZQ752KVlYX9JyCGgm0rWKGqh6Z9Pn8ZtKYWyhxWi8t7Mc95k3Brf7NK9Or9tclwDv/C@P1FLLwOlcxjYbDLYUkdHgEfqAEZtVIa7mpqa2hpMg/gTOHa1nkviM/HI9cI/tauSNhADCJnizDiW7M8DwLxH7BBMsYdgfVSyynm7jBCCMef1ErOSKPfZ3G9ZlTH2LA8fISSd32XvzkDdXR1SWDGzU4QvOSkB4OmPp5QFQyUvLTeG8Ji/q7eGW6f8GPGeNg6yXEdlJhWj8nxEbdPz2pAN33SI4d8fab1UXkmo6Kk6mNFVOGtBvLhWy7tJJasqa/X1AG5W6wVhTWrFdwdvIY9MDvvpD9B9PMpDFYBHBmLbvgA "Python 3.8 (pre-release) – Try It Online")

答案2

得分: 0

假设您的所有列表具有相同的形状,通过从列表days中获取排序后的索引并将它们应用于所有后续的列表:

from operator import itemgetter

days = [[5,2,7],[5,2]]
km = [[2,3,4],[4,3]]
time = [[30,10,20],[15,25]]

def sort_by_ind(arr, ind):
    return [itemgetter(*a)(arr[i]) for i, a in enumerate(ind)]

idx_days = [[i[0] for i in sorted(enumerate(a), key=itemgetter(1))] for a in days]
days = sort_by_ind(days, idx_days)
km = sort_by_ind(km, idx_days)
time = sort_by_ind(time, idx_days)

print(days)
print(km)
print(time)

输出结果:

[[2, 5, 7], [2, 5]]
[[3, 2, 4], [3, 4]]
[[10, 30, 20], [25, 15]]
英文:

Assuming that all your lists have same shape, by obtaining sorted indices from list days and apply them to all subsequent lists:

from operator import itemgetter

days = [[5,2,7],[5,2]]
km = [[2,3,4],[4,3]]
time = [[30,10,20],[15,25]]

def sort_by_ind(arr, ind):
    return [itemgetter(*a)(arr[i]) for i, a in enumerate(ind)]

idx_days = [[i[0] for i in sorted(enumerate(a), key=itemgetter(1))] for a in days]
days = sort_by_ind(days, idx_days)
km = sort_by_ind(km, idx_days)
time = sort_by_ind(time, idx_days)

print(days)
print(km)
print(time)

[[2, 5, 7], [2, 5]]
[[3, 2, 4], [3, 4]]
[[10, 30, 20], [25, 15]]

huangapple
  • 本文由 发表于 2023年2月16日 18:26:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定