zoukankan      html  css  js  c++  java
  • day①:列表和元组

    列表

    列表是一组有序的数据集合,可以将各种各样的数据有序的存放在列表中,并且可以对其进行增删改查,以及遍历。

    列表内建方法:

    list.append(obj) # 向列表中添加一个对象obj
    list.count(obj) # 返回一个对象obj在列表中出现的次数
    list.extend(seq) # 把序列seq的内容添加到列表中
    list.index(obj,i=0,j=len(list)) # 返回list[k] == obj 的k值,并且k的范围在i<=k<j;否则异常
    list.insert(index.obj) # 在索引量为index的位置插入对象obj
    list.pop(index=-1) # 删除并返回指定位置的对象,默认是最后一个对象
    list.remove(obj) # 从列表中删除对象obj
    list.reverse() # 原地翻转列表
    list.sort(func=None,key=None,reverse=False) # 以指定的方式排序列表中成员,如果func和key参数指定,则按照指定的方式比较各个元素,如果reverse标志被置为True,则列表以反序排列

    例子:

    >>> shopping_list=['a','b','c','d','e','f']
    >>> shopping
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'shopping' is not defined
    >>> shopping_list
    ['a', 'b', 'c', 'd', 'e', 'f']
    
    ##切片
    >>> len(shopping_list) #通过len()内置函数查找列表元素个数
    6
    >>> shopping_list[2] #第三个元素
    'c'
    >>> shopping_list[0] #第一个元素
    'a'
    >>> shopping_list[-1] #最后一个元素
    'f'
    >>> shopping_list[-3] #倒数第三位元素
    'd'
    >>> shopping_list[-4] #倒数第四个元素
    'c'
    >>> shopping_list[8]  #超出范围
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: list index out of range
    >>> shopping_list[0:3] #取0到第三个元素,不包括第四个,0可以不写
    ['a', 'b', 'c']
    >>> shopping_list[:3] #同上 
    ['a', 'b', 'c']
    >>> shopping_list[2:5] #取第3至第5个元素
    ['c', 'd', 'e']
    >>> shopping_list[:-3] #取从0至倒数第3个元素
    ['a', 'b', 'c']
    >>> shopping_list[-3:] #取最后3个元素
    ['d', 'e', 'f']
    >>> shopping_list[1:6:2] #1-6,每隔2个元素取1个
    ['b', 'd', 'f']
    >>> shopping_list[::2]  #从头到尾每隔一个取一个
    ['a', 'c', 'e']
    
    
    ##增删改
    >>> shopping_list.append('g') #向列表后面追加一个元素
    >>> shopping_list
    ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    >>> shopping_list.pop() #删除最后一个元素
    'g'
    >>> shopping_list.remove('f') #删除叫”f“的元素,如果有多个f,那会从左边数找到的第一个
    >>> shopping_list[2]
    'c'
    >>> shopping_list[2]='C' #将索引为2的元素改成”C“,原来是小写的!
    >>> shopping_list
    ['a', 'b', 'C', 'd', 'e']
    >>> shopping_list.insert(3,"haha") #插入一个新元素,索引为3
    >>> shopping_list
    ['a', 'b', 'C', 'haha', 'd', 'e']
    >>> shopping_list.index('haha')  #返回‘haha’的索引值,如果有多个相同元素,则返回匹配的第一个
    3
    >>> shopping_list.append('haha') #再插多个进去
    >>> shopping_list
    ['a', 'b', 'C', 'haha', 'd', 'e', 'haha']
    >>> shopping_list.count('haha')  #统计‘haha’的元素个数
    2
    >>> list2=['h','i','j'] #创建一个新列表
    >>> shopping_list.extend(list2) #把上面的新列表合并到shopping_list
    >>> shopping_list
    ['a', 'b', 'C', 'haha', 'd', 'e', 'haha', 'h', 'i', 'j']
    >>> shopping_list.sort() #将列表排序
    >>> shopping_list
    ['C', 'a', 'b', 'd', 'e', 'h', 'haha', 'haha', 'i', 'j']
    >>> shopping_list.reverse() #将列表反转
    >>> shopping_list
    ['j', 'i', 'haha', 'haha', 'h', 'e', 'd', 'b', 'a', 'C']
    >>> del shopping_list[3:8] #删除索引3至8的元素,不包括8
    >>> shopping_list         
    ['j', 'i', 'haha', 'a', 'C']
    >>> for i in shopping_list: #遍历列表
    ...   print i
    ... 
    j
    i
    haha
    a
    C

    元组

    1.介绍:另一种有序列表叫元组。tupel一旦初始化就不能修改里面的元素。

       元组没有append()、extend()、remove()、pop()、index()、insert()这样的方法,其它获取元素的 方法和list是一样的。

         然而您可以使用in来查看一个元素是否存在于tuple中

    2、使用 tuple的好处:
    1) Tuple 比 list 操作速度快。如果您定义了一个值的常量集,并且唯一要用它做的是不断地遍历它,请使用 tuple 代替 list。
    2) 如果对不需要修改的数据进行 “写保护”,可以使代码更安全。

    例子1:

    >>> student=('yaobin','hy','nimei')
    >>> student[0]
    'yaobin'
    >>> student[1]
    'hy'
    >>> student[1]='hyhy]'  #不能修改
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'tuple' object does not support item assignment
    >>> t = (1,2)
    >>> t
    (1, 2)
    >>> t = () #空元组
    >>> t
    ()
    >>> t = (1) #定义的不是元组了,是1这个数
    >>> t
    1
    >>> t = (1,) #防止歧义,要加,
    >>> t
    (1,)          #这个才是元组

    例子2:可以修改元组的元素的元素

    >>> a=('a',[11,22,33]) #定义一个元组
    >>> a
    ('a', [11, 22, 33])
    >>> type(a)
    <type 'tuple'>
    >>> a[1]
    [11, 22, 33]
    >>> a[0]
    'a'
    >>> a[1][0]
    11
    >>> a[1][0]=111 #修改第二个元素列表里的第一个元素里面的东西
    >>> a
    ('a', [111, 22, 33])
    >>> a[1][1]=222  #修改第二个元素列表里的第二个元素里面的东西
    >>> a[1][2]=333  #修改第二个元素列表里的第三个元素里面的东西
    >>> a
    ('a', [111, 222, 333])
    >>> a[1]
    [111, 222, 333]
    >>> a[1]=[1111,2222,3333]   #你不能把这个列表改了,或者删了!
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'tuple' object does not support item assignment
  • 相关阅读:
    穷举、迭代、以及while代替for循环的使用
    for循环与for循环嵌套
    day07 数据类型补充
    day06
    day05
    day04
    python2 和 python3 的区别
    day03
    第一周笔记
    day02笔记
  • 原文地址:https://www.cnblogs.com/binhy0428/p/5083314.html
Copyright © 2011-2022 走看看