做爰高潮a片〈毛片〉,尤物av天堂一区二区在线观看,一本久久A久久精品VR综合,添女人荫蒂全部过程av

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

python統(tǒng)計一個文本中重復(fù)行數(shù)的方法

來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-27 14:31:29
文檔

python統(tǒng)計一個文本中重復(fù)行數(shù)的方法

python統(tǒng)計一個文本中重復(fù)行數(shù)的方法:本文實例講述了python統(tǒng)計一個文本中重復(fù)行數(shù)的方法。分享給大家供大家參考。具體實現(xiàn)方法如下: 比如有下面一個文件 2 3 1 2 我們期望得到 2,2 3,1 1,1 解決問題的思路: 出現(xiàn)的文本作為key, 出現(xiàn)的數(shù)目作為value,然后按照value排除后輸出 最好按照va
推薦度:
導(dǎo)讀python統(tǒng)計一個文本中重復(fù)行數(shù)的方法:本文實例講述了python統(tǒng)計一個文本中重復(fù)行數(shù)的方法。分享給大家供大家參考。具體實現(xiàn)方法如下: 比如有下面一個文件 2 3 1 2 我們期望得到 2,2 3,1 1,1 解決問題的思路: 出現(xiàn)的文本作為key, 出現(xiàn)的數(shù)目作為value,然后按照value排除后輸出 最好按照va

本文實例講述了python統(tǒng)計一個文本中重復(fù)行數(shù)的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:

比如有下面一個文件
2
3
1
2
我們期望得到
2,2
3,1
1,1

解決問題的思路:

出現(xiàn)的文本作為key, 出現(xiàn)的數(shù)目作為value,然后按照value排除后輸出
最好按照value從大到小輸出出來,可以參照:
代碼如下:

in recent Python 2.7, we have new OrderedDict type, which remembers the order in which the items were added.
>>> d = {"third": 3, "first": 1, "fourth": 4, "second": 2}
>>> for k, v in d.items():
... print "%s: %s" % (k, v)
...
second: 2
fourth: 4
third: 3
first: 1
>>> d
{'second': 2, 'fourth': 4, 'third': 3, 'first': 1}To make a new ordered dictionary from the original, sorting by the values:
>>> from collections import OrderedDict
>>> d_sorted_by_value = OrderedDict(sorted(d.items(), key=lambda x: x[1]))The OrderedDict behaves like a normal dict:
>>> for k, v in d_sorted_by_value.items():
... print "%s: %s" % (k, v)
...
first: 1
second: 2
third: 3
fourth: 4
>>> d_sorted_by_value
OrderedDict([('first': 1), ('second': 2), ('third': 3), ('fourth': 4)])


代碼如下:
代碼如下:

#coding=utf-8
import operator
f = open("f.txt")
count_dict = {}
for line in f.readlines():
line = line.strip()
count = count_dict.setdefault(line, 0)
count += 1
count_dict[line] = count
sorted_count_dict = sorted(count_dict.iteritems(), key=operator.itemgetter(1), reverse=True)
for item in sorted_count_dict:
print "%s,%d" % (item[0], item[1])

補充說明:
1. python的dict對象的兩個方法:

items方法將所有的字典項以列表的方式返回, 這些列表項中每一項都來自于(鍵, 值)
iteritems方法與items的作用大致相同, 但是返回一個迭代器對象而不是列表

2. python的內(nèi)建函數(shù)sorted
代碼如下:

>>> help(sorted)
Help on built-in function sorted in module __builtin__:
sorted(...)
sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list

希望本文所述對大家的Python程序設(shè)計有所幫助。

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

python統(tǒng)計一個文本中重復(fù)行數(shù)的方法

python統(tǒng)計一個文本中重復(fù)行數(shù)的方法:本文實例講述了python統(tǒng)計一個文本中重復(fù)行數(shù)的方法。分享給大家供大家參考。具體實現(xiàn)方法如下: 比如有下面一個文件 2 3 1 2 我們期望得到 2,2 3,1 1,1 解決問題的思路: 出現(xiàn)的文本作為key, 出現(xiàn)的數(shù)目作為value,然后按照value排除后輸出 最好按照va
推薦度:
標(biāo)簽: 文件 的方法 統(tǒng)計
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 太仆寺旗| 西乌| 齐齐哈尔市| 陇西县| 新丰县| 噶尔县| 尚志市| 曲阜市| 曲阜市| 通州市| 正镶白旗| 普陀区| 遂溪县| 荃湾区| 佛学| 郯城县| 兴化市| 石首市| 龙陵县| 天水市| 长阳| 新营市| 会东县| 扶风县| 昭苏县| 渝北区| 温州市| 洪泽县| 肇庆市| 泉州市| 顺义区| 定襄县| 大丰市| 新乐市| 布拖县| 福清市| 普定县| 嵊泗县| 安国市| 政和县| 永胜县|