99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀Python統(tǒng)計列表中的重復(fù)項出現(xiàn)的次數(shù)的方法
Python統(tǒng)計列表中的重復(fù)項出現(xiàn)的次數(shù)的方法
2017-12-14
收藏

Python統(tǒng)計列表中的重復(fù)項出現(xiàn)的次數(shù)的方法

本文以實例形式詳細講述了Python列表list數(shù)組array用法。分享給大家供大家參考。具體如下:
Python中的列表(list)類似于C#中的可變數(shù)組(ArrayList),用于順序存儲結(jié)構(gòu)。
創(chuàng)建列表
 代碼如下:
sample_list = ['a',1,('a','b')]
Python 列表操作
代碼如下:
sample_list = ['a','b',0,1,3]
得到列表中的某一個值
代碼如下:
value_start = sample_list[0]
end_value = sample_list[-1]
刪除列表的第一個值
代碼如下:
del sample_list[0]
在列表中插入一個值
代碼如下:
sample_list[0:0] = ['sample value']
得到列表的長度
代碼如下:
list_length = len(sample_list)
列表遍歷
代碼如下:
for element in sample_list:
print(element)
Python 列表高級操作/技巧
產(chǎn)生一個數(shù)值遞增列表
代碼如下:
num_inc_list = range(30)
#will return a list [0,1,2,...,29]
用某個固定值初始化列表
代碼如下:
initial_value = 0
list_length = 5
sample_list = [ initial_value for i in range(10)]
sample_list = [initial_value]*list_length
# sample_list ==[0,0,0,0,0]

附:python內(nèi)置類型'

1、list:列表 (即動態(tài)數(shù)組,C++標準庫的vector,但可含不同類型的元素于一個list中)
代碼如下:
a = ["I","you","he","she"]      #元素可為任何類型。

 
下標:按下標讀寫,就當作數(shù)組處理
以0開始,有負下標的使用
0第一個元素,-1最后一個元素,
-len第一個元 素,len-1最后一個元素

取list的元素數(shù)量
代碼如下:
len(list)   #list的長度。實際該方法是調(diào)用了此對象的__len__(self)方法。
創(chuàng)建連續(xù)的list
代碼如下:
L = range(1,5)      #即 L=[1,2,3,4],不含最后一個元素
L = range(1, 10, 2) #即 L=[1, 3, 5, 7, 9]
 
list的方法
代碼如下:
L.append(var)   #追加元素
L.insert(index,var)
L.pop(var)      #返回最后一個元素,并從list中刪除之
L.remove(var)   #刪除第一次出現(xiàn)的該元素
L.count(var)    #該元素在列表中出現(xiàn)的個數(shù)
L.index(var)    #該元素的位置,無則拋異常
L.extend(list)  #追加list,即合并list到L上
L.sort()        #排序
L.reverse()     #倒序

list 操作符:,+,*,關(guān)鍵字del
代碼如下:
a[1:]       #片段操作符,用于子list的提取
[1,2]+[3,4] #為[1,2,3,4]。同extend()
[2]*4       #為[2,2,2,2]
del L[1]    #刪除指定下標的元素
del L[1:3]  #刪除指定下標范圍的元素

list的復(fù)制
代碼如下:
L1 = L      #L1為L的別名,用C來說就是指針地址相同,對L1操作即對L操作。函數(shù)參數(shù)就是這樣傳遞的
L1 = L[:]   #L1為L的克隆,即另一個拷貝。
list comprehension
[ <expr1> for k in L if <expr2> ]

本文實例展示了Python統(tǒng)計列表中的重復(fù)項出現(xiàn)的次數(shù)的方法,是一個很實用的功能,適合Python初學(xué)者學(xué)習(xí)借鑒。具體方法如下:

對一個列表,比如[1,2,2,2,2,3,3,3,4,4,4,4],現(xiàn)在我們需要統(tǒng)計這個列表里的重復(fù)項,并且重復(fù)了幾次也要統(tǒng)計出來。
方法1:    
mylist = [1,2,2,2,2,3,3,3,4,4,4,4]
myset = set(mylist)  #myset是另外一個列表,里面的內(nèi)容是mylist里面的無重復(fù) 項
for item in myset:
  print("the %d has found %d" %(item,mylist.count(item)))

方法2:    
List=[1,2,2,2,2,3,3,3,4,4,4,4]
a = {}
for i in List:
  if List.count(i)>1:
    a[i] = List.count(i)
print (a)

利用字典的特性來實現(xiàn)。

方法3:
    
>>> from collections import Counter
>>> Counter([1,2,2,2,2,3,3,3,4,4,4,4])
Counter({1: 5, 2: 3, 3: 2})

這里再增補一個只用列表實現(xiàn)的方法:    
l=[1,4,2,4,2,2,5,2,6,3,3,6,3,6,6,3,3,3,7,8,9,8,7,0,7,1,2,4,7,8,9]
 
count_times = []
for i in l :
  count_times.append(l.count(i))
 
m = max(count_times)
n = l.index(m)
 
print (l[n])

其實現(xiàn)原理就是把列表中的每一個數(shù)出現(xiàn)的次數(shù)在其對應(yīng)的位置記錄下來,然后用max求出出現(xiàn)次數(shù)最多的位置。
只用這段代碼的話,有一個缺點,如果有多個結(jié)果,最后的現(xiàn)實的結(jié)果只是出現(xiàn)在最左邊的那一個,不過解決方法也很簡單

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }