有趣的定長數(shù)組

高并發(fā)訪問的時代,我們要得到商品或評論的Top 10 ,如果需要去通過查詢數(shù)據(jù)庫下愈,那么顯然是效率低下的。一個解決方案是在內(nèi)存中存放一個固定長度的數(shù)組寞忿,每當新數(shù)據(jù)從上面壓入驰唬, 就有相應數(shù)量的舊數(shù)據(jù)就從下面被頂出

常規(guī)辦法:利用numpy的Array構(gòu)建動態(tài)數(shù)組

import numpy as np

class DynamicArray(object):
    """基于np擴展一個固定長度的動態(tài)數(shù)組"""

    def __init__(self, length, name = None, item_type = float):
        self.name = name
        self._data = np.zeros(length, dtype=item_type)
        self._length = length
        self._dataSize = 0

    def append(self, value):
        """動態(tài)添加數(shù)據(jù)"""
        self._data[0:self._length - 1] = self._data[1:self._length]
        self._data[-1] = value
        self._dataSize += 1
        self._dataSize = min(self._dataSize, self._length)
 
   def getData(self):
        """獲取數(shù)據(jù)"""
        return self._data

其實,我們可以采用Python內(nèi)建的一個集合模塊

Deques是堆棧和隊列的泛化(名稱發(fā)音為“deck”腔彰,是“雙端隊列”的縮寫)叫编。 Deques支持線程安全,高效的內(nèi)存追加霹抛,并從雙側(cè)出現(xiàn)搓逾,并且在任一方向都具有大致相同的O(1) 性能。
雖然列表 list 對象也支持類似的操作杯拐,但是Deques針對快速固定長度操作進行了優(yōu)化霞篡,并且會導致pop(0)insert(0, v) 操作的O(n)內(nèi)存移動成本,這會改變底層數(shù)據(jù)表示的大小和位置端逼。如果沒有指定maxlen或者是None朗兵,deques可能會增長到任意長度。否則顶滩,deque被限制到指定的最大長度余掖。
一旦固定長度的deque已滿,當添加新項目時礁鲁,相應數(shù)量的項目將從相反的一端被丟棄盐欺。有界長度deques提供類似于Unix中尾部tail過濾器的功能。它們也可用于跟蹤只有最近的活動感興趣的數(shù)據(jù)池仅醇。 Deque對象支持以下方法:
append(x)
Add x to the right side of the deque.

appendleft(x)
Add x to the left side of the deque.

clear()
Remove all elements from the deque leaving it with length 0.

copy()
Create a shallow copy of the deque.
New in version 3.5.

count(x)
Count the number of deque elements equal to x.
New in version 3.2.

extend(iterable)
Extend the right side of the deque by appending elements from the iterable argument.

extendleft(iterable)
Extend the left side of the deque by appending elements from iterable. Note, the series of left appends results in reversing the order of elements in the iterable argument.

index(x[, start[, stop]])
Return the position of x in the deque (at or after index start and before index stop). Returns the first match or raises ValueError if not found.
New in version 3.5.

insert(i, x)
Insert x into the deque at position i.
If the insertion would cause a bounded deque to grow beyond maxlen, an IndexError is raised.
New in version 3.5.

pop()
Remove and return an element from the right side of the deque. If no elements are present, raises an IndexError.

popleft()
Remove and return an element from the left side of the deque. If no elements are present, raises an IndexError.

remove(value)
Remove the first occurrence of value. If not found, raises a ValueError.

reverse()
Reverse the elements of the deque in-place and then return None.
New in version 3.2.

rotate(n=1)
Rotate the deque n steps to the right. If n is negative, rotate to the left.
When the deque is not empty, rotating one step to the right is equivalent to d.appendleft(d.pop()), and rotating one step to the left is equivalent to d.append(d.popleft()).
Deque objects also provide one read-only attribute:

maxlen
Maximum size of a deque or None if unbounded.
New in version 3.1.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末冗美,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子析二,更是在濱河造成了極大的恐慌粉洼,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件叶摄,死亡現(xiàn)場離奇詭異属韧,居然都是意外死亡,警方通過查閱死者的電腦和手機准谚,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進店門挫剑,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人柱衔,你說我怎么就攤上這事樊破∮淅猓” “怎么了?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵哲戚,是天一觀的道長奔滑。 經(jīng)常有香客問我,道長顺少,這世上最難降的妖魔是什么朋其? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮脆炎,結(jié)果婚禮上梅猿,老公的妹妹穿的比我還像新娘。我一直安慰自己秒裕,他們只是感情好袱蚓,可當我...
    茶點故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著几蜻,像睡著了一般喇潘。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上梭稚,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天颖低,我揣著相機與錄音,去河邊找鬼弧烤。 笑死忱屑,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的扼褪。 我是一名探鬼主播想幻,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼粱栖,長吁一口氣:“原來是場噩夢啊……” “哼话浇!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起闹究,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤幔崖,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后渣淤,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體赏寇,經(jīng)...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年价认,在試婚紗的時候發(fā)現(xiàn)自己被綠了嗅定。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,977評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡用踩,死狀恐怖渠退,靈堂內(nèi)的尸體忽然破棺而出忙迁,到底是詐尸還是另有隱情,我是刑警寧澤碎乃,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布姊扔,位于F島的核電站,受9級特大地震影響梅誓,放射性物質(zhì)發(fā)生泄漏恰梢。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一梗掰、第九天 我趴在偏房一處隱蔽的房頂上張望嵌言。 院中可真熱鬧,春花似錦及穗、人聲如沸呀页。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蓬蝶。三九已至,卻和暖如春猜惋,著一層夾襖步出監(jiān)牢的瞬間丸氛,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工著摔, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留缓窜,地道東北人。 一個月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓谍咆,卻偏偏與公主長得像禾锤,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子摹察,可洞房花燭夜當晚...
    茶點故事閱讀 44,927評論 2 355

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,332評論 0 10
  • 有一年的南方 雪花飛揚 老人牽著小孩在雪地里搖晃 五點的街道嵌著零散星光 校門口的攤販一片繁忙 臃腫的棉服摩擦作響...
    胡子不歸閱讀 279評論 2 4