笨办法学Python记录--习题38-40,复习前面,运用list操作函数
#习题38 区分列表和字符串,用到了split(字符串专用函数),join、append、pop(这些是list操作函数)
ten_things = "Apples Oranges Crows Telephone Liht Sugar" print "Wait there is not 10 things in that list, let's fix that." stuff=ten_things.split(' ') more_stuff = ["Day","Night","Song","Frisbee","Corn","Banana","Girl","Boy"] while len(stuff)!=10:
next_one = more_stuff.pop()
print "Adding:",next_one
stuff.append(next_one)
print "There's %d items now." % len(stuff) print "There we go:",stuff
print "Let's do some things with stuff." print "stuff[1]"
print stuff[1] print "stuff[-1]"
print stuff[-1] print "stuff.pop()"
print stuff.pop() print "' '.join(stuff)"
print ' '.join(stuff) print "'#'.join(stuff[3:5]"
print '#'.join(stuff[3:5] )
结果:
#习题38 区分列表和字符串,同时学者使用split函数
ten_things = "Apples Oranges Crows Telephone Liht Sugar"
print "Wait there is not 10 things in that list, let's fix that."
stuff=ten_things.split(' ')
more_stuff = ["Day","Night","Song","Frisbee","Corn","Banana","Girl","Boy"]
while len(stuff)!=10:
next_one = more_stuff.pop()
print "Adding:",next_one
stuff.append(next_one)
print "There's %d items now." % len(stuff)
print "There we go:",stuff
print "Let's do some things with stuff."
print "stuff[1]"
print stuff[1]
print "stuff[-1]"
print stuff[-1]
print "stuff.pop()"
print stuff.pop()
print "' '.join(stuff)"
print ' '.join(stuff)
print "'#'.join(stuff[3:5]"
print '#'.join(stuff[3:5]
)
个人觉得这么使用字典很帅很帅!!
cities = {'CA':'San Francisco','MI':'Detroit','FL':'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 = raw_input("> ") if not state:break
city_found = cities['_find'](cities,state)
print city_found
关于字典:字典出现在当索引不好用的时候--字典中的值并没有特殊的顺序,但是都存储在一个特定的键(key)里。key可以是数字、字符串甚至元组。
字典应用:1. 数字电话/地址簿;2. 存储文件修改次数,用文件名作为键;3. 表征游戏键盘的状态,每个键都是由坐标值组成的元组;
dict函数的使用:
>>>d=dict(name='GG', age=32)
>>>d
{'age':32,'name':'GG'}
or
>>>a=[('name','GG'),('age',42)]
>>>d=dict(a)
简单数据库实现:
people = {'Alice':{'phone':'','addr':'Foo drive 23'},'Beth':{'phone':'','addr':'Bar steet 42'},'Cecil':{'phone':'','addr':'Baz avenue 90'}}
labels = {'phone':'phone number','addr':'address'}
name = raw_input('Name: ')
request=raw_input('phone number(p) or address(a)?')
if request=='p':
key = 'phone'
if request == 'a':
key = 'addr'
if name in people:
print "%s's %s is %s." % (name,labels[key],people[name][key])
深拷贝,浅拷贝
待续
笨办法学Python记录--习题38-40,复习前面,运用list操作函数的更多相关文章
- 笨办法学Python记录--习题18 变量 函数 help的由来;if语句,循环和列表,冒泡排序,判断输入字符串的方法
20140414 记录 习题17 - 33 函数可以做3件事: 1. 给代码片段命名,,就跟“变量”给字符串和数字命名一样. 2. 可以接受参数,就跟你的脚本接受argv 一样. 3. 通过使用#1 ...
- 笨办法学Python记录--习题37 异常,lambda,yield,转义序列
习题中提到了raise,查了下,顺便所有异常类关键字罗列如下文章中: 为什么使用异常 错误处理.事件通知.特殊情况处理.退出时的行为.不正常的程序流程. 简单的示例 在没有任何定义x变量的时候: pr ...
- 笨办法学Python记录--习题1-11
20140412(习题1-10),和打印较劲: 1. 读这本书时没有按照要求安装Python2,我选择的是最新版3.4.0(官方release),然后悲剧发现完全不兼容,现在摘录2,3区别: 这个星期 ...
- 笨办法学Python记录--习题15-17 开始读写文件啦
习题15 - 17 打开并读流程: from sys import argv script,filename = argv txt = open(filename) print "Here' ...
- 笨办法学Python记录--习题12-14 主要是pydoc用法,raw_input,argv
20140413 -- 习题12 - 14 1. pydoc在windows的用法,必须进入到python安装目录,执行Python -m pydoc raw_input; 网上给出了一个好玩的,不过 ...
- 【笨办法学Python】习题11:打印出改变了的输入
print "How old are you?", age = raw_input() print "How tall are you?", height = ...
- 笨办法学Python - 习题1: A Good First Program
在windows上安装完Python环境后,开始按照<笨办法学Python>书上介绍的章节进行练习. 习题 1: 第一个程序 第一天主要是介绍了Python中输出函数print的使用方法, ...
- 笨办法学 Python (Learn Python The Hard Way)
最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注 ...
- 笨办法学 Python (第三版)(转载)
笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html 摘自https://learn-python ...
随机推荐
- boot、cloud
最近在学习Spring Boot也整理了一些文章,有需要的可以参考一下 https://www.zhihu.com/question/39483566 Spring Cloud是一系列框架的有序集合. ...
- Python基础(三):简化除法判断、分析apache访问日志、扫描存活主机、利用多线程实现ssh并发访问
一.简化除法判断 目标: 编写mydiv.py脚本,主要要求如下: 提示用户输入一个数字作为除数 如果用户按下Ctrl+C或Ctrl+D则退出程序 如果用户输入非数字字符,提示用户应该输入数字 如果用 ...
- VIEW当中自定义属性的使用
主要有三种方法可以实现自定义属性. 第一种方法,直接设置属性值,通过attrs.getAttributeResourceValue拿到这个属性值. (1)在xml文件中设置属性值 [html] vie ...
- 55、saleforce 学习笔记二
String goodsName = 'abcd1123汉字显示';//测试文本 System.debug('简化后的字符串名称为:'+goodsName.abbreviate(5)); //返回简化 ...
- 析构中delete this
查看下面代码如何出错 #include <iostream> using namespace std; class A { public: A() { p = this; } ~A() { ...
- 将日志(Microsoft.Extensions.Logging)添加到.NET Core控制台应用程序
在.NET Core项目中,日志记录是通过依赖项注入进行管理的. 尽管这对于ASP.NET项目效果很好,但在启动Startup.cs中的新项目时,所有这些都会自动创建,而在控制台应用程序中则需要一些配 ...
- python学习笔记:通配符之glob模块(过滤)
glob模块用来查找文件目录和文件,可以和常用的find功能进行类比.glob支持*?[]这三种通配符.返回的数据类型是list.常见的两个方法有glob.glob()和glob.iglob(),ig ...
- Django框架(三十)—— 使用Vue搭建前台
目录 vue的使用 一.创建vue项目 二.pycharm开发vue项目 1.安装vue.js插件 2.运行vue项目 三.vue项目的目录结构 四.vue的使用 1.创建新的组件 2.显示数据 3. ...
- 【小程序】获取到的e.target与e.currentTarget区别
[小程序]获取到的e.target与e.currentTarget区别:https://blog.csdn.net/qq_33744228/article/details/80310294 使用e.t ...
- 微服务-熔断器 Hystrix 的原理与使用
前言 分布式系统中经常会出现某个基础服务不可用造成整个系统不可用的情况, 这种现象被称为服务雪崩效应. 为了应对服务雪崩, 一种常见的做法是手动服务降级. 而Hystrix的出现,给我们提供了另一种选 ...