1.1字典的用法
sarah = []
sarahDic = {}
sarah = dataHandle2.readDataFromFile('sarah2.txt')
sarahDic['Name'] = sarah.pop(0)
sarahDic['Dob'] = sarah.pop(0)
sarahDic['runtimes'] = sarah
print(sarahDic['Name'] + "'s fatest times are:" + str(sorted(set(dataHandle2.sanitize(t) for t in sarahDic['runtimes']))[0:3]))
字典相较于列表,就是在列表的一些属性上,加上其称号。字典可以随着其加入的值动态扩大
2.关于类的继承
# auth = 'gao'
# encoding: utf-8
import dataHandle2
class AthleteList(list):
def __init__(self, a_name, a_dob=None, a_times=[]):
list.__init__([])
self.name = a_name
self.dob = a_dob
self.extend(a_times)
def top3(self):
return (sorted(set(dataHandle2.sanitize(t) for t in self))[0:3])
继承list的类,拥有list的所有共有方法和属性
3.对类的应用
sarah = dataHandle2.readDataFromFile('sarah2.txt')
print(sarah.name + "'s fatest times are:" + str(sarah.top3()))
sarah.append('2.09')
print(sarah.top3())
所有类的方法都可以被类的实例调用
4.point
5.一个关于python字符串的处理,2和3都能用
6.一个枚举函数enumerate(),返回列表的位置和列表值
animals = ['cat', 'dog', 'monkey']
for idx, animal in enumerate(animals):
print('#%d: %s' % (idx + 1, animal))
# Prints "#1: cat", "#2: dog", "#3: monkey", each on its own line
7.访问字典dict中的键值对:d.item()
d = {'person': 2, 'cat': 4, 'spider': 8}
for animal, legs in d.items():
print('A %s has %d legs' % (animal, legs))
# Prints "A person has 2 legs", "A cat has 4 legs", "A spider has 8 legs"
今天就到这,吃饭去