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

使用 sum() 連接元組

Concatenate tuples using sum()(使用 sum() 連接元組)
本文介紹了使用 sum() 連接元組的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

從這篇文章我了解到您可以使用 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!')

這看起來很不錯.但為什么這行得通?而且,這是最優的,還是 itertools 中的某些東西比這個結構更可取?

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中的加法運算符連接元組:

the addition operator concatenates tuples in python:

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

來自sum的文檔字符串:

返回一個開始"值(默認值:0)加上一個可迭代的數字

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

這意味著 sum 不是從可迭代的第一個元素開始,而是從通過 start= 參數傳遞的初始值開始.

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.

默認情況下 sum 與數字一起使用,因此默認起始值??為 0.因此,對一個可迭代的元組求和需要從一個空元組開始.() 是一個空元組:

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

因此工作串聯.

根據性能,這里是一個比較:

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

現在 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

因此,如果您的元組列表很小,您不必費心.如果它是中等大小或更大的,你應該使用 itertools.

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

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

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

相關文檔推薦

Sum values in a dict of lists(列表字典中的總和值)
How to sum numbers from a text file in Python(如何在 Python 中對文本文件中的數字求和)
What is a subtraction function that is similar to sum() for subtracting items in list?(什么是類似于 sum() 的減法函數,用于減去列表中的項目?)
Python equivalent of sum() using xor()(Python 等價于 sum() 使用 xor())
sum matrix columns in python(python中的求和矩陣列)
sum of N lists element-wise python(N個列表元素的總和python)
主站蜘蛛池模板: 日韩一区二区三区在线 | 中文字幕亚洲精品在线观看 | 欧美国产日韩一区 | 天天操操 | 欧美精品久久久 | 99成人精品 | 性一交一乱一伦视频免费观看 | 免费在线观看成人 | 欧美日韩三级 | 成人福利在线视频 | 日韩一区二区三区在线观看 | 久久精品免费看 | 黄色一级大片在线观看 | 亚洲国产精品一区二区三区 | 久久久国产精品网站 | 羞羞午夜 | 国产在线一区二区 | 三级成人片 | 亚洲最大av网站 | 日韩一区二区福利视频 | 国产成人亚洲精品自产在线 | 亚洲美女在线视频 | 国产精品一区二区视频 | 国产精品久久一区二区三区 | 91精品国产乱码久久蜜臀 | 91av在线免费 | 91精品国产综合久久久亚洲 | 国产9 9在线 | 中文 | 一区二区三区在线看 | 狠狠的操 | 精品欧美黑人一区二区三区 | 99久热在线精品视频观看 | 97精品超碰一区二区三区 | 国产精品久久久久久久久久免费看 | 亚洲 欧美 日韩在线 | 国产区一区二区三区 | 国产免费又黄又爽又刺激蜜月al | 少妇一区二区三区 | japanhd成人 | 欧美美女爱爱视频 | 国产亚洲精品久久久久动 |