num_list = [1, 3, 5, 9, 18, 20, 22, 25, 27, 31]
overflow = 20
total = 0
key = 1
number_dict = {1: [1]}
for left, right in zip(num_list[:-1], num_list[1:]):
total += right - left
if total >= overflow:
key += 1
number_dict[key] = [right]
total = 0
else:
number_dict[key].append(right)
for k, v in sorted(number_dict.items()):
print k
print v
출력 :
1
[1, 3, 5, 9, 18, 20]
2
[22, 25, 27, 31]
-------------------우선, km
어떤 것에 할당되기 전에 사용하고 있습니다 .
Traceback (most recent call last):
File "<pyshell#29>", line 15, in <module>
number_dict.update({km: num_list})
NameError: name 'km' is not defined
ndpu가 지적했듯이 마지막 x는 num_list의 범위를 벗어납니다.
출처
https://stackoverflow.com/questions/22049897