久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 sum() 連接元組

Concatenate tuples using sum()(使用 sum() 連接元組)
本文介紹了使用 sum() 連接元組的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

從這篇文章我了解到您可以使用 sum() 連接元組:

From this post I learned that you can concatenate tuples with sum():

>>> tuples = (('hello',), ('these', 'are'), ('my', 'tuples!'))
>>> sum(tuples, ())
('hello', 'these', 'are', 'my', 'tuples!')

這看起來(lái)很不錯(cuò).但為什么這行得通?而且,這是最優(yōu)的,還是 itertools 中的某些東西比這個(gè)結(jié)構(gòu)更可取?

Which looks pretty nice. But why does this work? And, is this optimal, or is there something from itertools that would be preferable to this construct?

推薦答案

python中的加法運(yùn)算符連接元組:

the addition operator concatenates tuples in python:

('a', 'b')+('c', 'd')
Out[34]: ('a', 'b', 'c', 'd')

來(lái)自sum的文檔字符串:

返回一個(gè)開(kāi)始"值(默認(rèn)值:0)加上一個(gè)可迭代的數(shù)字

Return the sum of a 'start' value (default: 0) plus an iterable of numbers

這意味著 sum 不是從可迭代的第一個(gè)元素開(kāi)始,而是從通過(guò) start= 參數(shù)傳遞的初始值開(kāi)始.

It means sum doesn't start with the first element of your iterable, but rather with an initial value that is passed through start= argument.

默認(rèn)情況下 sum 與數(shù)字一起使用,因此默認(rèn)起始值??為 0.因此,對(duì)一個(gè)可迭代的元組求和需要從一個(gè)空元組開(kāi)始.() 是一個(gè)空元組:

By default sum is used with numeric thus the default start value is 0. So summing an iterable of tuples requires to start with an empty tuple. () is an empty tuple:

type(())
Out[36]: tuple

因此工作串聯(lián).

根據(jù)性能,這里是一個(gè)比較:

As per performance, here is a comparison:

%timeit sum(tuples, ())
The slowest run took 9.40 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 285 ns per loop


%timeit tuple(it.chain.from_iterable(tuples))
The slowest run took 5.00 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 625 ns per loop

現(xiàn)在 t2 的大小為 10000:

Now with t2 of a size 10000:

%timeit sum(t2, ())
10 loops, best of 3: 188 ms per loop

%timeit tuple(it.chain.from_iterable(t2))
1000 loops, best of 3: 526 μs per loop

因此,如果您的元組列表很小,您不必費(fèi)心.如果它是中等大小或更大的,你應(yīng)該使用 itertools.

So if your list of tuples is small, you don't bother. If it's medium size or larger, you should use itertools.

這篇關(guān)于使用 sum() 連接元組的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Sum values in a dict of lists(列表字典中的總和值)
How to sum numbers from a text file in Python(如何在 Python 中對(duì)文本文件中的數(shù)字求和)
What is a subtraction function that is similar to sum() for subtracting items in list?(什么是類似于 sum() 的減法函數(shù),用于減去列表中的項(xiàng)目?)
Python equivalent of sum() using xor()(Python 等價(jià)于 sum() 使用 xor())
sum matrix columns in python(python中的求和矩陣列)
sum of N lists element-wise python(N個(gè)列表元素的總和python)
主站蜘蛛池模板: 91tv国产成人福利 | 一区二区三区精品 | 好吊日在线视频 | 欧美日韩第一区 | 一本久久道| www.四虎.com| 亚洲专区在线 | 国产剧情在线 | 中国久久久 | 国产免费视频 | 夜夜嗨av一区二区三区网页 | 日本人の夫妇交换 | 免费成人深夜夜国外 | 精品国产毛片 | 日韩中文字幕 | 九九九精品视频 | 中文在线免费观看 | 最近中文字幕在线 | 亚洲国产欧美日韩 | 免费在线毛片 | 91成人看片| 日韩一区二区免费视频 | 成人久久av| 国产精品成人免费精品自在线观看 | 一级肉体裸体bbbb | 欧美日韩亚洲一区二区三区 | 性爱一级视频 | 日本天堂在线 | 一区二区精品视频 | 三级视频在线观看 | a毛片在线观看 | 黄色三级视频在线观看 | 一级大片免费看 | 日本中文在线观看 | 欧美日韩一区二区三区视频 | 日韩精品网 | 亚洲一区二区久久 | 天堂资源 | 天天干天天操天天干 | 中文字幕在线免费观看视频 | 国产精品成人免费精品自在线观看 |