exercise 38 列表操作

ten_things="Apples Orange Crows Telephone Light Sugar"  # no comma between two words
print "Wait there are not 10 things in that list. Let's fix that."

more_stuff="Day Night Song Frisbee Corn Banana Girl Boy"
#more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"]


stuff2=more_stuff.split(' ')  #注意 别拼写错!!!
stuff=ten_things.split(' ')
while len(stuff)!=10:
    next_one=stuff2.pop()
    print "Adding:", next_one
    stuff.append(next_one)
    print "There are %d items now" %len(stuff)

print "There we go: ", stuff
print "Let's do some things with stuff"
print stuff[1]
print stuff[-1] # whoa! fancy
print stuff.pop()
print ' '.join(stuff) # what? cool!
print '#'.join(stuff[3:5]) # super stellar!   stuff[3:5] 用法同range(3,5)不包括5


代码细节1:ten_things 是字符串 中间用空格隔开 不是逗号。
	2:在原版程序中 more_stuff 以数组形式定义(注释了的那行) 不是字符串,不需要split函数将其分开


列表能做什么:

什么时候不能使用列表:
1.你需要维持秩序的时候。列表只能显示原来的顺序,不能整理(sort)顺序。
2.如果需要通过号码随机访问内容。 记住,这是使用从0开始的基数。
3.如果你需要按顺序遍历内容(第一到最后),使用for循环。








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值