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

迭代列表的 n 個連續(xù)元素(重疊)

Iterate over n successive elements of list (with overlapping)(迭代列表的 n 個連續(xù)元素(重疊))
本文介紹了迭代列表的 n 個連續(xù)元素(重疊)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

itertools python 模塊為迭代器實(shí)現(xiàn)了一些基本構(gòu)建塊.正如他們所說,它們形成了一個迭代器代數(shù)".我期待,但我找不到使用該模塊進(jìn)行以下迭代的簡潔方法.給定一個有序?qū)崝?shù)列表,例如

The itertools python module implements some basic building blocks for iterators. As they say, "they form an iterator algebra". I was expecting, but I could not find a succinctly way of doing the following iteration using the module. Given a list of ordered real numbers, for example

a = [1.0,1.5,2.0,2.5,3.0]

...返回一個新列表(或只是迭代)按一些 n 值分組,比如 2

... return a new list (or just iterate) grouping by some n value, say 2

b = [(1.0,1.5),(1.5,2.0),(2.0,2.5),(2.5,3.0)]

我發(fā)現(xiàn)這樣做的方法如下.首先將列表一分為二,帶有偶數(shù)和賠率索引:

The way I found of doing this was as follows. First split the list in two, with evens and odds indexes:

even, odds = a[::2], a[1::2]

然后構(gòu)造新列表:

b = [(even, odd) for even, odd in zip(evens, odds)]
b = sorted(b + [(odd, even) for even, odd in zip(evens[1:], odds)])

本質(zhì)上,它類似于移動均值.

In essence, it is similar to a moving mean.

是否有簡潔的方法(使用或不使用 itertools)?

PS:

應(yīng)用程序

a 列表想象為實(shí)驗(yàn)期間發(fā)生的某些事件的時間戳集:

Imagine the a list as the set of timestamps of some events occurred during an experiment:

timestamp       event
47.8            1a
60.5            1b
67.4            2a
74.5            2b
78.5            1a
82.2            1b
89.5            2a
95.3            2b
101.7           1a
110.2           1b
121.9           2a
127.1           2b

...

此代碼用于根據(jù)不同的時間窗口對這些事件進(jìn)行分段.現(xiàn)在我對 2 連續(xù)事件之間的數(shù)據(jù)感興趣;'n > 2' 僅用于探索目的.

This code is being used to segment those events in accord with different temporal windows. Right now I am interested in the data between 2 successive events; 'n > 2' would be used only for exploratory purposes.

推薦答案

對于2,你可以這樣做

b = zip(a, a[1:])  # or list(zip(...)) on Python 3 if you really want a list

對于固定的 n,技術(shù)類似:

For fixed n, the technique is similar:

# n = 4
b = zip(a, a[1:], a[2:], a[3:])

對于變量 n,您可以壓縮可變數(shù)量的切片,或者(尤其是當(dāng)窗口大小接近 a 的大小時)您可以使用切片直接獲取窗口:

For variable n, you could zip a variable number of slices, or (especially if the window size is close to the size of a) you could use slicing to take windows directly:

b = zip(*[a[i:] for i in xrange(n)])
# or
b = [tuple(a[i:i+n]) for i in xrange(len(a)-n+1)]

如果 a 不是列表,您可以從 itertools 文檔中概括 pairwise 配方:

If a is not a list, you could generalize the pairwise recipe from the itertools docs:

import copy
import itertools

def nwise(iterable, n):
    # Make n tees at successive positions along the iterable.
    tees = list(itertools.tee(iterable, 1))
    for _ in xrange(n-1):
        tees.append(copy.copy(tees[-1]))
        next(tees[-1])

    return zip(*tees)

這篇關(guān)于迭代列表的 n 個連續(xù)元素(重疊)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 国产精品久久久久久久久久久免费看 | 99国产精品久久久久 | 欧美日韩成人在线 | 精品国产欧美日韩不卡在线观看 | 中文一区| 在线免费观看a级片 | 91久久精品一区二区二区 | 成人在线中文字幕 | 福利国产| 9久9久9久女女女九九九一九 | av电影一区 | 美女视频黄色的 | 成人在线精品 | 91久久北条麻妃一区二区三区 | 国产精品久久国产精品久久 | www.日韩欧美 | 最新av中文字幕 | 91在线视频免费观看 | 国产精品美女在线观看 | 天天综合天天 | 精品无码久久久久久国产 | 2019中文字幕视频 | 91精品久久久久久久久久入口 | 久热免费| 日韩av免费在线观看 | 欧美日韩一二区 | 国内自拍视频在线观看 | 国产精品久久久 | 日韩毛片 | 中文久久 | 国产精品一区二区三 | 亚洲高清久久 | 久久久夜夜夜 | 成人国产网站 | 国产一区二区三区 | 精品国产一区三区 | 天堂一区二区三区 | 国产精品久久精品 | 午夜精品久久久久久 | 国产一区二区三区精品久久久 | 国产高清精品在线 |