简体   繁体   English

根据Python中元组中第一个字段的总和对元组列表列表进行排序

[英]Sorting a list of list of tuples based on the sum of first field in the tuple in Python

I have an outermost list which contains a monthly total count of different items in this format. 我有一个最外面的列表,其中包含这种格式的每月不同项目的总数。 Every month has the same items. 每个月都有相同的项目。

big_list = [
  [
    (20, 'Item A', 'Jan'),
    (30, 'Item B', 'Jan'),
    (12, 'Item C', 'Jan'),
  ],
  [
    (22, 'Item A', 'Feb'),
    (34, 'Item B', 'Feb'),
    (15, 'Item C', 'Feb'),
  ],

  .... # until 'Dec'
]

And I want to sort this list based on the total number of item counts throughout a year. 我想根据一年中的项目总数对该列表进行排序。 (The sum of the first field in the tuple of a particular item throughout the year) . (全年中特定项目的元组中第一个字段的总和) For instance, if Item C has the most counts in the two months followed by Item A and Item B , the end result would be 例如,如果Item C在两个月中的计数最高,其次是Item AItem B ,则最终结果将是

[
  [
    (12, 'Item C', 'Jan'),
    (20, 'Item A', 'Jan'),
    (30, 'Item B', 'Jan'),
  ],
  [
    (15, 'Item C', 'Feb'),
    (22, 'Item A', 'Feb'),
    (34, 'Item B', 'Feb'),
  ],

  ... # until 'Dec'
]
# Item C = 12 + 15 = 27
# Item A = 20 + 22 = 42
# Item B = 30 + 34 = 64

How can I achieve this? 我该如何实现? Any help or enlightenment would be much appreciated. 任何帮助或启示将不胜感激。

big_list = [
  [
    (20, 'Item A', 'Jan'),
    (30, 'Item B', 'Jan'),
    (12, 'Item C', 'Jan'),
  ],
  [
    (22, 'Item A', 'Feb'),
    (34, 'Item B', 'Feb'),
    (15, 'Item C', 'Feb'),
  ]]

s = {}
for l in big_list:
    for m in l:
        s[m[1]] = s.get(m[1], 0) + m[0]

gives us s - the sums we want to use to sort: {'Item A': 42, 'Item B': 64, 'Item C': 27} 给我们s我们要用于排序的总和: {'Item A': 42, 'Item B': 64, 'Item C': 27}

And finally: 最后:

for l in big_list:
    l.sort(key=lambda x: s[x[1]])

changes big_list to: big_list更改为:

[[(12, 'Item C', 'Jan'), (20, 'Item A', 'Jan'), (30, 'Item B', 'Jan')],
 [(15, 'Item C', 'Feb'), (22, 'Item A', 'Feb'), (34, 'Item B', 'Feb')]]

This solution works for lists within months in any order and also if some item does not appear in some month. 此解决方案适用于以任何顺序在几个月内出现的列表,并且如果某个项目在某个月没有出现也可以使用。

If you really need a two liner: 如果您确实需要两个衬板:

for small_list in big_list:
  small_list.sort(key=lambda x: -sum([y[0] for l in big_list for y in l  if y[1] == x[1]]))

edit: or even a one-liner 编辑:甚至单线

[sorted(small_list, key=lambda x: -sum([y[0] for l in big_list for y in l  if y[1] == x[1]])) for small_list in big_list]

@Pankrat was close: @Pankrat很接近:

for inner in big_list:
    inner.sort()

You have a list of lists inside, so a simple list.sort() won't work on just that. 您内部有一个列表列表,因此,简单的list.sort()不能list.sort() You have to get into the inner level list to sort it (which holds the tuples). 您必须进入内部级别列表才能对其进行排序(该列表包含元组)。

Luckily, your case only requires you to sort the first element in the tuples; 幸运的是,您的案例仅要求您对元组中的第一个元素进行排序; if you had to sort the others, then you would require something else, like so: 如果您必须对其他商品进行排序,那么您将需要其他商品,例如:

for inner in big_list:
    inner.sort(key = lambda x: x[i]) # i is the index location you want to sort on

My proposed solution: 我建议的解决方案:

[sublist.sort() for sublist in biglist]

afterwards the biglist is sorted. 之后,对大名单进行排序。 you don't have to assign the list-comprehension! 您不必分配列表理解!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM