在Python中查找列表的限制
我将以下两个列表相互映射:在Python中查找列表的限制,python,algorithm,pandas,math,matplotlib,Python,Algorithm,Pandas,Math,Matplotlib,我将以下两个列表相互映射: 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] 正如你在截图中看到的 我想写一个程序,它将循环列表值并返回 XA= [X,A] AB= [A,B] BC=[B,C] CD= [C,D] DE=[D,E] 挑战在于,我不知道列表是否只包含
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]
正如你在截图中看到的
我想写一个程序,它将循环列表值并返回
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)
对于处于关闭状态的Prs:
#高雄
如果Prs>SwingHigh:
SwingHigh=Prs
Swinglow=Prs
elif Prs
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 [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
IIUC:
或
关注点:
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
将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
将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
我能理解A和C是峰,但X、B和D是什么ᴏʟᴅsᴘᴇᴇᴅ 局部极小值,而不是局部极大值。或者是“低谷”对“高峰”。@真的吗?这个列表中只有一个“15”,而B和C似乎都等于15,所以我很困惑…@Cᴏʟᴅsᴘᴇᴇᴅ 我认为这只是一个输入错误,看看列表和图表,标签应该是5,而不是15。好的,谢谢,这就把事情弄清楚了。我能理解a和C是峰值,但X、B和D是什么?@Cᴏʟᴅsᴘᴇᴇᴅ 局部极小值,而不是局部极大值。或者是“低谷”对“高峰”。@真的吗?这个列表中只有一个“15”,而B和C似乎都等于15,所以我很困惑…@Cᴏʟᴅsᴘᴇᴇᴅ 我认为这只是一个输入错误,看看列表和图表,标签应该是5,而不是15。好的,谢谢,这就把事情弄清楚了。谢谢你的回答,但我不能移动列表元素的位置,因为它们映射了一个日期列表。我将编辑我的问题以添加此信息。np。排序是对列表进行排序吗?这意味着如果我想知道B值是什么时候产生的,我就不会得到正确的日期?知道条目不是unique@Djalillounis,您能用所需的(输出)数据集扩展您的问题吗?谢谢,我会尝试一下,并让您知道它是否有效。我在真实场景中尝试过,但它不起作用,我可以用DM获取更多详细信息吗?谢谢你的回答,但我不能移动列表元素的位置,因为它们与日期列表映射。我将编辑我的问题以添加此信息。np.sort是对列表进行排序吗?这意味着如果我想知道B值是什么时候产生的,我就不会得到正确的日期?知道条目不是unique@Djalillounis,您能用所需的(输出)数据集扩展您的问题吗?谢谢,我将试用它,并让您知道它是否有效。我在真实场景中试用了它,但它不起作用,我可以了解更多详细信息吗?