我有数字列表(0-100)
numbers = list(range(101))
然后我需要使用CumSum直到达到1000,然后停止计算并打印'达到最大值'
np.cumsum(numbers)
array([ 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55,
66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231,
253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528,
561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946,
990, 1035, 1081, 1128, 1176, 1225, 1275, 1326, 1378, 1431, 1485,
1540, 1596, 1653, 1711, 1770, 1830, 1891, 1953, 2016, 2080, 2145,
2211, 2278, 2346, 2415, 2485, 2556, 2628, 2701, 2775, 2850, 2926,
3003, 3081, 3160, 3240, 3321, 3403, 3486, 3570, 3655, 3741, 3828,
3916, 4005, 4095, 4186, 4278, 4371, 4465, 4560, 4656, 4753, 4851,
4950, 5050], dtype=int32)
现在我得到了整个列表的CumSum
我在考虑,
0> 1000 ==>不,继续
0 + 1> 1000 ==>不,继续
...
990 + 45> 1000 ==>是。打印'MAX VALUE REACHED', 并停止计算
我期待这样的事情
array([ 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55,
66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231,
253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528,
561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946,
990])
MAX VALUE REACHED
有人可以帮助我将我的想法转化为代码吗?
谢谢!
答案 0 :(得分:1)
鉴于克莱门特先生的评论,我希望现在你有这样的事情。
>>> total = 0
>>> i = 0
>>> while total < 1000:
... total += i
... i += 1
...
>>> print (total)
1035
但有人应该迟早建议您仔细查看itertools库以及numpy。你的代码可以更紧凑地编写,就像这样。
>>> from itertools import count, accumulate, dropwhile
>>> next(dropwhile(lambda x: x<1000, accumulate(count())))
1035
这称为functional programming。 count
是一个所谓的生成器,它产生一系列可能无穷大的数字,在本例中为0,1,2,3 ......。 accumulate
产生了这些潜在无限的一系列总和。 dropwhile
安排忽略此系列中无法满足lambda
条件的数字。最后next
生成结果序列中的下一个项目。
答案 1 :(得分:1)
我现在有这样的事情:
def cumsum(x):
q = np.array(range (0,x))
z = q.cumsum()
v = np.clip(z, 0, 1000)
solution = ['MAX VALUE REACHED' if i == 1000 else i for i in v]
if x == 0:
return[0]
else:
return solution
我的输出是
[0,
1,
3,
6,
10,
15,
21,
28,
36,
45,
55,
66,
78,
91,
105,
120,
136,
153,
171,
190,
210,
231,
253,
276,
300,
325,
351,
378,
406,
435,
465,
496,
528,
561,
595,
630,
666,
703,
741,
780,
820,
861,
903,
946,
990,
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED',
'MAX VALUE REACHED']
现在我必须在一次“MAX VALUE REACHED”之后停止计算。 任何sugestions?
建议的替代
而不是使用clip
创建一个仅包含小于1,000的cumsum
项的新列表,并且当您遇到一个至少突破1,000的项时。
import numpy as np
q = np.array(range(101))
z = q.cumsum()
temp = []
for _ in z:
if _ < 1000:
temp.append(_)
else:
break
solution = np.array(temp)
print (solution)
print('MAX VALUE REACHED')
输出:
[ 0 1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153
171 190 210 231 253 276 300 325 351 378 406 435 465 496 528 561 595 630
666 703 741 780 820 861 903 946 990]
MAX VALUE REACHED