将列表中的整数排序到箱中

时间:2016-01-22 01:35:54

标签: python list list-comprehension

我有一个名为shoeizes和quantity的整数列表。鞋类列表中的整数表示为在数量列表中具有数量(在相同索引中)的箱。我想重新整理清单,以便我按照升序排列鞋子,每个鞋子的所有数量总和在一起。

shoesizes    = [ 2 ,  5 ,  6 , 1 , 3 , 2 ,  4 ,  5 , 2 , 3 , 1 ]    
quantities   = [ 50, 100, 120, 20, 40, 10, 90 , 10 ,30 , 20, 80]

所以期望的输出是:

orderedsizes = [ 1 , 2 , 3 , 4 , 5 ,  6 ]
totalquant   = [100, 90, 60, 90,110, 120] 

2 个答案:

答案 0 :(得分:5)

因为您无论如何都需要排序,itertools.groupby can be used to do this pretty nicely

from operator import itemgetter
from itertools import groupby

shoesizes    = [ 2 ,  5 ,  6 , 1 , 3 , 2 ,  4 ,  5 , 2 , 3 , 1 ]    
quantities   = [ 50, 100, 120, 20, 40, 10, 90 , 10 ,30 , 20, 80]

# For convenience, short names for itemgetters
getsize, getcnt = itemgetter(0), itemgetter(1)

# Sort to bins of same size next to each other
sorted_bins = sorted(zip(shoesizes, quantities), key=getsize)

# Group and sum bins of same size
summed_sizes = [(k, sum(map(getcnt, g))) for k, g in groupby(sorted_bins, key=getsize)]

# Convert back to separate lists
orderedsizes, totalquant = map(list, zip(*summed_sizes))

print(orderedsizes)
print(totalquant)

哪个输出:

[1, 2, 3, 4, 5, 6]
[100, 90, 60, 90, 110, 120]

也可以使用collections.Counter(或仅collections.defaultdict(int))执行此操作,然后对.items()的{​​{1}}进行排序;除非箱子的数量巨大,否则性能差异不太重要;如果你不想在第一时间排序,Counter通过完全避免排序更有意义。示例代码:

Counter

答案 1 :(得分:0)

您正在创建具有重复键的字典式结构。我会像这样累积值:

# ordersizes is just the set of unique size values in a list: sorted(list(set(shoesizes)))

[sum(v for key, v in zip(shoesizes, quantities) if key == index) for index in orderedsizes]