如何修复Python中动态列表索引超出范围的问题

2024-05-19 07:57:23 发布

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

我们正在进行一个Python项目,在这个项目中,我们更新两个列表,然后用另一个列表中的元素减去其中一个列表中的元素。两个列表的示例:

list_station_temp = {25.0, 24.8, 24.9}
list_lpn_temp = {25.2, 24.5, 24.8}

不幸的是,对于我们来说,列表使用Crontab每20分钟动态更新一次,这意味着有时2个列表的长度是不同的,因此例如,列表的长度可以不同:

有时len(list_lpn_temp) = 3 and len(list_station_temp) = 2当我们试图相互减去列表时,我们会收到一个超出范围的索引

list_lpn_temp = []
list_station_temp = []
new_list_lpn_temp = []
list_lpn_WL = []
list_lpn_validated = []

for x in 3:
    a_temp = pd.read_sql('SELECT temperature FROM Raw_Data', conn).astype(float).values
    c_temp = pd.read_sql('SELECT temperature1m FROM Weather_Station', conn).astype(float).values

    list_lpn_temp.extend(a_temp)
    list_station_temp.extend(c_temp)

for i in range (len(list_lpn_temp)):
    WeLP = list_station_temp[i]-list_lpn_temp[i] #THIS IS WHERE THE INDEX OUT OF RANGE OCCURS
    if min_tol < WeLP < max_tol:
        validated_lpn = 1
        list_lpn_validated.append(validated_lpn)
        new_list_lpn_temp.extend(list_lpn_temp[i])
        list_lpn_WL.extend(WeLP)
    else:
        validated_lpn = 0
        list_lpn_WL.extend(WeLP)
        list_lpn_validated.append(validated_lpn)
        new_list_lpn_temp.extend(None)

如何防止指标超出范围?我们知道for-zip可能有用,但不确定如何实现。感谢所有的帮助!你知道吗

WeLP = list_station_temp[i]-list_lpn_temp[i]

以上代码行是索引超出范围的地方

更新代码

list_lpn_temp = []
list_station_temp = []
new_list_lpn_temp = []
list_lpn_WL = []
list_lpn_validated = []

dict_temperature = {}

for x in 3:
    a_temp = pd.read_sql('SELECT temperature FROM Raw_Data', conn).astype(float).values
    c_temp = pd.read_sql('SELECT temperature1m FROM Weather_Station', conn).astype(float).values

    if a_temp:
        list_lpn_temp.extend(a_temp)
    else: 
        list_lpn_temp.append(float('nan'))

    if c_temp:
        list_station_temp.extend(c_temp)
    else: 
        list_station_temp.append(float('nan'))

for temp in dict_temperature:
    WeLP.append(dict_temperature[temp]['station'] - dict_temperature[temp]['lpn'])
    print (f'WeLP = {WeLP}')
    if min_tol < WeLP < max_tol:
        validated_lpn = 1
        list_lpn_validated.append(validated_lpn)
        new_list_lpn_temp.extend(list_lpn_temp[i])
        list_lpn_WL.extend(WeLP)
    else:
        validated_lpn = 0
        list_lpn_WL.extend(WeLP)
        list_lpn_validated.append(validated_lpn)

更新后接收错误:

  Traceback (most recent call last):
  File "compareTemp.py", line 129, in <module>
    print(f'DICT_TEMPERATURE_STATION = {dict_temperature[temp]["station"]}')
TypeError: list indices must be integers or slices, not str

Tags: in列表newforfloattemplisttemperature
2条回答

你试过python中的set减法运算吗?我认为使用这种方法,您的用例就足够了。你知道吗

>>> list_a = [1,2,3,4]
>>> list_b = [3,4,5,6,7]
>>>
>>> set(list_a) - set(list_b)
set([1, 2])
>>>

输出是一个集合,可以使用list方法将其转换为列表。你知道吗

this_is_a_list = []
this_is_a_dict = {}

你有没有考虑过用一个dict来做同样的事情,而不是用两个单独的列表?你知道吗

temperatures = {
    1: {
        'station': 25.0,
        'lpn': 25.2
    },
    2: {
        'station': 24.8,
        'lpn': 24.5
    },
    3: {
        'station': 24.9,
        'lpn': 24.8
    }
}

WeLP = []

for temp in temperatures:
    WeLP.append(temperatures[temp]['station'] - temperatures[temp]['lpn'])

print(WeLP)

输出为:

[-0.1999999999999993, 0.3000000000000007, 0.09999999999999787]

当添加到dict时,如果temp不存在,则将其添加为null,然后处理错误。我认为你这样做的方式是,如果我正确理解你想要什么,你将错误地从一个结果中减去另一个结果(例如,如果没有收集到一个值)。你知道吗

相关问题 更多 >

    热门问题