美文网首页
Python 数据结构

Python 数据结构

作者: CaptainRoy | 来源:发表于2018-11-12 17:17 被阅读8次

Python 有四种内置的数据结构 - 列表(List),元组(Tuple),字典(Dictionary),集合(Set)

List 列表
shopList = ['apple','mango','carrot','banana']

print('I have',len(shopList),'items to purchase.')

for item in shopList:
    print(item)

shopList.append('rice')

print(shopList) # ['apple', 'mango', 'carrot', 'banana', 'rice']

shopList.sort()
print(shopList) # ['apple', 'banana', 'carrot', 'mango', 'rice']

print(shopList[1])

del shopList[0]
print(shopList) # ['banana', 'carrot', 'mango', 'rice']
元组
  • 元组是不可改变的
zoo = ('python','elephant','penguin') # ('python', 'elephant', 'penguin')
print(len(zoo)) # 3

newZoo = ('monkey','camel',zoo)
print(newZoo) # ('monkey', 'camel', ('python', 'elephant', 'penguin'))

print(newZoo[2]) # ('python', 'elephant', 'penguin')
print(newZoo[2][2]) # penguin
字典
address = {
    'roy' : 'roy@icloud.com',
    'lily' : 'lily@mail.com',
    'leo' : 'leo@hotmail.com'
}

for name,mail in address.items():
    print('name:',name,'email:',mail)

del address['roy']

address['faker'] = 'faker@qq.com'
集合
bri = {'brazil','russia','india'}

result = 'russia' in bri

bri.add('china')

bri.remove('india')

相关文章

网友评论

      本文标题:Python 数据结构

      本文链接:https://www.haomeiwen.com/subject/uoscfqtx.html