我想删除加倍的时间值(包括为其分配的测量值),并在两者之间填充缺失的时间值。
我正在处理一些模拟数据。基本上,在下面显示的示例中有列表。应删除倍增的时间元素,并为其分配指定的测量值。然后,应添加缺少的时间元素。缺少的测量值应由“-”代替。以后我可能会插值。
我尝试了一些事情,只是遍历列表,然后追加和插入,但这似乎不起作用。我也找不到解决方案的类似问题。
谢谢!
time = [1, 2, 3, 3, 5, 7, 7, 10]
meas = [10,20,30,40,50,60,70,80]
预期列表如下:
time = [1,2,3,4,5,6,7,8,9,10]
meas = [10 20 30 - 50 - 60 - - 80]
答案 0 :(得分:0)
起草一个粗略的解决方案,可能会有战利品。但这有效。
time = [1, 2, 3, 3, 5, 7, 7, 10]
meas = [10,20,30,40,50,60,70,80]
# Remove duplicate time and sort result
t = list(set(time))
t.sort()
# Remove duplicate objects (keep 1st instance)
for n, e in enumerate(t):
while e != time[n]:
del time[n]
del meas[n]
# Check that it works
print(time)
print(meas)
# Create list in the wanted format
m = 0 # keep track of meas index
meas_padded = [] # new list for result, could be done using the meas list but im to lazy right now.
for n in range(0, time[-1]):
if n + 1 == time[m]:
meas_padded.append(meas[m])
m += 1
else:
meas_padded.append('--')
# As the list is padded to time[-1] size, I will not create the time series as it will map towars meas_padded index n+1
# Added padding and string repr since it was neat for debug.
print([str(n+1).zfill(2) for n, m in enumerate(meas_padded)])
print([str(m) for m in meas_padded])
输出:
[1, 2, 3, 5, 7, 10]
[10, 20, 30, 50, 60, 80]
['01', '02', '03', '04', '05', '06', '07', '08', '09', '10']
['10', '20', '30', '--', '50', '--', '60', '--', '--', '80']
希望它可以帮助您:)
填充meas
而不需要创建一个新的,可以使用带有reverse_range的一些技巧
答案 1 :(得分:0)
您可以使用numpy
来加快脚本速度:
import numpy as np
def get_correction(time, meas):
"""
Deleting doubled measuring points and adding missing one
:param np.array time: 1D array which contains times
:param np.array time: 1D array with measurements for corresponding times
:return: a tuple of corrected arrays (`time`,`meas`)
"""
if type(t) == list: time = np.array(time)
if type(m) == list: meas = np.array(meas)
t_, index = np.unique(t, return_index=True) # Determine unique times and their indexes
measures = np.zeros(np.max(t)) ; measures[:] = np.nan # Create empty measures array
measures[t_ - 1] = m[index] # Insert correspondant values in measures array
time = np.arange(np.max(t)) + 1 # Create correpondant times
return (time, measures)
如果您测试此功能:
time = [1, 2, 3, 3, 5, 7, 7, 10]
meas = [10,20,30,40,50,60,70,80]
get_correction(time, meas)
您获得:
(array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
array([10., 20., 30., nan, 50., nan, 60., nan, nan, 80.]))