Python数据结构

来源:互联网 发布:淘宝微淘粉丝怎么增加 编辑:程序博客网 时间:2024/05/22 00:08

列表

shoplist =['apple','mango','carrpt','banana']print('I have',len(shoplist),'items to purchase.')print('This items are:',end=' ')for item in shoplist:print(item,end=' ')print('\n I alse have to buy rice.')shoplist.append('rice')print('My shooping list is now',shoplist)print('I will sort my list now')shoplist.sort()print('Sorted shopping list is',shoplist)print('This first item I will buy is',shoplist[0])olditem = shoplist[0]del shoplist[0]print('I bought the',olditem)print('My shooping list is now',shoplist)
元组

zoo = ('python','elephant','penguin')print('Number of animals in the zoo is',len(zoo))new_zoo = 'monkey','camel',zoo print('Number of cages in the new zoo is',len(new_zoo))print('All animal in new zoo are',new_zoo)print('Animals btought form old zoo is',new_zoo[2][2])print('Number of animals in the new zoo is',len(new_zoo)-1 + len(new_zoo[2]))
字典

ab = {'Swaroop' : 'swaroop@163.com','Larry' : 'larry@126.com','Saharui' : '7651516@qq.com'}print(ab['Swaroop'])print('\nThere are {} contacts in the address=book\n'.format(len(ab)))for name,address in ab.items():print('Contact {} at {}'.format(name,address))ab['Guido'] = 'guid@python.org'if 'Guido' in ab:print('Guido a address is',ab['Guido'])
序列

shoplist = ['apple','mango','carrot','banana']name = 'swaroop'print('item 0 is {}'.format(shoplist[0]))print('item 1 is {}'.format(shoplist[1]))print('item 2 is {}'.format(shoplist[2]))print('item 3 is {}'.format(shoplist[3]))print('item -1 is {}'.format(shoplist[-1]))print('item -2 is {}'.format(shoplist[-2]))print('Character 0 is',name[0])#Slicing on a list #print('Item 1 to 3',shoplist[1:3])print('Item 2 to end',shoplist[2:])print('Item 1 to -1',shoplist[-1])print('Item start to end is' ,shoplist[:])#从某一字符串中切片 print('characters 1 to 3 is ',name[1:3])print('characters 2 to end is ',name[2:])print('characters 1 to -1 is',name[1:-1])print('characters start to end is',name[:])
引用

print('Simple Assignment')shoplist = ['apple','mango','carrot','banana']mylist = shoplistdel shoplist[0]print('shoplist is',shoplist)print('mylist is',mylist)print('Copy by making a full slice')mylist = shoplist[:]del mylist[0]print('shoplist is',shoplist)print('mylist is',mylist)

原创粉丝点击