-
Notifications
You must be signed in to change notification settings - Fork 0
/
sum_dublicates.py
36 lines (28 loc) · 967 Bytes
/
sum_dublicates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import itertools
import sublime
import sublime_plugin
class SumDuplicatesCommand(sublime_plugin.TextCommand):
def run(self, edit, isint=False):
v = self.view
allcontent = sublime.Region(0, v.size())
lines = sorted(v.substr(allcontent).split('\n'))
o = ""
s = 0
d = {}
for line in lines:
k, v = line.split("\t")
if o != "" and o != k:
d[o] = s
s = 0
o = k
s += float(v)
d[o] = s
# dict输出,无排序
# output = "\n".join(k + "\t" + str(v) for k,v in d.items())
# 转tuple,有排序
d = sorted(d.items(), key=lambda x:x[1], reverse=True)
if isint:
output = '\n'.join([str(t[0]) + '\t' + str(int(t[1])) for t in d])
else:
output = '\n'.join([str(t[0]) + '\t' + str(t[1]) for t in d])
self.view.replace(edit, allcontent, output)