gpt4 book ai didi

python - 在 Python 中查找列表的限制

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:21:19 25 4
gpt4 key购买 nike

我将以下 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正如您在屏幕截图中看到的那样

我想编写一个程序来循环遍历列表值并返回值

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)

最佳答案

结合使用 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

关于python - 在 Python 中查找列表的限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49334810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com