方法 | 说明 |
---|---|
clear | 清空字典内容 |
get | 返回关键字所关联的值,如果指定键不存在,则返回默认值 |
keys | 以列表的形式返回字典中的所有键。所得列表中的每个条目肯定是唯一的 |
items | 返回(key,value)列表 |
values | 以列表的形式返回字典中的所有值。所得列表中的每个条目不一定是唯一的 |
update | 用另一个字典的内容对当前字典进行更新 |
附录:
def find_two_smallest(L):
"""Return a tuple of the indices of the two smallest values in list L"""
if L[0] < L[1]:
min1,min2 = 0,1
else:
min1,min2 = 1,0
for n in range(2,len(L)):
if L[n] < L[min1]:
min2 = min1
min1 = n
elif L[n] < L[min2]:
min2 = n
return (min1,min2)
def linear_search(L,v):
"""Return the index of the first occurrence of v in list L, or return len
if v is not in L"""
for i in range(len(L)):
if L[i] == v:
return i
return len(L)
def selection_sort(L):
"""Reorder the values in L from smallest to largest."""
i = 0
while i != len(L):
smallest = find_min(L, i)
L[i],L[smallest] = L[smallest],L[i]
i += 1
def find_min(L,b):
"""Return the index of the smallest value in L[b:]."""
smallest = b # The index of the smallest so far.
i = b + 1
while i != len(L):
if L[i] < L[smallest]:
smallest = i
i += 1
return smallest
def insertion_sort(L):
"""Reorder the values in L from smallest to largest."""
i = 0
while i != len(L):
insert(L, i)
i += 1
def insert(L, b):
"""Insert L[b] where it belongs in L[0:b+1];
L[0:b-1] must already be sorted"""
i = b
while i != 0 and L[i-1] > L[b]:
i -= 1
value = L[b]
del L[b]
L.insert(i, value)