我有一个由数字数据点组成的时间序列,以列表的形式说:
x = [924, -5, 24, 1, 0, 242, -5, 42, 5, 1, -9, 50, 3, 432, 0, -5, 4]
我需要填充这两个列表:
x_list = []
y_list = []
遵循以下三个步骤:
x[0]
和x[1]
,即 924 和 -5,应分别添加到下面的这两个列表中。x[1]
和x[2]
,并将它们分别添加到x_list
和y_list
。x[2]
tox_list
和x[3]
toy_list
等等,直到x
到达时间序列的末尾。我假设我必须使用def
. 但我不明白如何为此目的设置代码。这是我的代码的当前状态。
import numpy as np
import matplotlib.pyplot. as plt
x = [924, -5, 24, 1, 0, 242, -5, 42, 5, 1, -9, 50, 3, 432, 0, -5, 4]
x_results = []
y_results = []
for t in Timeseries:
next_n = # This is where I am stuck, struggling to find the correct code
next_y =
x_results.append(next_x)
y_results.append(next_y)
plt.plot(x_results, y_results, "bo")
plt.show()
对于 python ≥ 3.10,这是以下工作itertools.pairwise
:
from itertools import pairwise
x, y = zip(*pairwise(mylist))
输出:
x
(924, -5, 24, 1, 0, 242, -5, 42, 5, 1, -9, 50, 3, 432, 0, -5),
y
(-5, 24, 1, 0, 242, -5, 42, 5, 1, -9, 50, 3, 432, 0, -5, 4)
对于 python < 3.10,pairwise 的方法是:
from itertools import tee
def pairwise(iterable):
a, b = tee(iterable)
next(b, None)
return zip(a, b)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句