python3 _笨方法学Python_日记_DAY7

习题 40: 字典, 可爱的字典

回顾一下列表:

列表可以通过数字,从0开始的数字来索引列表中的元素

而字典dict,可以通过任何东西找到元素,可以将一个东西和另一个相关联

还可以通过字符串向字典中添加

stuff = {‘name‘: ‘Zed‘, ‘age‘: 18, ‘height‘: 6**6}
stuff[‘city‘] = ‘San Francisco‘
print(stuff[‘city‘])
stuff[1] = ‘Wow‘
stuff[2] = ‘Neato‘
print(stuff)
San Francisco
{‘name‘: ‘Zed‘, ‘age‘: 18, ‘height‘: 46656, ‘city‘: ‘San Francisco‘, 1: ‘Wow‘, 2: ‘Neato‘}

通过del来删除

stuff = {‘name‘: ‘Zed‘, ‘age‘: 18, ‘height‘: 6**6}
stuff[‘city‘] = ‘San Francisco‘
print(stuff[‘city‘])
stuff[1] = ‘Wow‘
stuff[2] = ‘Neato‘
print(stuff)

del stuff[‘city‘]
del stuff[1]
del stuff[2]
print(stuff)
San Francisco
{‘name‘: ‘Zed‘, ‘age‘: 18, ‘height‘: 46656, ‘city‘: ‘San Francisco‘, 1: ‘Wow‘, 2: ‘Neato‘}
{‘name‘: ‘Zed‘, ‘age‘: 18, ‘height‘: 46656}

练习:

cities = {‘CA‘: ‘San Francisco‘, ‘MI‘: ‘Detroit‘,
          ‘EL‘: ‘Jacksonville‘}

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

def find_city(themap, state):
    if state in themap:
        return themap[state]
    else:
        return "Not Found."

cities[‘_find‘] = find_city

while True:
    print("State?(ENTER to quit")
    state = input(">")
    if not state:break

    city_found = cities[‘_find‘](cities,state)
    print(city_found)

结果

State?(ENTER to quit
>YK
Not Found.
State?(ENTER to quit
>NY
New York
State?(ENTER to quit
>OR
Portland
State?(ENTER to quit
>CA
San Francisco
State?(ENTER to quit
>

Process finished with exit code 0

1. Python 看到 city_found = 于是知道了需要创建一个变量。
2. 然后它读到 cities ,然后知道了它是一个字典
3. 然后看到了 [‘_find‘] ,于是 Python 就从索引找到了字典 cities 中对应的位
置,并且获取了该位置的内容。
4. [‘_find‘] 这个位置的内容是我们的函数 find_city ,所以 Python 就知道了
这里表示一个函数,于是当它碰到 ( 就开始了函数调用。
5. cities, state 这两个参数将被传递到函数 find_city 中,然后这个函数就被
运行了。
6. find_city 接着从 cities 中寻找 states ,并且返回它找到的内容,如果什么
都没找到,就返回一个信息说它什么都没找到。
7. Python find_city 接受返回的信息,最后将该信息赋值给一开始
city_found 这个变量。

如果你倒着阅读的话,代码可能会变得更容易理解。让我
们来试一下,一样是那行:
1. state city ...
2. 作为参数传递给...
3. 一个函数,位置在...
133
4. ‘_find‘ 然后寻找,目的地为...
5. cities 这个位置...
6. 最后赋值给 city_found.
还有一种方法读它,这回是“由里向外”。
1. 找到表达式的中心位置,此次为 [‘_find‘].
2. 逆时针追溯,首先看到的是一个叫 cities 的字典,这样就知道了 cities
_find 元素。
3. 上一步得到一个函数。继续逆时针寻找,看到的是参数。
4. 参数传递给函数后,函数会返回一个值。然后再逆时针寻找。
5. 最后,我们到了 city_found = 的赋值位置,并且得到了最终结果。

原文地址:https://www.cnblogs.com/mrfri/p/8611533.html

时间: 2024-08-05 04:08:02

python3 _笨方法学Python_日记_DAY7的相关文章

python3 _笨方法学Python_日记_DAY5

Day5 习题  24:  更多练习 1 print("Let's practice everything.") 2 print('You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.') 3 4 poem = """ 5 \tThe lovely world 6 with logic so firmly planted 7 cannot discern \n

python3 _笨方法学Python_日记_DAY4

Day4 习题  19:  函数和变量 1 def cheese_and_crackers(cheese_count, boxes_of_crackers): 2 print("You have %d cheeses!" % cheese_count) 3 print("You have %d boxes of crackers!" % boxes_of_crackers) 4 print("Man that's enough for a party!&q

python3 _笨方法学Python_日记_DAY6

Day6 习题  36:  设计和调试 If 语句的规则1. 每一个"if 语句"必须包含一个 else.2. 如果这个 else 永远都不应该被执行到,因为它本身没有任何意义,那你必须在else 语句后面使用一个叫做 die 的函数,让它打印出错误信息并且死给你看,这和上一节的习题类似,这样你可以找到很多的错误.3. "if 语句"的嵌套不要超过 2 层,最好尽量保持只有 1 层. 这意味着如果你在 if 里边又有了一个 if,那你就需要把第二个 if 移到另一个

笨方法学python3

阅读<笨方法学python3>,归纳的知识点 习题1:安装环境+练习  print函数使用  主要区别双引号和单引号的区别 习题2:注释符号# 习题3:运算符优先级,跟C/C++, Java类似 以下运算符优先级:从下往下以此递增,同行为相同优先级 Lambda #运算优先级最低 逻辑运算符: or 逻辑运算符: and 逻辑运算符:not 成员测试: in, not in 同一性测试: is, is not 比较: <,<=,>,>=,!=,== 按位或: | 按位异

Day 2 笨方法学Python

手打第25个练习,出错的地方有: def 定义后indent 4个空格,第一行空了以后,直接换行是跟上面对其的,但是运行时是错误的,我的解决方法是,重新手动空格4个: 还发现一个问题就是,中文解释,以前老是出错 # -*- coding : utf-8 -*- 网上看到的加上这个就可以了,但是我的还是出错.今天偶然在削微寒的博客http://www.cnblogs.com/xueweihan/的GIthub上找到了答案 #coding:utf-8 换成这个语句就可以了.以后,尽量每句都加上注释,

笨方法学python(5)加分题

这篇对应的是习题17,更多文件操作 # -*- coding: utf-8 -*- #对文件更多操作复制A文件的内容到B文件 #from sys import argv from os.path import exists prompt = "> " from_file = raw_input("please input the filename where you want to copy from: >") #in_file = open(from_

笨方法学python(6)加分题--列表与字典的区别

he string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSIIG

笨方法学python(4)加分题

自己在看笨方法学python这本书,把自己觉得有学到东西的记下来,并不是每个习题都有记录 这次对应的是:习题 6: 字符串(string)和文本 这次只要是想说明一下,在print语句中,只要带有格式化字符的,会当作格式化来处理 脚本1: 结果1: 打出的结果没有%r,那是因为当作格式化处理了 脚本2: 结果2: 会报错,因为print joke_evaluation %hilarious 是格式化的标识 脚本3: 结果3:

笨方法学Python3(21-44)

接着前天的总结 习题21:函数可以返回某些东西 定义函数的加减乘除,以及嵌套使用 习题22:回顾以前学的知识 习题23:字符串.字节串和字符编码 综合运用字符串.函数.文件读取等知识.详情如下: from sys import argv script, encoding, error = argv # 命令行参数处理 def main(language_file, encoding,errors): line = language_file.readline() # 就是用readline()处