假设我有一个像[1,2,1.5,2,1.99,2.01,4]
这样的列表,我希望我的函数找到序列[2,1.99,2.01]
并构建它的新列表。在循环遍历初始列表时,这种情况可能会发生多次。我希望我的问题是可以理解的。
Thanks in advance
这是我的尝试:
import itertools
Support = []
Liste1 = [1,2,1.5,2,1.99,2.01,4]
Liste2 = Liste1[1:]
Liste1.pop(-1)
for S1,S2 in itertools.zip_longest(Liste1, Liste2):
if S2<=1.05*S1 and S2>=0.95*S1:
Support.append(S1)
问题是这个循环将值与之前的值进行比较,而不是与第一个检测到的值进行比较。
Layer1 = [round(x,2) for x in list(data["Close"])]
Layer2 = Layer1[0:1]
Layer1.pop(0)
Layer3 = []
Layer4 = []
for element in Layer1:
if element < 1.01*Layer2[0] and element > 0.99*Layer2[0]:
Layer2.append(element)
if len(Layer2)>= 6:
Layer3.extend(Layer2)
Layer4.append(statistics.mean(Layer3))
Layer3.clear()
Layer2.clear()
Layer2.append(element)
else:
Layer2.clear()
Layer2.append(element)
R_S = sorted([round(s,2) for s in Layer4])
R_S_clean = R_S[0:1]
R_S.pop(0)
for element in R_S:
if element >= 1.01*R_S_clean[-1] or element <= 0.99*R_S_clean[-1]:
R_S_clean.append(element)
R_S_clean_rev = reversed(R_S_clean)
for element in R_S_clean_rev:
if element < Price:
Support = element
Delta_S = str(abs(round((100-((100/element)*Price)),2))) + "%"
break
else:
Support = "/"
Delta_S = "/"
for element in R_S_clean:
if element > Price:
Resistance = element
Delta_R = str(abs(round((100-((100/element)*Price)),2))) + "%"
break
else:
Resistance = "/"
Delta_R = "/"
这是我的变通办法,时间会证明性能是否足够好。