2013-02-17 119 views
3

我有一個整數的元組,例如(1, 2, 3, 4, 5),我想通過乘以相鄰元素來產生元組(1*2, 2*3, 3*4, 4*5)。是否可以用單線程來做到這一點?乘以相鄰元素

回答

10

Sh並且甜美。請記住zip只能運行最短的輸入。

print tuple(x*y for x,y in zip(t,t[1:])) 
+0

+1我知道我錯過了什麼! – Volatility 2013-02-17 01:15:29

+0

@nneonneo @Paul Manta不太好。 ''t [1:]''創建一個新的對象,''zip(t,t [1:])''更多地創建一個對象,並且''對於x,y in zip(t,t [1: ])'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''需要將壓縮對象的每個元素解壓到兩個標識符''x''和''''' – eyquem 2013-02-17 01:51:54

+0

如果對你很重要,元組開箱很便宜,避免它僅僅節省幾微秒是不值得的。 – nneonneo 2013-02-17 01:53:09

6
>>> t = (1, 2, 3, 4, 5) 
>>> print tuple(t[i]*t[i+1] for i in range(len(t)-1)) 
(2, 6, 12, 20) 

雖然不是最pythonic的解決方案。

+0

+1似乎不夠好。爲什麼它不是最Pythonic? – 2013-02-17 01:06:14

+0

@PaulManta [Case in point](http://stackoverflow.com/a/14917025/1907098) – Volatility 2013-02-17 01:18:00

+0

@Volatility請參閱我對Eugenie的回答的評論。 – eyquem 2013-02-17 01:46:56

3

如果t是你的元組:

>>> tuple(t[x]*t[x+1] for x in range(len(t)-1)) 
(2, 6, 12, 20) 

並與可愛的地圖另一種解決方案:

>>> tuple(map(lambda x,y:x*y, t[1:], t[:-1])) 
(2, 6, 12, 20) 

編輯: 如果你擔心片的額外內存消費,你可以使用來自itertools的islice,它將迭代你的元組(thx @eyquem):

>>> tuple(map(lambda x,y:x*y, islice(t, 1, None), islice(t, 0, len(t)-1))) 
(2, 6, 12, 20) 
+0

第一個是基本的好。第二個不太好,因爲''t [1:]''創建了一個全新的對象,''t [:1]''也創建了一個全新的對象:它耗時耗內存,這可能不方便如果元組非常大。 - 第一個解決方案是訪問現有元組的元素,索引訪問非常快(據我所知) – eyquem 2013-02-17 01:45:17

+0

@eyquem對於小元組,索引是所有(相對昂貴的)Python調用,而迭代可能涉及更多的C代碼。 (有關過早優化的提示等等)。如果你真的關心不復制元組,你可以使用'izip'和'islice'。 – nneonneo 2013-02-17 01:52:22

+0

@nneonneo索引元組很昂貴?我想知道更多 – eyquem 2013-02-17 01:58:01

2

我喜歡recipes from itertools

from itertools import izip, tee 

def pairwise(iterable): 
    xs, ys = tee(iterable) 
    next(ys) 
    return izip(xs, ys) 

print [a * b for a, b in pairwise(range(10))] 

結果:

[0, 2, 6, 12, 20, 30, 42, 56, 72] 
3
tu = (1, 2, 3, 4, 5) 

it = iter(tu).next 
it() 
print tuple(a*it() for a in tu) 

我計時各種代碼:

from random import choice 
from time import clock 
from itertools import izip 

tu = tuple(choice(range(0,87)) for i in xrange(2000)) 

A,B,C,D = [],[],[],[] 

for n in xrange(50): 

    rentime = 100 

    te = clock() 
    for ren in xrange(rentime): # indexing 
     tuple(tu[x]*tu[x+1] for x in range(len(tu)-1)) 
    A.append(clock()-te) 

    te = clock() 
    for ren in xrange(rentime): # zip 
     tuple(x*y for x,y in zip(tu,tu[1:])) 
    B.append(clock()-te) 

    te = clock() 
    for ren in xrange(rentime): #i ter 
     it = iter(tu).next 
     it() 
     tuple(a*it() for a in tu) 
    C.append(clock()-te) 

    te = clock() 
    for ren in xrange(rentime): # izip 
     tuple(x*y for x,y in izip(tu,tu[1:])) 
    D.append(clock()-te) 


print 'indexing ',min(A) 
print 'zip  ',min(B) 
print 'iter  ',min(C) 
print 'izip  ',min(D) 

結果

indexing 0.135054036197 
zip  0.134594201218 
iter  0.100380634969 
izip  0.0923947037962 

izip比ZIP更好: - 31%

我的解決方案並沒有那麼糟糕(我並不這麼認爲的方式):-25%相對於拉鍊,10%比冠軍izip更多的時間

我很驚訝,索引並不比拉鍊快:nneonneo是正確的,拉鍊是可以接受的

+0

當我在'line'元組(tu [x] * tu [x + 1])範圍內(len(tu)-1)''把'range'更改爲'xrange''並且將元組大小增加到200,000時,索引執行比我的機器上的郵編快30%。雖然,izip和iter是最快和非常接近的。 – Akavall 2013-02-17 03:38:06

+0

@Akavall @nneonneo哦,我是愚蠢的讓''範圍''。你是對的,阿卡瓦爾,它會改善。我和你一樣(20萬),我有16個用於「zip」,13個用於索引,另外13個用於「iter」,10個用於「izip」。元組越長,''zip'越糟糕。這意味着通過「zip」構造新對象非常重要。事實上''izip''仍然是最好的,這意味着拆包並不是很耗時。不過,10比13比現在只減少23%的時間,不是32或35%。 – eyquem 2013-02-17 05:32:46