我有一个列表:
[(14, 2), (14, 2), (16, 2), (14, 2), (15, 2), (15, 2), (21, 2), (15, 2), (18, 2), (15, 2), (19, 2), (25, 2), (22, 2), (17, 2), (31, 2), (26, 2), (21, 2), (25, 2), (29, 2), (33, 2), (25, 2), (23, 2), (25, 2), (19, 2), (12, 2), (29, 2), (18, 2), (21, 2), (13, 2), (13, 2), (18, 2), (11, 2), (12, 2), (20, 2), (23, 2), (17, 2), (14, 2), (17, 2), (12, 2), (13, 2), (15, 2), (21, 2), (15, 2), (19, 2), (22, 2), (16, 2), (16, 2), (13, 2), (17, 2), (18, 2), (20, 2), (18, 2), (13, 2), (13, 2), (18, 2), (14, 2), (13, 2), (22, 2), (14, 2), (25, 2), (22, 2), (9, 2), (18, 2), (22, 2), (19, 2), (13, 2), (14, 2), (15, 2), (13, 2), (17, 2), (21, 2), (18, 2), (21, 2), (18, 2), (15, 2), (16, 2), (13, 2), (16, 2), (16, 2), (15, 2), (11, 2), (24, 2), (15, 2), (12, 2), (20, 2), (21, 2), (21, 2), (14, 2), (11, 2), (26, 2), (17, 2), (21, 2), (16, 2), (13, 2), (15, 2), (13, 2), (12, 2), (22, 2), (16, 2), (13, 2), (13, 2), (22, 2), (12, 2), (16, 2), (16, 2), (21, 2), (19, 2), (15, 2), (16, 2), (16, 2), (13, 2), (14, 2), (14, 2), (20, 2), (14, 2), (20, 2), (13, 2), (19, 2), (20, 2), (17, 2), (17, 2), (25, 2), (22, 2), (22, 2), (22, 2), (14, 2), (19, 2), (20, 2), (16, 2), (13, 2), (19, 2), (16, 2), (12, 2), (18, 2), (20, 2), (19, 2), (18, 2), (15, 2), (22, 2), (18, 2), (20, 2), (14, 2), (19, 2), (16, 2), (18, 2), (28, 2), (14, 2), (17, 2), (17, 2), (23, 2), (18, 2), (24, 2), (17, 2), (18, 2), (18, 2), (22, 2)]
我希望我的输出是一个列表列表,其中每个子列表都是可以加在一起而不会超过一定阈值并且仅作为(每个元素的总和)存储的元素数量:
例如,如果阈值为50(含),则为
[[16, 16, 18], [16, 17, 17], [23, 17], [20, 17], [21, 27], [24, 19], [33], [28], [23, 27], [31], [35], [27], [25], [27, 21] ...]
元组的第二个值可能会有所不同。首选作为列表理解。
编辑:
根据要求,我要清除/优化的原始代码:
padding = len("Packages () ") + math.floor(math.log10(len(apps))+1)
line_length = columns - (padding * 2) - 2
spacings = 2
element_length = [item for sublist in [list(a) for a in zip([len(i) for i in apps],[i for i in itertools.repeat(spacings, len([len(i) for i in apps]))])] for item in sublist]
limits = []
outer_limit = 0
while line_length <= sum(element_length):
while line_length >= sum(element_length[0:outer_limit]):
outer_limit += 1
limits.append(outer_limit - 1)
element_length = element_length[outer_limit - 1:]
outer_limit = 0
message = ""
a = 0
b = 0
for amount in limits:
b += math.ceil(amount / 2)
message += (" " * spacings).join(apps[a:b]) + ("\n" + " " * padding)
a = b
print("Packages ({}) {}".format(len(apps), message))
答案 0 :(得分:1)
这是一个非常简单的方法,只需一个for循环即可:
import { reset } from './protocol'
import SerialPort from 'serialport'
jest.mock('serialport')
describe('test protocol commands', () => {
beforeAll(() => {
const mock = jest.spyOn(SerialPort.prototype, 'write');
mock.mockImplementation(() => {
throw new Error('test error')
});
})
it('should throw an error when calling reset command', () => {
expect(() => reset()).toThrow() // Success!
})
})
答案 1 :(得分:1)
这可以通过functools的reduce函数来完成:
a = [(14, 2), (14, 2), (16, 2), (14, 2), (15, 2), (15, 2), (21, 2), (15, 2), (18, 2), (15, 2), (19, 2), (25, 2), (22, 2), (17, 2), (31, 2), (26, 2), (21, 2), (25, 2), (29, 2), (33, 2), (25, 2), (23, 2), (25, 2), (19, 2), (12, 2), (29, 2), (18, 2), (21, 2), (13, 2), (13, 2), (18, 2), (11, 2), (12, 2), (20, 2), (23, 2), (17, 2), (14, 2), (17, 2), (12, 2), (13, 2), (15, 2), (21, 2), (15, 2), (19, 2), (22, 2), (16, 2), (16, 2), (13, 2), (17, 2), (18, 2), (20, 2), (18, 2), (13, 2), (13, 2), (18, 2), (14, 2), (13, 2), (22, 2), (14, 2), (25, 2), (22, 2), (9, 2), (18, 2), (22, 2), (19, 2), (13, 2), (14, 2), (15, 2), (13, 2), (17, 2), (21, 2), (18, 2), (21, 2), (18, 2), (15, 2), (16, 2), (13, 2), (16, 2), (16, 2), (15, 2), (11, 2), (24, 2), (15, 2), (12, 2), (20, 2), (21, 2), (21, 2), (14, 2), (11, 2), (26, 2), (17, 2), (21, 2), (16, 2), (13, 2), (15, 2), (13, 2), (12, 2), (22, 2), (16, 2), (13, 2), (13, 2), (22, 2), (12, 2), (16, 2), (16, 2), (21, 2), (19, 2), (15, 2), (16, 2), (16, 2), (13, 2), (14, 2), (14, 2), (20, 2), (14, 2), (20, 2), (13, 2), (19, 2), (20, 2), (17, 2), (17, 2), (25, 2), (22, 2), (22, 2), (22, 2), (14, 2), (19, 2), (20, 2), (16, 2), (13, 2), (19, 2), (16, 2), (12, 2), (18, 2), (20, 2), (19, 2), (18, 2), (15, 2), (22, 2), (18, 2), (20, 2), (14, 2), (19, 2), (16, 2), (18, 2), (28, 2), (14, 2), (17, 2), (17, 2), (23, 2), (18, 2), (24, 2), (17, 2), (18, 2), (18, 2), (22, 2)]
from functools import reduce
b = list(reduce(lambda a,b:a+[[b]] if sum(a[-1])+b>50 else a[:-1]+[a[-1]+[b]],map(sum,a),[[]]))
print(b)
# [[16, 16, 18], [16, 17, 17], [23, 17], [20, 17], [21, 27], [24, 19], [33], [28], [23, 27], [31], [35], [27], [25], [27, 21], [14, 31], [20, 23], [15, 15, 20], [13, 14, 22], [25, 19], [16, 19, 14], [15, 17], [23, 17], [21, 24], [18, 18], [15, 19], [20, 22], [20, 15, 15], [20, 16], [15, 24], [16, 27], [24, 11], [20, 24], [21, 15], [16, 17, 15], [19, 23], [20, 23], [20, 17], [18, 15], [18, 18], [17, 13], [26, 17], [14, 22], [23, 23], [16, 13], [28, 19], [23, 18], [15, 17, 15], [14, 24], [18, 15, 15], [24, 14], [18, 18], [23, 21], [17, 18], [18, 15, 16], [16, 22], [16, 22], [15, 21], [22, 19], [19, 27], [24, 24], [24, 16], [21, 22], [18, 15], [21, 18], [14, 20], [22, 21], [20, 17], [24, 20], [22, 16], [21, 18], [20, 30], [16, 19], [19, 25], [20, 26], [19, 20], [20, 24]]