我有10,000个1和0的列表。使用Numpy,我想使用将计算一系列游程长度的函数执行游程长度编码,但是不确定从哪里开始。
我认为我可能必须遍历游程并可能使用某种计数器,但是我不太确定。我最喜欢提示而不是完整的答案。谢谢!
虽然我不确定如何正确地验证序列,但我确实是根据要点找到了该功能:
def rldecode(starts, lengths, values, minlength=None):
"""
Decode a run-length encoding of a 1D array.
Parameters
----------
starts, lengths, values : 1D array_like
The run-length encoding.
minlength : int, optional
Minimum length of the output array.
Returns
-------
1D array. Missing data will be filled with NaNs.
"""
starts, lengths, values = map(np.asarray, (starts, lengths, values))
# TODO: check validity of rle
ends = starts + lengths
n = ends[-1]
if minlength is not None:
n = max(minlength, n)
x = np.full(n, np.nan)
for lo, hi, val in zip(starts, ends, values):
x[lo:hi] = val
return x
[source][1]