Python 数据结构
它们是用来存储一系列相关数据的集合。
列表 (list)
是一种用于保存一系列有序项目的集合,也就是说,你可以利用列表保存一串项目的序列。列表是可变的,但是字符串是不可变的。
用列表实现我想买的物品,以及添加,分类和删除物品。
shoplist = ['apple','mango','carrot','banana']
print('I have',len(shoplist),'items to purchase.')
print('These items are :',end=' ')
for item in shoplist:
print(item,end=' ')
print('\nI also have to buy rice .')
shoplist.append('rice')
print('My shopping list is now',shoplist)
print('I will sort my list now.')
shoplist.sort()
print('Sorted shopping list is',shoplist)
print('The first item I will buy is',shoplist[0])
olditem = shoplist[0]
del shoplist[0]
print('I bought the ',olditem)
print('My shopping list is now.',shoplist)
元组 (tuple)
元组(Tuple)用于将多个对象保存到一起。你可以将它们近似地看作列表,但是元组不能提供列表类能够提供给你的广泛的功能。元组的一大特征类似于字符串,它们是不可变的,也就是说,你不能编辑或更改元组。
用元组实现增加动物园的小动物
oo = ('python','elephant','penguin')
print('Number of animals in the zoo is',len(zoo))
new_zoo = 'monkey','camel',zoo
print('Number of cages in new zoo is',len(new_zoo))
print('all animals in the new zoo are',new_zoo)
print('animals brought from old zoo are is',new_zoo[2])
print('last animal brought from old zoo is',new_zoo[2][2])
print('number of animals in the new zoo is',len(new_zoo)-1+len(new_zoo[2]))
字典(dictionary)
字典就像一本地址簿,如果你知道了他或她的姓名,你就可以在这里找到其地址或是能够联系上对方的更多详细信息,换言之,我们将键值(Keys)(即姓名)与值(Values)(即地址等详细信息)联立到一起。在这里要注意到键值必须是唯一的,正如在现实中面对两个完全同名的人你没办法找出有关他们的正确信息。
另外要注意的是你只能使用不可变的对象(如字符串)作为字典的键值,但是你可以使用可
变或不可变的对象作为字典中的值。基本上这段话也可以翻译为你只能使用简单对象作为键
值。
用字典功能实现添加地址薄
ab = {'jack':'jack@qq.com',
'mary':'mary@qq.com',
'michael':'michael@qq.com',
'august':'august@qq.com'
}
print("jack's address is",ab['jack'])
del ab['mary']
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['jane'] ='jane@qq.com'
if 'jane' in ab:
print("\njane's address is",ab['jane'])
序列(sequeece)
序列的主要功能是资格测试(Membership Test)(也就是 in 与 not in 表达式)和索引操(Indexing Operations),它们能够允许我们直接获取序列中的特定项目。
从序列中获取某一部分
shoplist = ['apple','mango','carrot','banana']
name = 'micheal'
print('Item 0 is',shoplist[0])
print('Item 1 is',shoplist[1])
print('Item 2 is',shoplist[2])
print('Item 3 is',shoplist[3])
print('Item -4 is',shoplist[-4])
print('Item -1 is',shoplist[-1])
print('Item -2 is',shoplist[-2])
print('Character 0 is',name[0])
# Slicing on a list #
print('Item 1 to 3 is',shoplist[1:3])
print('Item 2 to end is',shoplist[2:])
print('Item 1 to -1 is',shoplist[1:-1])
print('Item start to end is',shoplist[:])
print('character 1 to 3 is',name[1:3])
print('character 2 to end is',name[2:])
print('character 1 to -1 is',name[1:-1])
print('character start to end is',name[:])
#提供第三个参数,这一参数将视为切片的步长
shoplist = [::1] #按照一步
注意:切片设置的步长不能为0.
集合(set)
集合(Set)是简单对象的无序集合(Collection)。
仅仅是将一个变量名赋予给另一个名称,那么它们都将“查阅”同一个对象。
print('Simple Assignment')
shoplist = ['apple','mango','carrot','banana']
mylist = shoplist
del 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)
有关字符串的更多内容
在程序中使用的所有字符串都是 str 类下的对象。
name = 'Swaroop'
if name.startswith('Swa'):
print('Yes, the string starts with "Swa"')
if 'a' in name:
print('Yes, it contains the string "a"')
if name.find('war') != -1:
print('Yes, it contains the string "war"')
delimiter = '_*_'
mylist = ['Brazil', 'Russia', 'India', 'China']
print(delimiter.join(mylist))