我正在寻找一种方法来实现与此问题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]
我希望根据a
次10中存储的浮点数(舍入到最接近的整数),在b
和c
中生成新的重复项。例如,a
和b
中的第一个元素必须重复2
次,因为c
中的第一个元素是0.221
和
int(round(c[0]*10.),0) = 2
因此,新a_2
和b_2
列表中的前两个元素如下所示:
a_2 = [0.9935, 0.9935, ...]
b_2 = [4.01, 4.01, ...]
将此应用于a
和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.423
和c[2]=0.125
,这意味着a
和b
中的第二项和第三项需要重复4
和1
次分别
为了完整起见,我会提到这个问题与另一个问题How to obtain a weighted gaussian filter中给出的答案有关。因此,这是使其他问题的答案有效的一个组成部分。
答案 0 :(得分:1)
您可以使用zip()
将a
和c
以及b
和c
与itertools.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
,b
和c
特别大,或者对于某些c
次10的值会导致大量重复并且您只需要处理这些值一个接一个(例如,您不需要在内存中实现整个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()
;现在每个阶段都由迭代器处理,只产生足以一次满足一个步骤,节省内存。
如果您需要,只需要迭代输出一次就可以使用它;如果您的绘图库采用一般迭代来绘制这些应该可以正常工作。
答案 1 :(得分:1)
您可以使用chain
,izip
和repeat
:
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)))