我有一系列商品,每个商品都分配了权重。我想将其分成相等大小的大约等于累计重量。使用numpy https://stackoverflow.com/a/33555976/10690958可以做到这一点 有没有简单的方法可以使用纯python完成此操作?
示例数组:
[ ['bob',12],
['jack,6],
['jim',33],
....
]
或
a, 11
b,2
c, 5
d, 3
e, 3
f, 2
这里的输出正确(假设需要2个块)
[a,11],[b,2] - cumulative weight of 13
和
[c,5],[d,3],[e,3],[f,2] - cumulative weight of 13
为进一步阐明这个问题,设想一下将100个人分入10部电梯的情况,我们希望每部电梯具有相同的近似值。总重量(该电梯中所有人员的总重量)。因此,第一个列表将成为名称和权重。这是一个负载平衡问题。
答案 0 :(得分:1)
您需要类似以下内容:
array =[ ['bob',12],
['jack',6],
['jim',33],
['bob2',1],
['jack2',16],
['jim2',3],
['bob3',7],
['jack3',6],
['jim3',1],
]
array = sorted(array, key= lambda pair: pair[1], )
summ = sum(pair[1] for pair in array )
chunks = 4
splmitt = summ // chunks
print(array)
print(summ)
print(splmitt)
def split(array, split):
splarr = []
tlist = []
summ = 0
for pair in array:
summ += pair[1]
tlist.append(pair)
if summ > split:
splarr.append(tlist)
tlist = []
summ = 0
if tlist:
splarr.append(tlist)
return splarr
spl = split(array, splmitt)
import pprint
pprint.pprint(spl)
答案 1 :(得分:1)
您只需要模拟cumsum
:建立一个汇总权重的列表。最后,您将获得总重量。使用累积的权重扫描列表,并在每次达到total_weight / number_of_chunks时创建一个新块。代码可能是:
def split(w_list, n_chunks):
# mimic a cumsum
s = [0,[]]
for i in w_list:
s[0]+= i[1]
s[1].append(s[0])
# now scan the lists populating the chunks
index = 0
splitted = []
stop = 0
chunk = 1
stop = s[0] / n_chunks
for i in range(len(w_list)):
# print(stop, s[1][i]) # uncomment for traces
if s[1][i] >= stop: # reached a stop ?
splitted.append(w_list[index:i+1]) # register a new chunk
index = i+1
chunk += 1
if chunk == n_chunks: # ok we can stop
break
stop = s[0] * chunk / n_chunks # next stop
splitted.append(w_list[index:]) # do not forget last chunk
return splitted