-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexemple_4_OT.py
More file actions
146 lines (101 loc) · 3.23 KB
/
exemple_4_OT.py
File metadata and controls
146 lines (101 loc) · 3.23 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ouvrir un fichier data.txt
#lire les valeurs
#mettre les valeurs dans un dictionnaire
# V1 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#trier le dictionnaire par valeur
import re
#opening input file
file = open('data.txt', 'r')
#print(file.read())
#reading input file
List = file.read()
#regex declaration
regex = r"(.*),(.*)\n?"
#regex matches iterations
matches = re.finditer(regex, List)
#dictionary declaration
Dickus = dict()
#input values into dictionary
for matchNum, match in enumerate(matches):
matchNum = matchNum + 1
for groupNum in range(0, len(match.groups())):
if groupNum == 1 :
a = match.group(groupNum).rstrip().replace(" ","")
groupNum = groupNum + 1
#print("Group {groupNum} found: {group}".format(groupNum=groupNum, group=match.group(groupNum)))
if groupNum == 2:
b = match.group(groupNum).rstrip().replace(" ","")
print('a =',a,',b =',b)
Dickus[a] = int(b)
#print(Dickus)
#output sorted dictionary
print('Dictionary:',Dickus,'\nSorted Dictionary:',sorted(Dickus),'\n\n')
file.close()
# V2 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#opening input file
file = open('data.txt', 'r')
#reading input file
lines = file.readlines()
#dictionary
Dickus = dict()
#deleting \n
for x in range(len(lines)):
lines[x] = lines[x].rstrip().split(",")
for y in range(len(lines[x])):
lines[x][y] = lines[x][y].replace(" ","")
#filling dictionary
for x in range(len(lines)):
Dickus[lines[x][0]] = int(lines[x][1])
sortval = []
sortkey = []
#retrieving dictionnary values
for x in Dickus:
sortval.append(Dickus[x])
sortkey.append(x)
#sort by value
for x in range(len(sortval)):
for y in range(len(sortval)):
if sortval[x] < sortval[y]:
a = sortval[x]
b = sortkey[x]
sortval[x] = sortval[y]
sortval[y] = a
sortkey[x] = sortkey[y]
sortkey[y] = b
#output
print('Dictionary:', Dickus, '\nSorted by keys:', sorted(Dickus),'\nSorted by values',sortval)
print('\nby keys:')
for x in sorted(Dickus):
print(x,'=',Dickus[x])
print('\nby values:')
for x in range(len(sortval)):
print(sortval[x] , '=' , sortkey[x])
file.close()
# V3 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#opening input file
file = open('data.txt', 'r')
#reading input file
lines = file.readlines()
#dictionary
Dickus = dict()
#deleting \n
for x in range(len(lines)):
lines[x] = lines[x].rstrip().split(",")
for y in range(len(lines[x])):
lines[x][y] = lines[x][y].replace(" ","")
#filling dictionary
for x in range(len(lines)):
Dickus[lines[x][0]] = int(lines[x][1])
#sort by key
SKDickus = dict()
for x in sorted(Dickus):
SKDickus[x] = Dickus[x]
#sort by value
sortval = [(v,k) for k,v in Dickus.items()]
sortval.sort()
sortval = [(k,v) for v,k in sortval]
SVDickus = dict()
for x in range(len(sortval)):
SVDickus[sortval[x][0]] = sortval[x][1]
#output
print('\nDictionary:', Dickus, '\nSorted by keys:', SKDickus,'\nSorted by values',SVDickus)