在python中查找多个重复项,计算重复项的数量和唯一以及第一个重复文本的索引

时间:2015-10-17 10:35:08

标签: python

请帮帮我。 我的文件看起来像这样:

This is a cat
we are working at BusinessBrio
Gitu is my beloved cat
Jery is also a cat
Boni is a nice dog
Gitu is my beloved cat
we are working at BusinessBrio
This is a cat
we are working at BusinessBrio
Gitu is my beloved cat
Jery is also a cat
Boni is a nice dog
Gitu is my beloved cat
we are working at BusinessBrio

我需要这样的输出:

[[1,'we are working at BusinessBrio',4],[2,'Gitu is my beloved cat',4],[0,'This is a cat',2],[3,'Jery is also a cat',2],[4,'Boni is a nice dog',2]]

输出的内容必须根据重复次数按降序排序

2 个答案:

答案 0 :(得分:0)

results.Text = CDbl("5.97427068015303E-10")
results.Text = CDbl(5.97427068015303E-10)

答案 1 :(得分:0)

使用Countersorted功能。

from collections import Counter

with open("hel.txt","r") as f:
    b=f.read().splitlines()  

counter=Counter(b)

output=[]

for key, value in counter.iteritems():
    lst=[]
    lst.append(b.index(key))
    lst.append(key)
    lst.append(value)
    output.append(lst)

out=sorted(output,key=lambda x:x[2],reverse=True)
print out

输出:

[[1, 'we are working at BusinessBrio', 4], [2, 'Gitu is my beloved cat', 4], [0, 'This is a cat', 2], [4, 'Boni is a nice dog', 2], [3, 'Jery is also a cat', 2]]