python学习04——列表的操作
笨办法学python第38节
如何创建列表在第32节,形式如下:

本节主要是讲对列表的操作,首先讲了 mystuff.append('hello') 的工作原理,我的理解是,首先Python找到mystuff这个变量,然后进行append()这个函数操作。其中需要注意的是括号()里面有一个额外参数就是mystuff本身。
本文练习:
# create a mapping of state to abbreviation
states = {
'Oregon': 'OR',
'Florida': 'FL',
'California': 'CA',
'Michigan': 'MI'
} # create a basic set of states and some cities in them
cities = {
'CA': 'San Francisco',
'MI': 'Detroit',
'FL': 'Jacksonville'
} # add some more cities
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 state 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 (why not sequence)
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 value
city = cities.get('TX', 'Does Not Exist')
print "The city for the state 'TX' is: %s" % city
存在的问题:
1. 40行的输出城市名称,运行时输出的并不是顺序输出的,这个输出遵循的规律是什么?
2. states.items(),按照书里说的()里面有一个额外参数states,所以在这个()里面不加参数,因为里面的参数就是前面的列表,那如果想再加一个参数在()里面,如何加?
回答:
1. Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。
dict内部存放的顺序和key放入的顺序是没有关系的,dict的查找是根据哈希表查找的,所以输出不是顺序输出的。
2. 这个和书里的那个函数不一样,书里的那个函数是append(),这个item()相当于遍历这个列表,所以后面不再加参数。
python学习04——列表的操作的更多相关文章
- Python学习06——列表的操作(2)
笨办法学Python第39节 之前用的第三版的书,昨天发现内容不对,八块腹肌又给我下了第四版,这次的内容才对上.本节的代码如下: ten_things = "Apples Oranges C ...
- Python学习02 列表 List
Python学习02 列表 List Python列表 List Python中的列表(List)用逗号分隔,方括号包围(comma-separated values (items) between ...
- Python学习--04条件控制与循环结构
Python学习--04条件控制与循环结构 条件控制 在Python程序中,用if语句实现条件控制. 语法格式: if <条件判断1>: <执行1> elif <条件判断 ...
- python学习笔记——列表操作
python列表操作——增 append:追加一条数据到列表的最后 name = ["Zhangsan","XiongDa","Lisi"] ...
- Python基础学习8---list列表的操作
a_list = ['hello','world',1,'shanghai',3.99] #列表添加操作的4种方法 #1. 通过+ 字符来拼接 a_list = a_list + [1,'wuhan' ...
- 【Python学习之六】文件操作
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 python3.6 一.文件的打开与关闭 使用open函数,可以打开一 ...
- 【Python学习之十】操作数据库
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 python3.6 操作mysql数据库 1.安装pymysql模块p ...
- Python学习3——列表和元组
一.通用序列操作——索引.切片.相加.相乘.成员资格检查 1.索引,正序从0开始为第一个元素,逆序从-1开始,-1为最后一个元素 >>> greeting[0] 'h' >&g ...
- Python学习之列表篇
浮点数类型:round(x,d)可对浮点数进行四舍五入,科学计数法:aeb表示a*10^bpython大小写敏感整数类型:无范围限制,pow(x,y)表示x^y,想算多大算多大,四种表示形式:十进制, ...
随机推荐
- consul模板的说明2
保证模板的正常执行 使用||true $ consul-template -template "in.ctmpl:out.file:service nginx restart || true ...
- sublimetext3备份
http://files.cnblogs.com/files/hwd13/Data.zip http://files.cnblogs.com/files/hwd13/sublime3.zip
- Ubuntu 12.04 Virtualbox 启用USB 设备支持
转载自:http://www.cnblogs.com/ericsun/archive/2013/06/10/3130679.html 具体步骤在上面的链接中 今天在ubuntu下安装了Virtualb ...
- Photoshop投影和CSS box-shadow转换
"混合模式":Photoshop提供了各式各样的混合模式,但是CSS3阴影只支持正常模式(normal). "颜色(color)":阴影颜色.对应于CSS3阴影 ...
- [Android] keystore生成
keytool -genkey -alias agg_keystore -keyalg RSA -validity 20000 -keystore agg.keystore keytool -expo ...
- angularjs指令系统系列课程(3):替换replace,内容保留transclude,作用方式restrict
这一节我们主要看一下replace,transclude,restrict这三个参数 1.replace 可取值:bool 默认为:true 对于replace属性,设置为false表示原有指令标识不 ...
- 正确使用ng-if和ng-show
在使用bootstrap中,我们会经常用到按钮组,也就是btn-group,如果仔细观察的话,会发现一个按钮组的第一个和最后一个按钮分别是有圆角的,如下图: 但是中间的按钮是没有圆角的,这样显得比较美 ...
- Predicate接口和Consumer接口
Predicate和Consumer接口是集合操作中常用到的俩个接口: 1.Predicate public interface Predicate<T>{ boolean test(T ...
- Js dom操作总结
1.选取文档元素 1.1. getElemenById 基于唯一id获取,任何HTML元素可以有id属性,在文档中该值必须唯一. 1.2. getElementsByName 基于name属性的值选取 ...
- 【转】 memcmp源码实现
原型: int memcmp(void *buf1, void *buf2, unsigned int count); 用法:#include <string.h> 功能:比较内存区域bu ...