zoukankan      html  css  js  c++  java
  • 简明python教程五----数据结构

    python中有三种内建的数据结构:列表、元组和字典

    list是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目。在python中,每个项目之间用逗号分隔。

    列表中的项目应该包括在方括号中,这样python就知道你是在指明一个列表。一旦你创建了一个列表,可以添加、删除或者搜索列表中的项目。

    列表是可变的数据类型,这种类型是可以被改变的。

    python为list类提供了append方法,让你在列表尾添加一个项目。

    如mylist.append('an item')列表mylist中增加那个字符串。注意使用点号来使用对象的方法。

    #!/usr/bin/python
    #Filename:using_list.py
    
    #This is my shopping list
    shoplist=['apple','mango','carrot','banana']
    print 'I have',len(shoplist),'items to purchase.'
    
    print 'These items are:',
    for item in shoplist:
        print item,
    
    print '
    I 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 T will buy is',shoplist[0]
    olditem=shoplist[0]
    del shoplist[0]
    print 'I bought the',olditem
    print 'My shopping list is now',shoplist

    结果:

    I have 4 items to purchase.
    These items are: apple mango carrot banana
    I also have to buy rice.
    My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice']
    I will sort my list now
    Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice']
    The first item I will buy is apple
    I bought the apple
    My shopping list is now ['banana', 'carrot', 'mango', 'rice']

    shoplist是购物列表,在shoplist列表中可以添加任何种类的对象包括数甚至其他列表。

    我们使用列表的sort方法来对列表排序。注意:这个方法影响列表本身,而不是返回一个修改后的列表。(列表可变而字符串是不可变的)

    元组

    元组和列表十分类似。只不过元组和字符串一样是不可变的,即你不能修改元组

    元组通过圆括号中用逗号分隔的项目定义。元组的值不会改变

    zoo = ('wolf','elephant','penguin')
    print 'Number of animals in the zoo is',len(zoo)
    
    new_zoo = ('monkey','dolphin',zoo)
    print 'Number of animals in the new zoo is',len(new_zoo)
    print 'All animals in new zoo are',new_zoo
    print 'Animals brought from old zoo are',new_zoo[2]
    print 'Last animal brought from old zoo is',new_zoo[2][2]

    输出结果:

    Number of animals in the zoo is 3
    Number of animals in the new zoo is 3
    All animals in new zoo are ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin'))
    Animals brought from old zoo are ('wolf', 'elephant', 'penguin')
    Last animal brought from old zoo is penguin

    len函数可以用来获取元组的长度。

    通过一对方括号来指明某个项目的位置来访问元组中的项目,称为索引运算符,

    含有0个或1个项目的元组,空的元组由一对空的圆括号组成,如myempty=()

    含有单个元素的元组:你必须在第一个(唯一一个)项目后跟一个逗号,python才能区分元组和表达式中一个带圆括号的对象。如singleton=(2,)。

    列表之中的列表不会失去它的身份,同样元组中的元组,或列表中的元组,他们就是使用一个对象存储的对象。

    元组与打印语句

    age = 22
    name = 'Swaroop'
    
    print '%s is %d years old'%(name,age)
    print 'why is %s playing with that python?'%name

    print语句可以使用跟着%符号的项目元组的字符串。这些字符串具备定制的功能。

    定制可以是%s表示字符串或%d表示整数。

    在第二个print语句中,我们使用了一个定制,,后面跟着%符号后的单个项目----没有圆括号。这只在字符串中只有一个定制的时候有效

    字典

    字典类似于你通过联系人名字查找地址和联系人详细情况的地址簿。我们把键(名字)值(详细情况)联系在一起。

    注意:键必须唯一。

       只能使用不可变的对象(比如字符串)来作为字典的,可以把不可变或可变的对象作为字典的

    键值对在字典中标记方式:d={key1:value1,key2:value2},注意键/值对用冒号分隔,而各个对用逗号分隔,所以这些都包括在花括号中。

    记住字典中的键/值对是没有顺序的。如果你想要一个特定的顺序,那么你应该在使用前自己对他们排序

    字典是dict类的实例/对象

    #'ab' is short for 'a' ddress 'b' ook
    ab={'Swaroop':'swaroopch@byteofpython.info',
        'Larry':'larry@wall.org',
        'Matsumoto':'matz@ruby-lang.org',
        'Spammer':'spammer@hotmial.com'
        }
    print "Swaroop's address is %s"%ab['Swaroop']
    
    ab['Guido']='guido@python.org'
    
    del ab['Spammer']
    print '
    There are %d contacts in the address-book
    '%len(ab)
    for name,address in ab.items():
        print 'Contact %s at %s' %(name,address)
    
    if 'Guido' in ab:
        print "
    Guido's address is %s"%ab['Guido']

    序列

    列表、元组和字符串都是序列,序列的两个主要特点是索引操作符和切片操作符

    切片操作符:让我们获取序列的一个切片,即一部分序列。

    shoplist = ['apple','mango','carrot','banana']
    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 -1 is",shoplist[-1]
    print "Item -2 is",shoplist[-2]
    
    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[:]
    
    name = 'swaroop'
    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[:]

    结果:

    Item 0 is apple
    Item 1 is mango
    Item 2 is carrot
    Item 3 is banana
    Item -1 is banana
    Item -2 is carrot
    Item 1 to 3 is ['mango', 'carrot']
    Item 2 to end is ['carrot', 'banana']
    Item 1 to -1 is ['mango', 'carrot']
    Item start to end is ['apple', 'mango', 'carrot', 'banana']
    characters 1 to 3 is wa
    characters 2 to end is aroop
    characters 1 to -1 is waroo
    characters start to end is swaroop

    使用索引来取得序列中的单个项目,例如shoplist[1]

    索引同样可以是负数,位置从序列尾开始计算的。因此shoplist[-1]表示序列的最后一个。

    切片操作符是序列名后跟一个方括号,方括号中有一对可选的数字,并且冒号分隔。记住数时可选的,而冒号是必须的。

    切片操作符的第一个数(冒号前)表示切片开始的位置,第二个数(冒号后)表示切片到哪里结束。

    不指明第一个数,python就从序列首开始,不指明第二数,python会停止在序列尾。

    注意返回的序列从开始的位置开始,在结束位置之前结束。即开始位置是包含在序列切片中的,而结束位置被排斥在切片外。

  • 相关阅读:
    前后端项目结构规范性记录
    开发问题记录(这部分还是比较零碎)
    对HashMap的一次记录
    面试问题记录 三 (JavaWeb、JavaEE)
    面试问题记录 二 (数据库、Linux、Redis)
    面试问题记录 一 (基础部分)
    对正则表达式的一些记录
    WEB与游戏开发的一些区别
    MarkDown常用语法全纪录
    MySQL压测相关内容
  • 原文地址:https://www.cnblogs.com/Caden-liu8888/p/6383366.html
Copyright © 2011-2022 走看看