繁体   English   中英

扩展列表的“pythonic”方式

[英]The "pythonic" way for expending a list

我有一个代表更改时间点的列表和另一个值:

indexes_list = [5, 6, 8, 9, 12, 15]
# [ 5  6  8  9 12 15]
values_list = [i * 10 for i in range(6)]
# [ 0 10 20 30 40 50]

我想创建“完整”列表,在上面的例子中是:

expanded_values = [0, 0, 0, 0, 0, 0, 10, 20, 20, 30, 40, 40, 40, 50, 50, 50]
# [ 0  0  0  0  0  0 10 20 20 30 40 40 40 50 50 50]

我写了一些东西,但感觉不对,我想有一种更好、更pythonic的方法来做到这一点:

result = []
for i in range(len(values_list)):
    if i == 0:
        tmp = [values_list[i]] * (indexes_list[i] + 1)
    else:
        tmp = [values_list[i]] * (indexes_list[i] - indexes_list[i - 1])
    result += tmp
# result = [0, 0, 0, 0, 0, 0, 10, 20, 20, 30, 40, 40, 40, 50, 50, 50]

用:

indexes_array = [5, 6, 8, 9, 12, 15]
values_array = [i * 10 for i, _ in enumerate(range(6))]

diffs = indexes_array[:1] + [j - i for i, j in zip(indexes_array, indexes_array[1:])]
res = [v for i, v in zip(diffs, values_array) for _ in range(i)]
print(res)

输出

[0, 0, 0, 0, 0, 10, 20, 20, 30, 40, 40, 40, 50, 50, 50]

作为替代方案,您可以使用带有扭曲的成对配方:

from itertools import tee


def pairwise(iterable, prepend):
    a, b = tee(iterable)
    yield prepend, next(b, None)
    yield from zip(a, b)


indices = [5, 6, 8, 9, 12, 15]
values = [i * 10 for i, _ in enumerate(range(6))]

differences = [second - first for first, second in pairwise(indices, prepend=0)]
res = [v for i, v in zip(differences, values) for _ in range(i)]

print(res)

输出

[0, 0, 0, 0, 0, 10, 20, 20, 30, 40, 40, 40, 50, 50, 50]

最后,如果您正在做数值工作,我建议您使用numpy ,如下所示:

import numpy as np

indices = [5, 6, 8, 9, 12, 15]
values = [i * 10 for i, _ in enumerate(range(6))]


differences = np.diff(indices, prepend=0)
res = np.repeat(values, differences).tolist()
print(res)

我认为使用适当的库是 Pythonic 的,在这种情况下是pandas

import pandas as pd

indexes_array = [5, 6, 8, 9, 12, 15]
values_array = [i * 10 for i in range(6)]

series = pd.Series(values_array, indexes_array).reindex(
    range(indexes_array[-1] + 1), method='backfill')

series
0      0
1      0
2      0
3      0
4      0
5      0
6     10
7     20
8     20
9     30
10    40
11    40
12    40
13    50
14    50
15    50
dtype: int64

有关详细信息,请参阅reindex 文档

尝试这个:

indexes_array = [5, 6, 8, 9, 12, 15]
# [ 5  6  8  9 12 15]
values_array = [i * 10 for i, _ in enumerate(range(6))]
# [ 0 10 20 30 40 50]

result = []
last_ind = 0
zipped = zip(indexes_array, values_array)

for ind, val in zipped:
    count = ind - last_ind
    last_ind = ind
    for i in range(count):
        result.append(val)
print(result)

输出:

[0, 0, 0, 0, 0, 10, 20, 20, 30, 40, 40, 40, 50, 50, 50]

尝试这个:

indexes_array = [5, 6, 8, 9, 12, 15]
values_array = [i * 10 for i, _ in enumerate(range(6))]
output=[]
for x in range(len(indexes_array)):
  if x ==0:
    output.extend([values_array[x]]*indexes_array[x])
  else:
    output.extend([values_array[x]]*(indexes_array[x]-indexes_array[x-1]))

print(output)

输出是:

[0, 0, 0, 0, 0, 10, 20, 20, 30, 40, 40, 40, 50, 50, 50]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM