在Python中查找列表的限制

时间:2018-03-17 10:15:14

标签: python algorithm pandas math matplotlib

我将以下2个列表映射到彼此:

Values =[1,2,3,4,5,6,8,9,10,9,7,6,5.50,5,6,7,8,10,12,15,14 ,13.50 12]
Dates =[Day1,Day2,Day3,Day4,Day5,Day6,Day8,Day9,Day10,Day11,..,Day20]

enter image description here正如您在屏幕截图中看到的那样

我想编写一个程序,它将遍历liste值并返回

的值
XA= [X,A]
AB= [A,B]
BC=[B,C]
CD= [C,D]
DE=[D,E]

挑战在于我不知道该列表是否仅适用于从X到D的值,或者它将持续到N

我尝试使用下面的代码,但结果并没有给我我想要的东西,因为我需要找到一种方法来存储值,然后再继续这个过程:

for Prs in close:


    # getung the high


    if Prs > SwingHigh:
        SwingHigh = Prs
        Swinglow=Prs


    elif Prs < Swinglow:
        Swinglow = Prs
        PrevHigh=SwingHigh

    else:
        PrevLow=Swinglow



    impulSizee = SwingHigh - InicialPrice
    retrSize = SwingHigh - Swinglow



    # geting the index if the lows low

    print('-------------------------------------')
    print('theprice testing',Prs)
    print('the starting price is InicialPrice ',InicialPrice)
    print('the swing low is PrevLow ',PrevLow)
    print('the swing hige is PrevHigh',PrevHigh)
    print('the new high SwingHigh ',SwingHigh)
    print('the new low Swinglow------ ',Swinglow)

2 个答案:

答案 0 :(得分:2)

IIUC:

In [89]: from scipy.signal import argrelextrema

In [90]: a = np.array([1,2,3,4,5,6,8,9,10,9,7,6,5.50,5,6,7,8,10,12,15,14 ,13.50, 12])

In [91]: idx = np.sort(np.concatenate((argrelextrema(a, np.greater)[0], argrelextrema(a, np.less)[0])))

In [92]: np.split(a, idx)
Out[92]:
[array([1., 2., 3., 4., 5., 6., 8., 9.]),
 array([10. ,  9. ,  7. ,  6. ,  5.5]),
 array([ 5.,  6.,  7.,  8., 10., 12.]),
 array([15. , 14. , 13.5, 12. ])]

In [93]: np.split(a, idx+1)
Out[93]:
[array([ 1.,  2.,  3.,  4.,  5.,  6.,  8.,  9., 10.]),
 array([9. , 7. , 6. , 5.5, 5. ]),
 array([ 6.,  7.,  8., 10., 12., 15.]),
 array([14. , 13.5, 12. ])]

兴趣点:

In [97]: np.concatenate((a[[0]], a[idx], a[[-1]]))
Out[97]: array([ 1., 10.,  5., 15., 12.])

<强>更新

In [129]: df = pd.DataFrame({'Value':Values, 
                             'Date':pd.date_range('2018-01-01', periods=len(Values))})

In [130]: df
Out[130]:
         Date  Value
0  2018-01-01    1.0
1  2018-01-02    2.0
2  2018-01-03    3.0
3  2018-01-04    4.0
4  2018-01-05    5.0
..        ...    ...
18 2018-01-19   12.0
19 2018-01-20   15.0
20 2018-01-21   14.0
21 2018-01-22   13.5
22 2018-01-23   12.0

[23 rows x 2 columns]

In [131]: idx = np.sort(np.concatenate((argrelextrema(df['Value'].values, np.greater)[0], 
                                        argrelextrema(df['Value'].values, np.less)[0])))

In [132]: idx
Out[132]: array([ 8, 13, 19], dtype=int64)

In [133]: df.iloc[idx]
Out[133]:
         Date  Value
8  2018-01-09   10.0
13 2018-01-14    5.0
19 2018-01-20   15.0

In [134]: poi = np.concatenate(([0], idx, [len(df)-1]))

In [135]: df.iloc[poi]
Out[135]:
         Date  Value
0  2018-01-01    1.0
8  2018-01-09   10.0
13 2018-01-14    5.0
19 2018-01-20   15.0
22 2018-01-23   12.0

答案 1 :(得分:2)

将pandas shift + cumsum技巧与groupby结合使用:

s = pd.Series(values)
v = s.gt(s.shift(-1))
[g.tolist() for _, g in s.groupby(v.ne(v.shift()).cumsum())]

[[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 8.0, 9.0],   # XA
 [10.0, 9.0, 7.0, 6.0, 5.5],                 # AB
 [5.0, 6.0, 7.0, 8.0, 10.0, 12.0],           # BC
 [15.0, 14.0, 13.5],                         # CD
 [12.0]]                                     # DE