如何在Python中从这个时间序列list = [60, 90, 120, 60, 90, 120, 150]创建累积list_1 = [60, 90, 120, 180, 210, 240]?

问题描述 投票:0回答:1

数据来自天文钟。 我将上面的问题设置为聊天gpt,但经过多次尝试后我无法得到我想要的。其脚本的总体思路是:

time_series = [60, 90, 120, 60, 90, 120, 150]
result = []

# Start by adding the first value
result.append(time_series[0])

i = 1
while i < len(time_series):
    current_value = time_series[i]

    if current_value >= result[-1]:
        # If the current value is greater than or equal to the last value in result, append it
        result.append(current_value)
    else:
        # If the current value is smaller, sum it with the last value in result
        new_value = result[-1] + current_value
        result.append(new_value)
        
        # Move to the next value and skip all smaller or equal values
        while i < len(time_series) and time_series[i] <= current_value:
            i += 1
        
        # Continue with the next value if available
        if i < len(time_series):
            current_value = time_series[i]
            # Append it if it is greater than or equal to the last value in result
            if current_value >= result[-1]:
                result.append(current_value)
    
    # Move to the next index
    i += 1

print("Final Result:", result)

输出为:

Final Result: [60, 90, 120, 180, 300]

python pandas numpy time-series series
1个回答
0
投票
time_series = [60, 90, 120, 60, 90, 120, 150]
i = 1 # declare i =1 instead of 0
result = [time_series[0]] # with first element inside


while i < len(time_series):

    old_val = time_series[i-1]
    current_value = time_series[i]
    add = abs(old_val - current_value) # number to be added

    if current_value < result[-1]:
        current_value = result[-1] + add
    result.append(current_value)
    
    i += 1

print(result) # Output : [60, 90, 120, 180, 210, 240, 270]
© www.soinside.com 2019 - 2024. All rights reserved.