0

我正在寻找一种方法来实现类似于这个问题R: repeat elements of a list based on another list但在 python 中的问题。

基本上我有三个相同长度的列表:

a = [0.9935,0.1955,1.3165,0.0975,0.2995,-0.0075,1.5015,1.0055,0.4525,0.2235,1.3815,0.4195,1.3685,0.0325,0.0055,0.6175,0.0615,1.8115]
b = [4.01,5.207,-0.245,5.312,0.841,1.204,-0.413,4.398,5.309,1.149, -0.295,1.903,-0.851,1.236,1.39,3.48,-0.034,4.286]
c = [0.221, 0.423, 0.125, 0.228, 0.233, 0.235, 0.244, 0.249, 0.265, 0.265, 0.268, 0.268, 0.275, 0.299, 0.301, 0.316, 0.318, 0.329]

我想根据存储在 10 次中的浮点数(四舍五入到最接近的整数)生成新的重复项。例如,and中的第一个元素必须重复多次,因为 and 中的第一个元素是andabcab2c0.221

int(round(c[0]*10.),0) = 2

所以 newa_2b_2lists 中的前两个元素看起来像:

a_2 = [0.9935, 0.9935, ...]
b_2 = [4.01, 4.01, ...]

将此应用于aand中的前三个项目b将导致:

a_2 = [0.9935, 0.9935, 0.1955, 0.1955, 0.1955, 0.1955, 1.3165, ...]
b_2 = [4.01, 4.01, 5.207, 5.207, 5.207, 5.207, -0.245, ...]

因为c[1]=0.423andc[2]=0.125意味着 and 中的第二项和第三项a需要b分别重复41次。

添加

为了完整起见,我会提到这个问题与另一个问题如何获得加权高斯滤波器中给出的答案有关。因此,这是使其他问题的答案起作用的一个组成部分。

4

2 回答 2

1

您将使用zip()组合aand c, and band c, withitertools.chain.from_iterable()来生成新序列:

from itertools import chain

a_2 = list(chain.from_iterable([i] * int(round(j * 10)) for i, j in zip(a, c)))
b_2 = list(chain.from_iterable([i] * int(round(j * 10)) for i, j in zip(b, c)))

演示:

>>> from itertools import chain
>>> a = [0.9935,0.1955,1.3165,0.0975,0.2995,-0.0075,1.5015,1.0055,0.4525,0.2235,1.3815,0.4195,1.3685,0.0325,0.0055,0.6175,0.0615,1.8115]
>>> b = [4.01,5.207,-0.245,5.312,0.841,1.204,-0.413,4.398,5.309,1.149, -0.295,1.903,-0.851,1.236,1.39,3.48,-0.034,4.286]
>>> c = [0.221, 0.423, 0.125, 0.228, 0.233, 0.235, 0.244, 0.249, 0.265, 0.265, 0.268, 0.268, 0.275, 0.299, 0.301, 0.316, 0.318, 0.329]
>>> list(chain.from_iterable([i] * int(round(j * 10)) for i, j in zip(a, c)))
[0.9935, 0.9935, 0.1955, 0.1955, 0.1955, 0.1955, 1.3165, 0.0975, 0.0975, 0.2995, 0.2995, -0.0075, -0.0075, 1.5015, 1.5015, 1.0055, 1.0055, 0.4525, 0.4525, 0.4525, 0.2235, 0.2235, 0.2235, 1.3815, 1.3815, 1.3815, 0.4195, 0.4195, 0.4195, 1.3685, 1.3685, 1.3685, 0.0325, 0.0325, 0.0325, 0.0055, 0.0055, 0.0055, 0.6175, 0.6175, 0.6175, 0.0615, 0.0615, 0.0615, 1.8115, 1.8115, 1.8115]
>>> list(chain.from_iterable([i] * int(round(j * 10)) for i, j in zip(b, c)))
[4.01, 4.01, 5.207, 5.207, 5.207, 5.207, -0.245, 5.312, 5.312, 0.841, 0.841, 1.204, 1.204, -0.413, -0.413, 4.398, 4.398, 5.309, 5.309, 5.309, 1.149, 1.149, 1.149, -0.295, -0.295, -0.295, 1.903, 1.903, 1.903, -0.851, -0.851, -0.851, 1.236, 1.236, 1.236, 1.39, 1.39, 1.39, 3.48, 3.48, 3.48, -0.034, -0.034, -0.034, 4.286, 4.286, 4.286]

如果a,bc特别大,或者如果c10 的某些值导致大量重复并且您只需要一个一个地处理这些值(例如,您不需要在内存中实现整体a_2和列表),您b_2可以在这里使用更多itertools功能:

from itertools import chain, repeat, izip

a_2_generator = chain.from_iterable(repeat(i, int(round(j * 10))) for i, j in izip(a, c))
b_2_generator = chain.from_iterable(repeat(i, int(round(j * 10))) for i, j in izip(b, c))

注意这里的缺失list();现在每个阶段都由迭代器处理,每次只产生足以满足一个步骤的内容,从而节省内存。

如果您只需要迭代输出一次,请使用此选项;如果您的绘图库采用通用迭代来绘制这些应该可以正常工作。

于 2013-11-08T13:50:36.027 回答
1

您可以使用chain,iziprepeat:

from itertools import chain, repeat, izip
new_a = list(chain.from_iterable(repeat(el, int(round(n * 10))) for el, n in izip(a, c)))
于 2013-11-08T13:51:57.073 回答