字典:
|
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
|
#可以用大括号创建字典,也可以用工厂函数创建;
cleese
=
{}
palin
=
dict
()
#给字典加入一些数据
cleese[
'Name'
]
=
'John Cleese'
cleese[
'Occupations'
]
=
[
'actor'
,
'comedian'
,
'writer'
,]
#查看里面有哪些数据项
In [
9
]: cleese
Out[
9
]: {
'Name'
:
'John Cleese'
,
'Occupations'
: [
'actor'
,
'comedian'
,
'writer'
]}
palin
=
{
'Name'
:
'Michael Palin'
,
'Occupations'
:[
'comedian'
,
'actor'
,
'writer'
,
'tv'
]}
In [
11
]: palin
Out[
11
]: {
'Name'
:
'Michael Palin'
,
'Occupations'
: [
'comedian'
,
'actor'
,
'writer'
,
'tv'
]}
#可以通过键来调用对应的数据
In [
13
]: palin[
'Name'
]
Out[
13
]:
'Michael Palin'
#如果字典中一个键对应着多个数据项,也可以使用类似列表的记号访问。
In [
15
]: palin[
'Occupations'
][
-
1
]
Out[
15
]:
'tv'
In [
16
]: palin[
'Occupations'
][
1
]
Out[
16
]:
'actor'
In [
17
]: palin[
'Occupations'
][
0
]
Out[
17
]:
'comedian'
|
一、对以下数据做处理,输出保留人名和比赛数据排序后的前三项。
Sarah Sweeney,2002-6-17,2:58,2.58,2:39,2-25,2-25,2:54,2.18,2:55,2:55
第一版代码:
|
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
|
#!/usr/local/python3/bin/python3
def
sanitize(time_string):
if
'-'
in
time_string:
splitter
=
'-'
elif
':'
in
time_string:
splitter
=
':'
else
:
return
(time_string)
(mins,secs)
=
time_string.split(splitter)
return
(mins
+
'.'
+
secs)
def
get_file_data(filename):
try
:
with
open
(filename) as f:
data
=
f.readline()
return
(data.strip().split(
','
))
except
IOError as ioerr:
print
(
'File error'
+
str
(ioerr))
return
(
None
)
sarah1
=
get_file_data(
'sarah2'
)
#这里是将列表中前两项数据,人名和生日使用pop弹出到sarah_name,sarah_dob两个变量中。
(sarah_name,sarah_dob)
=
sarah1.pop(
0
),sarah1.pop(
0
)
#这里要做字符串拼接,所以后面的序列处理完之后,需要使用str()转换成字符串。
print
(sarah_name
+
"'s fastest times are:"
+
str
(
sorted
(
set
([ sanitize(i)
for
i
in
sarah1 ]))[
0
:
3
]))
|
输出结果:
Sarah Sweeney's fastest times are:['2.18', '2.25', '2.39']
二、上面定义的函数不变,我们使用字典的方式来完成
第二版代码:
|
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
|
#!/usr/local/python3/bin/python3
def
sanitize(time_string):
if
'-'
in
time_string:
splitter
=
'-'
elif
':'
in
time_string:
splitter
=
':'
else
:
return
(time_string)
(mins,secs)
=
time_string.split(splitter)
return
(mins
+
'.'
+
secs)
def
get_file_data(filename):
try
:
with
open
(filename) as f:
data
=
f.readline()
return
(data.strip().split(
','
))
except
IOError as ioerr:
print
(
'File error'
+
str
(ioerr))
return
(
None
)
sarah1
=
get_file_data(
'sarah2'
)
#定义字典
sarah_dic
=
dict
()
#将列表中前两个数据项,弹出保存到字典对应的键上。
sarah_dic[
'name'
]
=
sarah1.pop(
0
)
sarah_dic[
'dob'
]
=
sarah1.pop(
0
)
#姓名和日期都弹出了,sarah1里面剩下的就是时间数据了,保存在sarah_dic字典中,键为time;
sarah_dic[
'time'
]
=
sarah1
print
(sarah_dic[
'name'
]
+
"'s fastest time are: "
+
str
(
sorted
(
set
([sanitize(i)
for
i
in
sarah1]))[
0
:
3
]))
|
输出结果与上面相同
三、把字典的创建移到get_file_data() 函数中,返回一个字典而不是列表。 并且把数据切片,去重复项,排序也移到get_file_data函数中,调用函数完成4个选手的成绩输出。
第三版代码:
|
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
|
#!/usr/local/python3/bin/python3
def
sanitize(time_string):
if
'-'
in
time_string:
splitter
=
'-'
elif
':'
in
time_string:
splitter
=
':'
else
:
return
(time_string)
(mins,secs)
=
time_string.split(splitter)
return
(mins
+
'.'
+
secs)
def
get_file_data(filename):
try
:
with
open
(filename) as f:
data
=
f.readline()
templ
=
data.strip().split(
','
)
return
({
'name'
:templ.pop(
0
),
'dob'
:templ.pop(
0
),
'time'
:
str
(
sorted
(
set
([sanitize(i)
for
i
in
templ]))[
0
:
3
])})
except
IOError as ioerr:
print
(
'File error'
+
str
(ioerr))
return
(
None
)
james1
=
get_file_data(
'james2'
)
julie1
=
get_file_data(
'julie2'
)
mikey1
=
get_file_data(
'mikey2'
)
sarah1
=
get_file_data(
'sarah2'
)
print
(james1[
'name'
]
+
"'s fastest time are: "
+
james1['time'])
print
(julie1[
'name'
]
+
"'s fastest time are: "
+
julie1['time'])
print
(mikey1[
'name'
]
+
"'s fastest time are: "
+
mikey1['time'])
print
(sarah1[
'name'
]
+
"'s fastest time are: "
+
sarah1['time'])
|
输出结果:
难点:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
def
get_file_data(filename):
try
:
with
open
(filename) as f:
data
=
f.readline()
templ
=
data.strip().split(
','
)
#可以看到这里是返回字典了,发现连字典名都没有,直接返回的是键和对应的数据。
return
({
'name'
:templ.pop(
0
),
'dob'
:templ.pop(
0
),
'time'
:
str
(
sorted
(
set
([sanitize(i)
for
i
in
templ]))[
0
:
3
])})
except
IOError as ioerr:
print
(
'File error'
+
str
(ioerr))
return
(
None
)
#由于返回过来的直接是字典数据,这里用任何的变量调函数,都会变成字典,而函数返回键值对应的数据就保存在该字典中。
james1
=
get_file_data(
'james2'
)
#这里就可以使用字典和键"james1['name']"来输出数据了。
print
(james1[
'name'
]
+
"'s fastest time are: "
+
james1['time'])
|
引入Class代替字典重构程序:
|
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
|
#!/usr/local/python3/bin/python3
#
def
sanitize(time_string):
if
'-'
in
time_string:
splitter
=
'-'
elif
':'
in
time_string:
splitter
=
':'
else
:
return
(time_string)
(mins,secs)
=
time_string.split(splitter)
return
(mins
+
'.'
+
secs)
class
Athlete():
def
__init__(
self
,a_name,a_dob,a_times
=
[]):
self
.name
=
a_name
self
.dob
=
a_dob
self
.time
=
a_times
def
top3(
self
):
return
(
sorted
(
set
(sanitize(i)
for
i
in
self
.time))[
0
:
3
])
def
get_file_data(filename):
try
:
with
open
(filename) as f:
data
=
f.readline()
templ
=
data.strip().split(
','
)
#这里是直接返回类,并且把类可以被传入的参数一并返回。
return
(Athlete(templ.pop(
0
),templ.pop(
0
),templ))
except
IOError as ioerr:
print
(
'File error'
+
str
(ioerr))
return
(
None
)
#由于函数直接返回的是类,这里用任何变量,都会成为返回类的实例化对象。
james1
=
get_file_data(
'james2'
)
julie1
=
get_file_data(
'julie2'
)
mikey1
=
get_file_data(
'mikey2'
)
sarah1
=
get_file_data(
'sarah2'
)
print
(james1.name
+
"'s fastest time are: "
+
str
(james1.top3()))
print
(julie1.name
+
"'s fastest time are: "
+
str
(julie1.top3()))
print
(mikey1.name
+
"'s fastest time are: "
+
str
(mikey1.top3()))
print
(sarah1.name
+
"'s fastest time are: "
+
str
(sarah1.top3()))
|
#这里的print是在玩字符串拼接,所以str要把sarah1.top3()实例的方法输出,转换为字符串才能拼接。
本文转自qw87112 51CTO博客,原文链接:http://blog.51cto.com/tchuairen/1678551
