forked from foxli180/HeadFirstPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
11-Ch5exe1.py
120 lines (93 loc) · 3.02 KB
/
11-Ch5exe1.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
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
def sanitize(time_string): #将不同的时间分隔符统一为: .
if ':' in time_string:
splitter = ':'
elif '-' in time_string:
splitter = '-'
else:
return time_string
(misn,secs) = time_string.split(splitter)
return(misn+'.'+secs)
def uniquelist(old_list): #其实用set() 也可以解决问题
unique_list = []
for each_data in old_list:
if each_data not in unique_list:
unique_list.append(each_data)
return unique_list
def load_from_file(filename):
try:
with open(filename) as input_file:
data = input_file.readline()
return(data.strip().split(','))
except IOError as err:
print('File Error: '+str(err))
return (None)
james = []
julie = []
mikey = []
sarah = []
try:
with open('james.txt') as james_file:
data = james_file.readline()
james = data.strip().split(',')
except IOError as err:
print('File Error: '+str(err))
try:
with open('julie.txt') as julie_file:
data = julie_file.readline()
julie = data.strip().split(',')
except IOError as err:
print('File Error: '+str(err))
try:
with open('mikey.txt') as mikey_file:
data = mikey_file.readline()
mikey = data.strip().split(',')
except IOError as err:
print('File Error: '+str(err))
try:
with open('sarah.txt') as sarah_file:
data = sarah_file.readline()
sarah = data.strip().split(',')
except IOError as err:
print('File Error: '+str(err))
clean_james = []
clean_julie = []
clean_mikey = []
clean_sarah = []
#for each_t in james:
# clean_james.append(sanitize(each_t))
clean_james = sorted([sanitize(each_t) for each_t in james])
#for each_t in julie:
# clean_julie.append(sanitize(each_t))
clean_julie = sorted([sanitize(each_t) for each_t in julie])
#for each_t in mikey:
# clean_mikey.append(sanitize(each_t))
clean_mikey = sorted([sanitize(each_t) for each_t in mikey])
#for each_t in sarah:
# clean_sarah.append(sanitize(each_t))
clean_sarah = sorted([sanitize(each_t) for each_t in sarah])
#print (sorted(james))
#print (sorted(julie))
#print (sorted(mikey))
#print (sorted(sarah))
#print (sorted(clean_james))
#print (sorted(clean_julie))
#print (sorted(clean_mikey))
#print (sorted(clean_sarah))
print(clean_james)
print(clean_julie)
print(clean_mikey)
print(clean_sarah)
unique_james = []
unique_julie = []
unique_mikey = []
unique_sarah = []
unique_james = uniquelist(clean_james)
print(unique_james[0:3])
clean_james = sorted(set([sanitize(each_t) for each_t in james]))[0:3]
clean_julie = sorted(set([sanitize(each_t) for each_t in julie]))[0:3]
clean_mikey = sorted(set([sanitize(each_t) for each_t in mikey]))[0:3]
clean_sarah = sorted(set([sanitize(each_t) for each_t in sarah]))[0:3]
print(clean_james)
print(clean_julie)
print(clean_mikey)
print(clean_sarah)