【问题标题】:How can i check in first List index 1 is greater than second list index 0, and first List index 2 is greater than second list index 1. and so on我如何检查第一个列表索引 1 是否大于第二个列表索引 0,以及第一个列表索引 2 是否大于第二个列表索引 1。依此类推
【发布时间】:2021-12-08 03:45:19
【问题描述】:

我有三个列表 -

High:  [18365.5, 18979.25, 19297.4, 19874.8, 20288.0, 20504.65, 20398.2]
Low:  [17855.5, 18265.0, 18822.55, 18742.15, 19492.55, 20055.55, 20131.25]
Close:  [18317.05, 18969.95, 18857.6, 19804.0, 20260.15, 20285.0, 20215.7]

我想检查一下

if Close [index1] > Low [index0]
   print("XYZ")
elif Close[index1] < High[index0]
   print("XYZ")

对 close 中的其余索引执行相同操作。

if Close [index2] > Low [index1]
   print("XYZ")
elif Close[index2] < High[index1]
   print("XYZ")

请帮我解决这个问题。我不明白我该怎么做。

【问题讨论】:

标签: python list if-statement


【解决方案1】:

按照您的要求解决。

考虑到其股票市场,正确的解决方案是检查所有列表的相同索引。

high = [18365.5, 18979.25, 19297.4, 19874.8, 20288.0, 20504.65, 20398.2]
low =  [17855.5, 18265.0, 18822.55, 18742.15, 19492.55, 20055.55, 20131.25]
close =  [18317.05, 18969.95, 18857.6, 19804.0, 20260.15, 20285.0, 20215.7]


for i in range(len(high)-1):
    if (close[i+1] > low[i]) and (close[i+1] < high[i]):
        print("Valid Indices")
    else:
        print("Invalid: Correct Sequence: Low < Close < High")

# Suggestion: Low =< Close =< High

【讨论】:

    【解决方案2】:

    您可以zip 这三个列表并对其进行迭代。确保正确切分Close,以便评估正确的项目:

    for (high, low, close) in zip(High, Low, Close[1:]):
        if close > low:
            print("S")
        elif close < high:
            print("L")
        else:
            continue
    

    【讨论】:

      猜你喜欢
      • 2011-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-29
      • 2015-12-06
      相关资源
      最近更新 更多