Generator仅使用最终项目的副本填充列表

时间:2014-06-25 22:22:19

标签: python python-2.7 python-3.x generator

我正在使用methodHalton sequence转换为Python中的generator。我在Python 2.7和Python 3.3中遇到了同样的问题。

当我使用我的生成器创建一个生成器对象并反复调用my_object.next()时,我得到了我期望的结果。但是,当我调用list(my_object)tuple(my_object)时,我会获得适当大小的列表/元组,但每个元素只是生成器输出的最终值的副本。我的Python 3.3代码如下:

def halton_gen(dim, num_pts):
    sequence = np.empty(dim)
    sequence.fill(np.nan)
    primes = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31)  # first 11 prime #s
    log_pts = log(num_pts + 1)
    k = 1

    # Generate the sequence
    while k <= num_pts:
        print("k = {}".format(k))
        for i in range(dim):
            print("i = {}".format(i))
            prime = primes[i]
            num_helper = int(ceil(log_pts / log(prime)))

            kk = k
            sum_ = 0

            for j in range(num_helper):
                sum_ += fmod(kk, prime) * pow(prime, -(j + 1))
                kk = floor(kk / prime)

            sequence[i] = sum_

        print("sequence {}".format(sequence))
        yield sequence
        k += 1

请注意,上面代码中的print语句给出了我的期望,但使用list(my_object)tuple(my_object)创建的列表/元组不正确:

my_object = halton_gen(2, 5)    
tuple(my_object)

k = 1
i = 0
i = 1
sequence [ 0.5         0.33333333]
k = 2
i = 0
i = 1
sequence [ 0.25        0.66666667]
k = 3
i = 0
i = 1
sequence [ 0.75        0.11111111]
k = 4
i = 0
i = 1
sequence [ 0.125       0.44444444]
k = 5
i = 0
i = 1
sequence [ 0.625       0.77777778]

Out[86]:
(array([ 0.625     ,  0.77777778]),
 array([ 0.625     ,  0.77777778]),
 array([ 0.625     ,  0.77777778]),
 array([ 0.625     ,  0.77777778]),
 array([ 0.625     ,  0.77777778]))

我期待(删除array()说明符以提高可读性):

([0.5    0.333],
 [0.25   0.667],
 [0.75   0.111],
 [0.125  0.444],
 [0.625  0.778])

如何让我的生成器正确填充我的列表?或者我的列表正确解压我的发电机?无论说明的方式是什么。

1 个答案:

答案 0 :(得分:2)

您的sequence对象正在被回收。一个解决方法是每次产量时重新创建它。

def halton_gen(dim, num_pts):
    primes = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31)  # first 11 prime #s
    log_pts = log(num_pts + 1)
    k = 1

    # Generate the sequence
    while k <= num_pts:
        sequence = np.empty(dim)
        sequence.fill(np.nan)
        print("k = {}".format(k))
        for i in range(dim):
            print("i = {}".format(i))
            prime = primes[i]
            num_helper = int(ceil(log_pts / log(prime)))

            kk = k
            sum_ = 0

            for j in range(num_helper):
                sum_ += fmod(kk, prime) * pow(prime, -(j + 1))
                kk = floor(kk / prime)

            sequence[i] = sum_

        print("sequence {}".format(sequence))
        yield sequence
        k += 1

另一种解决方法是在评论中提及使用sequence.copy()

    yield sequence.copy()