-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
40 lines (31 loc) · 1.32 KB
/
app.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
37
38
39
40
import operator
class Adjacency(object):
def __init__(self,object_Id,priority,distance,tier,attempts,hosr):
self.object_Id=int(object_Id)
self.priority=int(priority)
self.distance=int(distance)
self.tier=int(tier)
self.attempts=int(attempts)
self.hosr=float(hosr)
def __repr__(self):
return "{},{},{},{},{},{}\n".format(self.object_Id,self.priority,self.distance,self.tier,self.attempts,self.hosr)
with open('adjacencies.csv', 'r') as inputFile:
header = inputFile.readline()
adjacencyList = [x.strip().split(',') for x in inputFile.readlines()]
adjacencyList = [Adjacency(*x) for x in adjacencyList]
#Sort on first level
adjacencyList.sort(key = lambda x: x.priority)
#Sort on second level
secondLevelSort = zip(list({adjacency.priority for adjacency in adjacencyList}), \
['distance','tier','attempts','hosr'], \
[True,True,False,False])
finalAdjacencyList = []
for i in secondLevelSort:
finalAdjacencyList += (sorted(list(filter(lambda x: x.priority == i[0], adjacencyList)), \
key = lambda x: getattr(x, i[1]), \
reverse= i[2]))
#Write output
with open('result.csv', 'w') as outputFile:
outputFile.write(header)
for x in finalAdjacencyList:
outputFile.write(str(x))