python中怎么對列表以區(qū)間進行統(tǒng)計?
問題描述
python中怎么對列表以區(qū)間進行統(tǒng)計?假設list=[1,1,1,2,3,4,4,5,5,6,7,7,7,7,8,9,9,9,10……99,99,99,100,100]
怎么寫程序可以以10為一個區(qū)間分別統(tǒng)計,如統(tǒng)計出小于10的數(shù)字頻率,大于10小于20的頻率,大于20小于30的頻率……大于90小于100的頻率?抱歉題目描述的不好
問題解答
回答1:# code for python3from itertools import groupbylst = [1, 1, 1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 7, 7, 8, 9, 9, 9, 10, 99, 99, 99, 100, 100]dic = {}for k, g in groupby(lst, key=lambda x: (x-1)//10): dic[’{}-{}’.format(k*10+1, (k+1)*10)] = len(list(g)) print(dic)
結(jié)果:
{’91-100’: 5, ’1-10’: 19}
我回答過的問題: Python-QA
回答2:# coding: utf-8lst = [1, 1, 1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 7, 7, 8, 9, 9, 9, 10, 99, 99, 99, 100, 100]intervals = {’{0}-{1}’.format(10 * x + 1, 10 * (x + 1)): 0 for x in range(10)}for _ in lst: for interval in intervals:start, end = tuple(interval.split(’-’))if int(start) <= _ <= int(end): intervals[interval] += 1print intervals
相關文章:
1. PHP能實現(xiàn)百度網(wǎng)盤的自動化么?2. android百度地圖定位問題3. node.js - 運行node項目時,怎么用webpack的熱加載4. javascript - Express 和 request 如何代理遠程圖片?5. css3 - 頁面布局問題6. java - synchronized 關鍵字修飾方法,能不能繼承 ?7. 視頻 - html5 video的autoplay 在智能手機上不運作?8. css - 前后端交互問題!9. css3 - 如何使用css將select的邊框以及右邊的小三角形去掉?10. html - 如何用css令背景圖能夠撐滿本身會滾動的頁面?
