# 《笨办法学Python》 第38课手记

### 《笨办法学Python》 第38课手记

```# create a mapping of state to abbreviation
states = {
'Oregon': 'OR',
'Florida': 'FL',
'California': 'CA',
'New York': 'NY',
'Michigan': 'MI',
}

# create a basic set of states and some cities in them
cities = {
'CA': 'San Francisco',
'MI': 'Detroit',
'FL': 'Jacksonville'
}

cities['NY'] = 'New York'
cities['OR'] = 'Portland'

# print out some cities
print '-' * 10
print "NY State has: ", cities['NY']
print "OR State has: ", cities['OR']

# print some states
print '-' * 10
print "Michigan's abbreviation is: ", states['Michigan']
print "Florida's abbreviation is: ", states['Florida']

# do it by using the states then cities dict
print '-' * 10
print "Michigan has: ", cities[states['Michigan']]
print "Florida has: ", cities[states['Florida']]

# print every state abbreviation
print '-' * 10
for state, abbrev in states.items():
print "%s is abbreviated %s" % (state, abbrev)

# print every city in state
print '-' * 10
for abbrev, city in cities.items():
print "%s has the city %s" % (abbrev, city)

# now do both at the same time
print '-' * 10
for state, abbrev in states.items():
print "%s state is abbreviated %s and has city %s" % (
state, abbrev, cities[abbrev])

print '-' * 10
# safely get a abbreviation by state that might not be there
state = states.get('Texas', None)

if not state:
print "Sorry, no Texas."

# get a city with a default values
city = cities.get('TX', 'Does Not Exist')
print "The city for the state 'TX' is: %s" % city```

① 第一种： states = { ‘Oregon’: ‘OR’, ‘Florida’: ‘FL’, ‘California’: ‘CA’, ‘New York’: ‘NY’, ‘Michigan’: ‘MI’, } 第二种： cities[‘NY’] = ‘New York’ cities[‘OR’] = ‘Portland’

② print ‘-’ * 10 print “Michigan has: “, cities[states[‘Michigan’]] print “Florida has: “, cities[states[‘Florida’]]

③ print ‘-’ * 10 for state, abbrev in states.items(): print “%s is abbreviated %s” % (state, abbrev)

abbrev也是一个关键字，是指列表中元素的缩写。states.item()会遍历states里面的所有内容。

④ state = states.get(‘Texas’, None) 这里涉及到get函数。

dict.get(key, default=None)

### 本节课涉及的知识

Operation

Result

len(a)

the number of items in a 得到字典中元素的个数

a[k]

the item of a with key k 取得键K所对应的值

a[k] = v

set a[k] to v 设定键k所对应的值成为v

del a[k]

remove a[k] from a 从字典中删除键为k的元素

a.clear()

remove all items from a 清空整个字典

a.copy()

a (shallow) copy of a 得到字典副本

k in a

True if a has a key k, else False 字典中存在键k则为返回True，没有则返回False

k not in a

Equivalent to not k in a 字典中不存在键k则为返回true,反之返回False

a.has_key(k)

Equivalent to k in a, use that form in new code 等价于k in a

a.items()

a copy of a’s list of (key, value) pairs 得到一个键值的list

a.keys()

a copy of a’s list of keys 得到键的list

a.update([b])

updates (and overwrites) key/value pairs from b 从b字典中更新a字典，如果键相同则更新，a中不存在则追加

a.fromkeys(seq[, value])

Creates a new dictionary with keys from seq and values set to value 创建一个新的字典，键来自seq，值对应键对应的值

a.values()

a copy of a’s list of values 得到字典值的副本

a.get(k[, x])

a[k] if k in a, else x 得到a[k]，若存在返回x

a.setdefault(k[, x])

a[k] if k in a, else x (also setting it) 得到a[k],若不存在返回x，并设定为x

a.pop(k[, x])

a[k] if k in a, else x (and remove k) 弹出a[k]，若不存在则返回x，同时将删除k键

a.popitem()

remove and return an arbitrary (key, value) pair 弹出a中对象的键和值，并删除弹出的键和值

a.iteritems()

return an iterator over (key, value) pairs 返回a中所有对象（键和值）

a.iterkeys()

return an iterator over the mapping’s keys 返回a中所有键（索引）

a.itervalues()

return an iterator over the mapping’s values 返回a中所有值

251 篇文章52 人订阅

0 条评论

## 相关文章

4726

### 【编程基础】C++比C牛逼的七个点

1. 函数检测增强 ? 在C语言中，重复定义多个同名的全局变量是合法的,在C++中，不允许定义多个同名的全局变量。 C语言中多个同名的全局变量最终会被链接到全局...

3555

1061

### 关于函数参数传递，80%人都错了

https://docs.python.org/3/faq/programming.html#how-do-i-write-a-function-with-ou...

1273

2077

2027

3016

3074

### Java数据结构和算法（九）——高级排序

春晚好看吗？不存在的！！！ 　　在Java数据结构和算法（三）——冒泡、选择、插入排序算法中我们介绍了三种简单的排序算法，它们的时间复杂度大O表示法都是O(...

4026

1051