python 實現(xiàn)在無序數(shù)組中找到中位數(shù)方法
一、問題描述
1、求一個無序數(shù)組的中位數(shù), (若數(shù)組是偶數(shù),則中位數(shù)是指中間兩個數(shù)字之和除以2,若數(shù)組是奇數(shù),則中位數(shù)是指最中間位置。要求:不能使用排序,時間復(fù)雜度盡量低
2、例如:
lists = [3, 2, 1, 4] , 中位數(shù)為 = (2+3)/2 = 2.5
lists = [3, 1, 2] , 中位數(shù)為 2
3、算法思想:
利用快速排序思想(但是并不是全部使用):任意挑選一個元素,以該元素為key, 劃分?jǐn)?shù)組為兩個部分,如果左側(cè)數(shù)組長度剛好為(n-1)/2, 那么key就為中位數(shù), 若左側(cè)數(shù)組長度 < (n-1)/2 , 那么中位數(shù)點在右側(cè),反之,中位數(shù)在左側(cè)。然后進入相應(yīng)的一側(cè)繼續(xù)尋找中位
平均時間復(fù)雜度為O(n)
二、程序
class Solution(object): def findmedian(self, lists): if not lists or len(lists) == 0: return [] n = len(lists) if n % 2 == 0: a = self.partition(lists, n/2, 0, n-1) b = self.partition(lists, n/2-1, 0, n-1) mid = (lists[a]+lists[b])/ (2 * 1.0) return mid else: mid = self.partition(lists, n/2, 0, n-1) return lists[mid] def partition(self, lists, k, start, end): key = lists[start] left, right = start, end while left < right: while left < right and lists[right] > key: right = right - 1 lists[left] = lists[right] while left < right and lists[left] < key: left = left + 1 lists[right] = lists[left] lists[left] = key if left == k: return left elif left > k: return self.partition(lists, k, start, left-1) else: return self.partition(lists, k, left+1, end) if __name__ == "__main__": sol = Solution() lists = [2, 5, 4, 9, 3, 6, 8, 7, 1] # lists = [1, 2] data = sol.findmedian(lists) print("中位數(shù) = %s" % data)
知識補充:python streaming 實現(xiàn)某個字段排序
一,hadoop streaming默認(rèn)情況
1,在hadoop streaming的默認(rèn)情況下,是以\t作為分隔符的,標(biāo)準(zhǔn)輸入時,每行的第一個\t之前的內(nèi)容作為key,第一個\t之后的內(nèi)容作為value。注意,如果一個\t字符都沒有,那么整行作為key。
2,streaming的一些參數(shù)如下:
-D stream.map.output.field.separator :設(shè)置map輸出中key和value的分隔符
-D stream.num.map.output.key.fields : 設(shè)置map程序分隔符的位置,該位置之前的部分作為key,之后的部分作為value
-D map.output.key.field.separator : 設(shè)置map輸出中key內(nèi)部的分割符
-D num.key.fields.for.partition : 指定分桶時,key按照分隔符切割后,其中用于分桶key所占的列數(shù)(配合-partitioner org.apache.hadoop.mapred.lib.KeyFieldBasedPartitioner 使用)
-D stream.reduce.output.field.separator:設(shè)置reduce輸出中key和value的分隔符
-D stream.num.reduce.output.key.fields:設(shè)置reduce程序分隔符的位置
二,python streaming 實現(xiàn)某個字段的排序
1, 輸入數(shù)據(jù): cat data.txt (中間是tab鍵)
11 2
11 3
11 4 1
11 111 12 22
2,streaming程序如下:
vim sorted.sh
#!/bin/bash export CURRENT=/home/chunhe.liao/hadoop_streaming/sort /usr/local/hadoop-2.6.3/bin/hadoop jar /usr/local/hadoop-2.6.3/share/hadoop/tools/lib/hadoop-streaming-2.6.3.jar \ -D stream.map.output.field.separator='\t' \ -D stream.num.map.output.key.fields=3 \ -D mapreduce.job.output.key.comparator.class=org.apache.hadoop.mapreduce.lib.partition.KeyFieldBasedComparator \ -D mapreduce.partition.keycomparator.options=-k3,3nr \ # 按照第三列逆序排列,可以根據(jù)想要的第幾段來選擇。 -input "/user/test/inputdata/datas3/data.txt" \ -output "/user/test/streaming/sorted_20180711" \ -mapper "python mapper.py" \ -reducer "python reducer.py" \ -file "$CURRENT/mapper.py" \ -file "$CURRENT/reducer.py"
(2) mapper.py
# -*- coding: utf-8 -*- import sys for line in sys.stdin: line = line.strip() print('{0}'.format(line))
(3) reducer.py
# -*- coding: utf-8 -*- import sys for line in sys.stdin: line = line.strip() print("{0}".format(line))
運行命令:
bash sorted.sh
運行結(jié)果:
hdfs dfs -cat /user/test/streaming/sorted_20180711/part-00000
11 12 22
11 3
11 2
11 4 1
11 1
以上這篇python 實現(xiàn)在無序數(shù)組中找到中位數(shù)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Python如何實現(xiàn)大型數(shù)組運算(使用NumPy)
- python如何建立全零數(shù)組
- Python替換NumPy數(shù)組中大于某個值的所有元素實例
- Python數(shù)組拼接np.concatenate實現(xiàn)過程
- Python 改變數(shù)組類型為uint8的實現(xiàn)
- python由已知數(shù)組快速生成新數(shù)組的方法
- python 工具 字符串轉(zhuǎn)numpy浮點數(shù)組的實現(xiàn)
- 在python3中實現(xiàn)查找數(shù)組中最接近與某值的元素操作
- python 實現(xiàn)多維數(shù)組(array)排序
- python對數(shù)組進行排序,并輸出排序后對應(yīng)的索引值方式
- Python如何讀寫二進制數(shù)組數(shù)據(jù)
相關(guān)文章
用Python和MD5實現(xiàn)網(wǎng)站掛馬檢測程序
系統(tǒng)管理員通常從svn/git中檢索代碼,部署站點后通常首先會生成該站點所有文件的MD5值,如果上線后網(wǎng)站頁面內(nèi)容被篡改(如掛馬)等,可以比對之前生成MD5值快速查找去那些文件被更改,為了使系統(tǒng)管理員第一時間發(fā)現(xiàn),可結(jié)合crontab或nagios等工具2014-03-03Python趣味挑戰(zhàn)之turtle庫繪畫飄落的銀杏樹
銀杏還是和恐龍同時代的植物,被稱為活化石,適應(yīng)能力強,生長期漫長,壽命可達千年.因此,銀杏是長壽的代表和象征,接下來用Python的turtle庫來繪畫銀杏樹唯美的一幕,需要的朋友可以參考下2021-05-05工程師必須了解的LRU緩存淘汰算法以及python實現(xiàn)過程
這篇文章主要介紹了工程師必須了解的LRU緩存淘汰算法以及python實現(xiàn)過程,幫助大家更好的學(xué)習(xí)算法數(shù)據(jù)結(jié)構(gòu),感興趣的朋友可以了解下2020-10-10