用Python求列表的极限

2024-06-16 11:28:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我将以下两个列表相互映射:

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)

Tags: the列表newifislowprinthigh
2条回答

将熊猫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

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

相关问题 更多 >