-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_multi_dict.py
More file actions
46 lines (31 loc) · 1.18 KB
/
parse_multi_dict.py
File metadata and controls
46 lines (31 loc) · 1.18 KB
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
37
38
39
40
41
42
43
44
45
46
# For a specific-case project
CONST_ITEM_TYPES = ['itemA', 'itemB', 'itemC', 'itemD', 'itemE', 'itemF', 'itemG', 'itemH']
unsortedList, sortedList = [], []
def calculateTop(sortedList):
# Discard empty values and keep top 10 values
print(sortedList)
def sortList(sortedList):
# Sort all lists in outerList by the 2nd value in the tuple element
sortedList.sort(key=lambda l: l[-1][1], reverse=True)
print(sortedList)
calculateTop(sortedList)
def parseFile():
# 5000 items, 46ms
with open(r'dict_demo.txt', 'r') as data:
for line in data.readlines():
lineSplit = line.partition('.txt:')
fileId = lineSplit[0]
line = lineSplit[2]
line = line.replace(' ', ',', 1).replace(' ', '=', 1).strip()
listItem = [s.split('=')[1] for s in line.split(',')]
listItem.insert(0, fileId)
unsortedList.append(listItem)
data.close()
for index, innerList in enumerate(unsortedList):
# Add item indentifier to value as tuple (itemN, 8)
innerList[-1] = (CONST_ITEM_TYPES[index % 8], innerList[-1])
sortList(unsortedList)
def main():
parseFile()
exit(1)
main()