zoukankan      html  css  js  c++  java
  • Python学习进程(9)序列

        序列是Python中最基本的数据结构。

        (1)序列简介:

        序列中的每个元素都分配一个数字标明它的位置或索引,第一个索引是0,第二个索引是1,依此类推。序列都可以进行的操作包括索引,切片,加,乘,检查成员。 此外,Python已经内置确定序列的长度以及确定最大和最小的元素的方法。

        Python有6个序列的内置类型,但最常见的是列表和元组。 

        (2)列表:

        列表可以作为一个方括号内的逗号分隔值出现。列表的数据项不需要具有相同的类型。创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可。如下所示:

    list= ['name', 'age', True, [87,98,'sjm'],'sex'];

         在前面已经介绍列表的访问、更新和删除、列表脚本操作符和列表的截取。这里主要介绍列表函数和方法(主要针对2.7.x版本,Python3.5版本没有这些函数):

    1.Python2.7.*
    root@SJM:/home/sunjimeng/桌面# python
    Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
    [GCC 4.8.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> cmp
    <built-in function cmp>
    >>> len
    <built-in function len>
    >>> max
    <built-in function max>
    >>> min
    <built-in function min>
    >>> list
    <type 'list'>
    >>> exit()
    2.python3.5.*
    root@SJM:/home/sunjimeng/桌面# python3.5
    Python 3.5.2 (default, Jul 17 2016, 11:52:15) 
    [GCC 4.8.4] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> cmp            #高版本里没有cmp了
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'cmp' is not defined
    >>> len
    <built-in function len>
    >>> max
    <built-in function max>
    >>> min
    <built-in function min>
    >>> list
    <class 'list'>           #这里也由type变成了class

        1)函数:

          1.cmp(list1, list2)用于比较两个列表的元素:

          比较原则:

    1.如果比较的元素是同类型的,则比较其值,返回结果。
    2.如果两个元素不是同一种类型,则检查它们是否是数字。
    3.如果是数字,执行必要的数字强制类型转换,然后比较。
    4.如果有一方的元素是数字,则另一方的元素""(数字是"最小的")
    5.否则,通过类型名字的字母顺序进行比较。
    6.如果有一个列表首先到达末尾,则另一个长一点的列表""7.如果我们用尽了两个列表的元素而且所有元素都是相等的,那么结果就是个平局,就是说返回一个 0。

          比较实例:

    root@SJM:/home/sunjimeng/桌面# python
    Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
    [GCC 4.8.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> list1=['abcd',10]
    >>> list2=['accd',10]
    >>> list3=['abcd',11]
    >>> list4=['abcd',10]
    >>> cmp(list1,list2)
    -1
    >>> cmp(list1,list3)
    -1
    >>> cmp(list1,list4)
    0
    >>> list1=['adc',1,1]       #从第一个元素开始比较
    >>> list2=['adc',5]
    >>> cmp(list1,list2)
    -1

          2.min(list)和max(list)用于返回一个列表元素中的最大值和最小值:

          max(list)返回列表元素中的最大值;min(list)返回列表中的最小值;

          Python2.7.*版本允许存不同类型元素的列表大小值的比较,但Python3.5.*版本只允许相同元素的列表比较

    1.Python2.7.*:
    root@SJM:/home/sunjimeng/桌面# python Python 2.7.6 (default, Mar 22 2014, 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> list=['abc',True,10,20] >>> min(list),max(list) #Python2.7.*版本的最小值是布尔型,最大值是字符值 (True, 'abc') >>> exit()
    2.Python3.5.*: root@SJM:
    /home/sunjimeng/桌面# python3.5 Python 3.5.2 (default, Jul 17 2016, 11:52:15) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> list1=['abc',True,10,20] >>> list2=[10,20,30,40] >>> min(list1),max(list1) #当列表中存不同类型元素时不能比较 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: bool() < str() >>> min(list2),max(list2) (10, 40) >>> list3=[True,False] #False的值小于True >>> min(list3),max(list3) (False, True)

          3.len() 方法返回列表元素个数:

          两个版本一样的:

    1.Python2.7.*:
    root@SJM:/home/sunjimeng/桌面# python Python 2.7.6 (default, Mar 22 2014, 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> list=['abc',True,20] >>> len(list) 3 >>> exit()
    2.Python3.5.*: root@SJM:
    /home/sunjimeng/桌面# python3.5 Python 3.5.2 (default, Jul 17 2016, 11:52:15) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> list=['abc',True,20] >>> len(list) 3

          4.list(tuple)将一个元组转化为列表:

          两个版本一样的:

    1.Python2.7.*:
    root@SJM:/home/sunjimeng/桌面# python Python 2.7.6 (default, Mar 22 2014, 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> tuple=('abc',True,20) >>> list=list(tuple) >>> list ['abc', True, 20] >>> exit()
    2.Python3.5.*: root@SJM:
    /home/sunjimeng/桌面# python3.5 Python 3.5.2 (default, Jul 17 2016, 11:52:15) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> tuple=('abc',True,20) >>> list=list(tuple) >>> list ['abc', True, 20]

        2)方法:

        Python包含以下方法:

          1.Python2.7.*列表方法测试:

    root@SJM:/home/sunjimeng/桌面# python
    Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
    [GCC 4.8.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> list=['abc',20,'abc',True,9j]
    #1.append()追加元素到末尾
    >>> list.append(20)
    >>> list
    ['abc', 20, 'abc', True, 9j, 20]
    #2.count()计算20在列表中存在的个数
    >>> list.count('abc'),list.count(20)
    (2, 2)
    #3.extend()将另一个列表中的所有元素加到当前列表的末尾
    >>> list.extend(list)
    >>> list
    ['abc', 20, 'abc', True, 9j, 20, 'abc', 20, 'abc', True, 9j, 20]
    #4.index()返回首次匹配元素的下标
    >>> list.index(True)
    3
    #5.insert()在指定位置插入元素
    >>> list.insert(0,'abc')
    >>> list
    ['abc', 'abc', 20, 'abc', True, 9j, 20, 'abc', 20, 'abc', True, 9j, 20]
    #6.pop()删除并返回列表末尾元素
    >>> temp=list.pop()  #pop中不需要keyborder
    >>> temp
    20
    >>> list
    ['abc', 'abc', 20, 'abc', True, 9j, 20, 'abc', 20, 'abc', True, 9j]
    #7.remove()移除第一个匹配的对象
    >>> list.remove(20),list
    (None, ['abc', 'abc', 'abc', True, 9j, 20, 'abc', 20, 'abc', True, 9j])
    #8.reverse()将列表反向
    >>> list.reverse(),list
    (None, [9j, True, 'abc', 20, 'abc', 20, 9j, True, 'abc', 'abc', 'abc'])
    #9.将列表排序
    >>> list.sort(),list
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: no ordering relation is defined for complex numbers
    >>> list.remove(9j)
    >>> list
    [True, 'abc', 20, 'abc', 20, 9j, True, 'abc', 'abc', 'abc']
    >>> list.remove(9j)
    >>> list      #负数不能按大小排序
    [True, 'abc', 20, 'abc', 20, True, 'abc', 'abc', 'abc']
    >>> list.sort()
    >>> list
    [True, True, 20, 20, 'abc', 'abc', 'abc', 'abc', 'abc']

          2.Python3.5.*列表测试:

          除了不能使用sort()函数给存不同元素的列表排序之外,其余相同。

    root@SJM:/home/sunjimeng/桌面# python3.5
    Python 3.5.2 (default, Jul 17 2016, 11:52:15) 
    [GCC 4.8.4] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> list=['abv',20,45,True]
    >>> list_N=[10,30,20,40,0]
    >>> list.sort()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unorderable types: int() < str()
    >>> list_N.sort()
    >>> list_N
    [0, 10, 20, 30, 40]

        (3)元组:

        Python的元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号。元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可:

    >>> tuple=(98,'MenAngel',True,['name',10,'age',True])
    >>> tuple[3]
    ['name', 10, 'age', True]
    >>> tuple[3][1]=20
    >>> tuple[3]
    ['name', 20, 'age', True]
    >>> tuple[0]=True
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'tuple' object does not support item assignment

          1.创建空元组与只有一个元素的元组:(Python2.7.*与Python3.5.*相同):

    root@SJM:/home/sunjimeng/桌面# python
    Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
    [GCC 4.8.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> tuple1=()
    >>> tuple2=(20)     #这里代表int型
    >>> tuple3=(20,)    #这里代表tuple型
    >>> tuple4=(20,20)
    >>> tuple,tuple1,tuple2,tuple3
    (<type 'tuple'>, (), 20, (20,))
    >>> tuple1,tuple2,tuple3,tuple4
    ((), 20, (20,), (20, 20))

          2.元组的连接:(结合元素运算符)

          访问元组和访问列表的方法相同,但元组不允许修改,但可以对元组进行连接组合形成新的元组:

    >>> tuple1=(20,)
    >>> tuple2=('sex',20)
    >>> tuple1+tuple2
    (20, 'sex', 20)
    >>> tuple2*2
    ('sex', 20, 'sex', 20)

          3.元组的删除:

          元组中的单个元素是不允许删除的,只能删除整个元组。用del语句:

    >>> t1=('name=MenAngel', 'sex=True', 'age=19')
    >>> t1
    ('name=MenAngel', 'sex=True', 'age=19')
    >>> del t1
    >>> t1
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 't1' is not defined

          4.元组内置函数:

          Python元组包含了以下内置函数:

    root@SJM:/home/sunjimeng/桌面# python
    Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
    [GCC 4.8.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print 'abc',456,True       #默认为tuple,无关闭分隔符
    abc 456 True
    >>> t1=('age','sex',20,True)
    >>> t2=('aga','sss',90,True)
    >>> cmp(t1,t2)
    1
    >>> len(t1),len(t2)
    (4, 4)
    >>> max(t1),max(t2)
    ('sex', 'sss')
    >>> min(t1),min(t2)
    (True, True)
    >>> list=['age','sex',98,True,r'sg']
    >>> list
    ['age', 'sex', 98, True, 'sg']
    >>> tuple(list)
    ('age', 'sex', 98, True, 'sg')

          tuple没有sort()函数,因为tuple不允许修改。另外max和min函数用的时候Python2.7.*可以用在存不同类型元素的tuple中,但在Python3.5.*就不行了!

  • 相关阅读:
    Oracle 11g SQL Fundamentals Training Introduction02
    Chapter 05Reporting Aggregated data Using the Group Functions 01
    Chapter 01Restriicting Data Using The SQL SELECT Statemnt01
    Oracle 11g SQL Fundamentals Training Introduction01
    Chapter 04Using Conversion Functions and Conditional ExpressionsConditional Expressions
    Unix时代的开创者Ken Thompson (zz.is2120.bg57iv3)
    我心目中计算机软件科学最小必读书目 (zz.is2120)
    北京将评估分时分区单双号限行 推进错时上下班 (zz)
    佳能G系列领军相机G1X
    选购单反相机的新建议——心民谈宾得K5(转)
  • 原文地址:https://www.cnblogs.com/MenAngel/p/5691326.html
Copyright © 2011-2022 走看看