使用这个 setitem 函数来克服列表理解限制会不会很不Pythonic?

发布于 2024-09-12 03:54:04 字数 570 浏览 5 评论 0原文

>>> a=range(5)
>>> [a[i] for i in range(0,len(a),2)] ## list comprehension for side effects
[0, 2, 4]
>>> a
[0, 1, 2, 3, 4]
>>> [a[i]=3 for i in range(0,len(a),2)] ## try to do assignment
SyntaxError: invalid syntax
>>> def setitem(listtochange,n,value):  ## function to overcome limitation
    listtochange[n]=value
    return value

>>> [setitem(a,i,'x') for i in range(0,len(a),2)] ## proving the function
['x', 'x', 'x']
>>> a 
['x', 1, 'x', 3, 'x']   # We did assignment anyway
>>> a=range(5)
>>> [a[i] for i in range(0,len(a),2)] ## list comprehension for side effects
[0, 2, 4]
>>> a
[0, 1, 2, 3, 4]
>>> [a[i]=3 for i in range(0,len(a),2)] ## try to do assignment
SyntaxError: invalid syntax
>>> def setitem(listtochange,n,value):  ## function to overcome limitation
    listtochange[n]=value
    return value

>>> [setitem(a,i,'x') for i in range(0,len(a),2)] ## proving the function
['x', 'x', 'x']
>>> a 
['x', 1, 'x', 3, 'x']   # We did assignment anyway

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷清清 2024-09-19 03:54:04

不要使用列表推导式来执行副作用——这不是Pythonic。使用显式循环代替:

for i in range(0,len(a),2):
    a[i] = 3

除了列表推导式中令人惊讶和意外的副作用之外,您正在构建一个您从未使用过的结果列表,这在这里是浪费且完全不必要的。

Don't use list comprehensions to perform side-effects - that is not Pythonic. Use an explicit loop instead:

for i in range(0,len(a),2):
    a[i] = 3

Apart the side-effects in list comprehensions being surprising and unexpected, you are constructing a result list that you never use which is wasteful and completely unnecessary here.

二手情话 2024-09-19 03:54:04

是的。我建议使用

a[::2] = ['x'] * len(a[::2])

它。


编辑:

Python 2.6 的微基准:

~:249$ python2.6 -m timeit -s 'a = range(2000)' 'a[::2] = [8] * len(a[::2])'
10000 loops, best of 3: 26.2 usec per loop

~:250$ python2.6 -m timeit -s 'a = range(2000)' 'a[::2] = [8] * (len(a)/2)'
10000 loops, best of 3: 19.6 usec per loop

~:251$ python2.6 -m timeit -s 'a = range(2000)' 'for i in xrange(0,len(a),2): a[i] = 8'
10000 loops, best of 3: 92.1 usec per loop

~:252$ python2.6 -m timeit -s 'a = range(2000)
> def assign(x,i,v):x[i]=v;return v' '[assign(a,i,8) for i in xrange(0, len(a), 2)]'
1000 loops, best of 3: 336 usec per loop

Python 3.1:

~:253$ python3.1 -m timeit -s 'a = list(range(2000))' 'a[::2] = [8] * len(a[::2])'
100000 loops, best of 3: 19.8 usec per loop

~:254$ python3.1 -m timeit -s 'a = list(range(2000))' 'a[::2] = [8] * (len(a)//2)'
100000 loops, best of 3: 13.4 usec per loop

~:255$ python3.1 -m timeit -s 'a = list(range(2000))' 'for i in range(0,len(a),2): a[i] = 8'
10000 loops, best of 3: 119 usec per loop

~:256$ python3.1 -m timeit -s 'a = list(range(2000))
> def assign(x,i,v):x[i]=v;return v' '[assign(a,i,8) for i in range(0, len(a), 2)]'
1000 loops, best of 3: 361 usec per loop

Yes. And I recommend using

a[::2] = ['x'] * len(a[::2])

instead.


Edit:

Microbenchmarks for Python 2.6:

~:249$ python2.6 -m timeit -s 'a = range(2000)' 'a[::2] = [8] * len(a[::2])'
10000 loops, best of 3: 26.2 usec per loop

~:250$ python2.6 -m timeit -s 'a = range(2000)' 'a[::2] = [8] * (len(a)/2)'
10000 loops, best of 3: 19.6 usec per loop

~:251$ python2.6 -m timeit -s 'a = range(2000)' 'for i in xrange(0,len(a),2): a[i] = 8'
10000 loops, best of 3: 92.1 usec per loop

~:252$ python2.6 -m timeit -s 'a = range(2000)
> def assign(x,i,v):x[i]=v;return v' '[assign(a,i,8) for i in xrange(0, len(a), 2)]'
1000 loops, best of 3: 336 usec per loop

Python 3.1:

~:253$ python3.1 -m timeit -s 'a = list(range(2000))' 'a[::2] = [8] * len(a[::2])'
100000 loops, best of 3: 19.8 usec per loop

~:254$ python3.1 -m timeit -s 'a = list(range(2000))' 'a[::2] = [8] * (len(a)//2)'
100000 loops, best of 3: 13.4 usec per loop

~:255$ python3.1 -m timeit -s 'a = list(range(2000))' 'for i in range(0,len(a),2): a[i] = 8'
10000 loops, best of 3: 119 usec per loop

~:256$ python3.1 -m timeit -s 'a = list(range(2000))
> def assign(x,i,v):x[i]=v;return v' '[assign(a,i,8) for i in range(0, len(a), 2)]'
1000 loops, best of 3: 361 usec per loop
画离情绘悲伤 2024-09-19 03:54:04

您还可以使用 list.__setitem__

a = range(5)
[a.__setitem__(i,"x") for i in range(0,len(a),2)]

或者,如果您想避免构建中间列表:

any(a.__setitem__(i,"x") for i in range(0,len(a),2))

但是列表推导式中的赋值确实是unpythonic。

You can also use list.__setitem__

a = range(5)
[a.__setitem__(i,"x") for i in range(0,len(a),2)]

Or if you want to avoid the contruction of an intermediate list:

any(a.__setitem__(i,"x") for i in range(0,len(a),2))

But assignment in list comprehensions is indeed unpythonic.

留一抹残留的笑 2024-09-19 03:54:04

对于我提到的时间安排(另请参阅通过递归公式改进纯Python素数筛)
from time import Clock

def rwh_primes1(n):
    # https://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python/3035188#3035188
    """ Returns  a list of primes < n """
    sieve = [True] * (n//2)
    for i in xrange(3,int(n**0.5)+1,2):
        if sieve[i//2]:
            sieve[i*i//2::i] = [False] * ((n-i*i-1)//(2*i)+1)
    return [2] + [2*i+1 for i in xrange(1,n/2) if sieve[i]]

def rwh_primes_tjv(n):
    # recurrence formula for length by amount1 and amount2 tjv
    """ Returns  a list of primes < n """
    sieve = [True] * (n//2)
    amount1 = n-10
    amount2 = 6

    for i in range(3,int(n**0.5)+1,2):
        if sieve[i//2]:
             ## can you make recurrence formula for whole reciprocal?
            sieve[i*i//2::i] = [False] * (amount1//amount2+1)
        amount1-=4*i+4
        amount2+=4

    return [2] + [2*i+1 for i in xrange(1,n//2) if sieve[i]]

def rwh_primes_len(n):
    """ Returns  a list of primes < n """
    sieve = [True] * (n//2)

    for i in range(3,int(n**0.5)+1,2):
        if sieve[i//2]:
            sieve[i*i//2::i] = [False] * len(sieve[i*i//2::i])

    return [2] + [2*i+1 for i in xrange(1,n//2) if sieve[i]]

def rwh_primes_any(n):
    """ Returns  a list of primes < n """
    halfn=n//2
    sieve = [True] * (halfn)

    for i in range(3,int(n**0.5)+1,2):
        if sieve[i//2]:
            any(sieve.__setitem__(item,False) for item in range(i*i//2,halfn,i))

    return [2] + [2*i+1 for i in xrange(1,n//2) if sieve[i]]


if __name__ == "__main__":
    n = 1000000

    print("rwh sieve1")
    t=clock()
    r=rwh_primes1(n)
    print("Length %i,  %s ms" %(len(r),1000*(clock()-t)))

    print("rwh sieve with recurrence formula")
    t=clock()
    r=rwh_primes_tjv(n)
    print("Length %i,  %s ms" %(len(r),1000*(clock()-t)))

    print("rwh sieve with len function")
    t=clock()
    r=rwh_primes_len(n)
    print("Length %i,  %s ms" %(len(r),1000*(clock()-t)))

    print("rwh sieve with any with side effects")
    t=clock()
    r=rwh_primes_any(n)
    print("Length %i,  %s ms" %(len(r),1000*(clock()-t)))
    raw_input('Ready')

""" Output:
rwh sieve1
Length 78498,  213.199442946 ms
rwh sieve with recurrence formula
Length 78498,  218.34143725 ms
rwh sieve with len function
Length 78498,  257.80008353 ms
rwh sieve with any with side effects
Length 78498,  829.977273648 ms
Ready
"""

Length 函数和所有带有 setitem 的函数都不是令人满意的替代方案,但这里的计时可以证明这一点。

具有len功能的rwh筛子
长度 78498,257.80008353 ms

rwh 筛,any 有副作用
长度 78498,829.977273648 毫秒

For my timing mentioned (see also the Improving pure Python prime sieve by recurrence formula)
from time import clock

def rwh_primes1(n):
    # https://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python/3035188#3035188
    """ Returns  a list of primes < n """
    sieve = [True] * (n//2)
    for i in xrange(3,int(n**0.5)+1,2):
        if sieve[i//2]:
            sieve[i*i//2::i] = [False] * ((n-i*i-1)//(2*i)+1)
    return [2] + [2*i+1 for i in xrange(1,n/2) if sieve[i]]

def rwh_primes_tjv(n):
    # recurrence formula for length by amount1 and amount2 tjv
    """ Returns  a list of primes < n """
    sieve = [True] * (n//2)
    amount1 = n-10
    amount2 = 6

    for i in range(3,int(n**0.5)+1,2):
        if sieve[i//2]:
             ## can you make recurrence formula for whole reciprocal?
            sieve[i*i//2::i] = [False] * (amount1//amount2+1)
        amount1-=4*i+4
        amount2+=4

    return [2] + [2*i+1 for i in xrange(1,n//2) if sieve[i]]

def rwh_primes_len(n):
    """ Returns  a list of primes < n """
    sieve = [True] * (n//2)

    for i in range(3,int(n**0.5)+1,2):
        if sieve[i//2]:
            sieve[i*i//2::i] = [False] * len(sieve[i*i//2::i])

    return [2] + [2*i+1 for i in xrange(1,n//2) if sieve[i]]

def rwh_primes_any(n):
    """ Returns  a list of primes < n """
    halfn=n//2
    sieve = [True] * (halfn)

    for i in range(3,int(n**0.5)+1,2):
        if sieve[i//2]:
            any(sieve.__setitem__(item,False) for item in range(i*i//2,halfn,i))

    return [2] + [2*i+1 for i in xrange(1,n//2) if sieve[i]]


if __name__ == "__main__":
    n = 1000000

    print("rwh sieve1")
    t=clock()
    r=rwh_primes1(n)
    print("Length %i,  %s ms" %(len(r),1000*(clock()-t)))

    print("rwh sieve with recurrence formula")
    t=clock()
    r=rwh_primes_tjv(n)
    print("Length %i,  %s ms" %(len(r),1000*(clock()-t)))

    print("rwh sieve with len function")
    t=clock()
    r=rwh_primes_len(n)
    print("Length %i,  %s ms" %(len(r),1000*(clock()-t)))

    print("rwh sieve with any with side effects")
    t=clock()
    r=rwh_primes_any(n)
    print("Length %i,  %s ms" %(len(r),1000*(clock()-t)))
    raw_input('Ready')

""" Output:
rwh sieve1
Length 78498,  213.199442946 ms
rwh sieve with recurrence formula
Length 78498,  218.34143725 ms
rwh sieve with len function
Length 78498,  257.80008353 ms
rwh sieve with any with side effects
Length 78498,  829.977273648 ms
Ready
"""

Length function and all with setitem are not satisfactory alternatives, but the timings are here to demonstrate it.

rwh sieve with len function
Length 78498, 257.80008353 ms

rwh sieve with any with side effects
Length 78498, 829.977273648 ms

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文