笨办法学python 40

lesson 40
字典
列表 可以使用数字作为索引,通过数字找到列表中的元素
字典 可以通过任何东西找到元素,不止是数字。
字典可以将一个物体和另外一个物体关联。

# cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville','NY':'New York','OR':'Portland'}
cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville'}
cities['NY'] = 'New York'
cities['OR'] = 'Portland'
 
 
# 字典查的是key,不查value
def findCity(themap, state):
    if state in themap:
        return themap[state]
    else:
        return 'Not found.'
 
 
# ok pay attention!
# 调用函数不带括号时,是取的函数本身,即函数在内存中的地址
cities['_find'] = findCity
while True:
    print('State?(ENTER to quit)')
    state = input("> ")
    # 对于字符串,none为False,有字符就为True
    if not state: break
    # this line is the most important ever!study!
    cityFound = cities['_find'](cities, state)
    print(cityFound)
 
print(cities)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值