如何在numpy arange中获取第一个和最后一个值?

时间:2016-05-20 13:11:07

标签: python python-3.x numpy range python-2.x

我有一个从numpy.arange创建的列表,我想创建一个区间,其顶部和底部值来自一个列表,以后可以访问其他列表。

表格显示:

|------------------------------------|
| Probability Cumulative | Interval  |
|------------------------------------|
|           3            |  0  - 2   |
|           27           |  3  - 26  |
|           60           |  27 - 59  |
|           72           |  60 - 71  |
|           88           |  72 - 87  |
|           92           |  88 - 91  |
|           100          |  92 - 99  |
|------------------------------------|

这是我的原始代码:

>>> import numpy as np
>>> prob_cumulative = [3, 27, 60, 72, 88, 92, 100]
>>> for x in prob_cumulative:
>>>    newList = np.arange(0,x)
>>>    print newList

[0 1 2]
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 25 26]
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 50 51 52 53 54 55 56 57 58 59]
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71]
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
 75 76 77 78 79 80 81 82 83 84 85 86 87]
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91]
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99]

如果我这样做:

>>> import numpy as np
>>> for x in prob_cumulative:
>>>    newList = np.arange(0,x)
>>>    top = newList[-1]
>>>    bot = newList[0]
>>>    print bot,top
0 2
0 26
0 59
0 71
0 87
0 91
0 99

我得到了每个列表的最高值,因为np.arange( 0 ,x),我的最低值得到0.如何获得从top +的最后值开始的最低值1如表中所示?

编辑: 有了这个:

>>>import numpy as np
>>>prob_cumulative = [3, 27, 60, 72, 88, 92, 100]
>>>bot=0
>>>for x in prob_cumulative:
>>>   newList = np.arange(bot,x)
>>>   top = newList[-1]
>>>   bot = top+1
>>>   print newList
[0 1 2]
[ 3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26]
[27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
 52 53 54 55 56 57 58 59]
[60 61 62 63 64 65 66 67 68 69 70 71]
[72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87]
[88 89 90 91]
[92 93 94 95 96 97 98 99]

2 个答案:

答案 0 :(得分:0)

deepin@deepin-pc:~/test$ python test_list.py 
0
[0, 1, 2]
3
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
27
[27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
60
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71]
72
[72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87]
88
[88, 89, 90, 91]
92
[92, 93, 94, 95, 96, 97, 98, 99]
deepin@deepin-pc:~/test$ cat test_list.py 
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

prob_cumulative = [3, 27, 60, 72, 88, 92, 100]
lastvalue = 0
for x in prob_cumulative:
    print(lastvalue)
    newList = range(lastvalue, x)
    print(newList)
    lastvalue = x
deepin@deepin-pc:~/test$

答案 1 :(得分:0)

“如何获得从表格中显示的top + 1的最后值开始的最低值?”

t = 0
import numpy as np
prob_cumulative = [3, 27, 60, 72, 88, 92, 100]
for x in prob_cumulative:
    newList = np.arange(t,x)
    print newList
    t = x + 1