#习题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])

深拷贝,浅拷贝

待续

最新文章

  1. centos6环境下搭建irc服务器
  2. [转]CIDR简介
  3. 表单提交set集合问题
  4. codeforces B. Permutation 解题报告
  5. SpringMvc中的反射
  6. C++中求两个正整数的最大公约数和最小公倍数
  7. 劳动节BT5 aircrack-ng战记
  8. android学习日记18--Adapter简介
  9. 【HDOJ】4278 Faulty Odomete
  10. 程序猿都是project师吗?
  11. 【搜索引擎Jediael开发笔记2】使用HttpClient下载网页至本地文件
  12. Unity Container
  13. Ubuntu上64位adv无法创建问题
  14. dict的操作和三级菜单
  15. POJ - 3984 迷宫问题 bfs解法
  16. 1259 整数划分 V2
  17. 【UML】Java代码与UML模型相互转换方法
  18. (转)Eclipse配置GitHub代码库(以Windows7为例)
  19. 《剑指offer》第六十八题(树中两个结点的最低公共祖先)
  20. [剑指Offer]22-链表中倒数第k个结点

热门文章

  1. JindoFS解析 - 云上大数据高性能数据湖存储方案
  2. AndroidManifest.xml配置文件详解(转)
  3. windows下tomcat7+nginx1.8负载均衡
  4. 原来... 用debug如何查看当前标志寄存器的标志位值?
  5. 2、jQuery操作Dom(过滤器与选择器)
  6. 畜禽免疫系统使用LODOP打印
  7. testNG官方文档翻译-5 测试方法,测试类和测试组
  8. Eureka 系列(06)消息广播(下):TaskDispacher 之 Acceptor - Worker 模式
  9. 利用os和pandas来合并当前目录下所有excel文件
  10. 10-vim-选中命令-01-三种选择文本的方式