Python(4)操作列表

云计算 waitig 437℃ 百度已收录 0评论

4.4使用列表一部分

切片

players=['a','b','c','d']
print (players[1:3])
print (players[:3])
print (players[1:])
print (players[-3:])#打印最后三个
print (players[:])  #打印所有的

print ('\n'+"Here are the first three pop star:")
for player in players[:3]:
    print (player.title())

用切片复制列表

my_food=['aa','bb','cc']
friend_food=my_food[:]
my_food.append('dd')
friend_food.append('ee')
print ('\n'+"I like these foods:"+str(my_food))
print ('She likes these foods:'+str(friend_food))

练习

#
magicians=['a','b','c','d','e']
print ('\n'+"Here are the first three pop star:")
print (magicians[2:4])

#
my_pizza=['aa','bb','cc']
friend_pizza=my_pizza[:]
my_pizza.append('gg')
friend_pizza.append('ff')
print ('\n'+"I like these pizza:"+str(my_food))
print ('She likes these pizza:')
for pizza in friend_pizza:
    print pizza

本文由【waitig】发表在等英博客
本文固定链接:Python(4)操作列表
欢迎关注本站官方公众号,每日都有干货分享!
等英博客官方公众号
点赞 (0)分享 (0)