• Python之列表&元组&字典


    今天学习了Python的基本数据类型,做以下笔记,以备查用。

    一、列表

    列表的常用方法:

    1、append()方法

    def append(self, p_object): # real signature unknown; restored from __doc__
        """ L.append(object) -- append object to end """
        pass

    append()方法可以在列表尾部追加一个元素,该方法每次只能接受一个参数,并且该参数可以是任何数据类型。

    e:

    >>> a = [1,2,3,4,5,6]
    >>> a.append(7)
    >>> a
    [1, 2, 3, 4, 5, 6, 7]

    2、extend()方法

    def extend(self, iterable): # real signature unknown; restored from __doc__
            """ L.extend(iterable) -- extend list by appending elements from the iterable """
            pass

    extend()方法的参数是一个列表,并将该列表中的每个元素追加到列表中。

    e:

    >>> b = [1,2,3,4]
    >>> b.extend([5,6,7])
    >>> b
    [1, 2, 3, 4, 5, 6, 7]

    3、index()方法

    def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
            """
            L.index(value, [start, [stop]]) -> integer -- return first index of value.
            Raises ValueError if the value is not present.
            """
            return 0

    index()方法返回其参数在列表中的位置,(元素索引从0开始)

    e:

    >>> a = [1,4,7,9,3,6]
    >>> a.index(3)
    4

    4、insert()方法

    def insert(self, index, p_object): # real signature unknown; restored from __doc__
            """ L.insert(index, object) -- insert object before index """
            pass

    insert()方法向列表中插入一个值,接受两个参数,第一个参数指定新插入值的位置,第二个参数为插入的值。

    e:

    >>> a = [1,3,5,8,0]
    >>> a.insert(2,22)
    >>> a
    [1, 3, 22, 5, 8, 0]

    5、pop()方法

    def pop(self, index=None): # real signature unknown; restored from __doc__
            """
            L.pop([index]) -> item -- remove and return item at index (default last).
            Raises IndexError if list is empty or index is out of range.
            """
            pass

    pop()方法接受一个参数,删除列表中以该参数为索引的值,然后返回被删除的值。若不带参数,默认删除列表中最后一个元素。

    e:

    >>> a = [1,2,3,4,5]
    >>> a.pop(3)
    4
    >>> a
    [1, 2, 3, 5]

    6、remove()方法

    def remove(self, value): # real signature unknown; restored from __doc__
            """
            L.remove(value) -- remove first occurrence of value.
            Raises ValueError if the value is not present.
            """
            pass

    remove()方法接受一个参数,删除列表中对应的值。

    e:

    >>> a
    [1, 2, 3, 5]
    >>> a.remove(5)
    >>> a
    [1, 2, 3]

    7、count()方法

    def count(self, value): # real signature unknown; restored from __doc__
            """ L.count(value) -> integer -- return number of occurrences of value """
            return 0

    count()方法接受一个参数,返回该参数在列表中的个数。

    e:

    >>> a = [1,3,5,7,3,2,4,5]
    >>> a.count(3)
    2
    >>> a.count(5)
    2

    8、reverse()方法

    def reverse(self): # real signature unknown; restored from __doc__
            """ L.reverse() -- reverse *IN PLACE* """
            pass

    reverse()方法没有参数,对列表中的元素方向排序

    e:

    >>> a = [1,2,3,4,5]
    >>> a.reverse()
    >>> a
    [5, 4, 3, 2, 1]

    9、sort()方法

    def sort(self, cmp=None, key=None, reverse=False): # real signature unknown; restored from __doc__
            """
            L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
            cmp(x, y) -> -1, 0, 1
            """
            pass

    sort()方法对列表中的元素进行排序

    e:

    >>> a = [2,5,8,3,4,9,1]
    >>> a.sort()
    >>> a
    [1, 2, 3, 4, 5, 8, 9]

    二、元组

    元组里的元素不可修改,元素的元组可以修改。

    e:

    元素不可修改:

    >>> a = (1,2,3,[1,2,3])
    >>> a[0]=4
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'tuple' object does not support item assignment

    元素包含的元组可以修改:

    >>> a = (1,2,3,[1,2,3])
    >>> a[3]
    [1, 2, 3]
    >>> a[3][0]=4
    >>> a
    (1, 2, 3, [4, 2, 3])

    元组的常用方法:

    1、count()方法

    def count(self, value): # real signature unknown; restored from __doc__
            """ T.count(value) -> integer -- return number of occurrences of value """
            return 0

    count()方法接受一个参数,返回该参数在元组里的个数

    e:

    >>> a = (1,2,3,4,5,2,5,3,4,2,6,)
    >>> a.count(2)
    3
    >>> a.count(5)
    2

    2、index()方法

    def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
            """
            T.index(value, [start, [stop]]) -> integer -- return first index of value.
            Raises ValueError if the value is not present.
            """
            return 0

    index()方法接受一个参数,返回该参数在元组第一次出现时的位置(索引)

    e:

    >>> a = (1,2,3,4,5,2,5,3,4,2,6,)
    >>> a.index(4)
    3

    三、字典

    字典的常用方法:

    1、clear()方法

    def clear(self): # real signature unknown; restored from __doc__
            """ D.clear() -> None.  Remove all items from D. """
            pass

    clear()方法清空字典里的所有元素。

    e:

    >>> d = {1:'ahaii',2:'nancy',3:'tom'}
    >>> d.clear()
    >>> d
    {}

    2、copy()方法

    def copy(self): # real signature unknown; restored from __doc__
            """ D.copy() -> a shallow copy of D """
            pass

    copy()方法对字典进行浅复制。

    e:

    >>> d = {1:'ahaii',2:'nancy',3:'tom'}>>> a = d.copy()
    >>> a
    {1: 'ahaii', 2: 'nancy', 3: 'tom'}

    3、get()方法

    def get(self, k, d=None): # real signature unknown; restored from __doc__
            """ D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None. """
            pass

    get()方法返回指定键的值,若改键不存在,则返回默认值或空。

    e:

    >>> a = {1: 'ahaii', 2: 'nancy', 3: 'tom'}
    >>> a.get(2)
    'nancy'
    >>> a.get(4)
    >>> 

    4、has_key()方法

    def has_key(self, k): # real signature unknown; restored from __doc__
            """ D.has_key(k) -> True if D has a key k, else False """
            return False

    has_key()方法判断参数是否包含在字典的key中,若存在,返回True,若不存在,返回False。

    e:

    >>> a = {1: 'ahaii', 2: 'nancy', 3: 'tom'}>>> a.has_key(1)
    True
    >>> a.has_key(4)
    False

    5、items()方法

    def items(self): # real signature unknown; restored from __doc__
            """ D.items() -> list of D's (key, value) pairs, as 2-tuples """
            return []

    items()方法返回一个列表,列表中的每个元素是字典的一个键值对组成的元组。

    e:

    >>> a = {1: 'ahaii', 2: 'nancy', 3: 'tom'}
    >>> a.items()
    [(1, 'ahaii'), (2, 'nancy'), (3, 'tom')]

    6、keys()方法

    def keys(self): # real signature unknown; restored from __doc__
            """ D.keys() -> list of D's keys """
            return []

    keys()方法返回一个列表,该列表包含字典所有的key。

    e:

    >>> a = {1: 'ahaii', 2: 'nancy', 3: 'tom'}
    >>> a.keys()
    [1, 2, 3]

    7、pop()方法

    def pop(self, k, d=None): # real signature unknown; restored from __doc__
            """
            D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
            If key is not found, d is returned if given, otherwise KeyError is raised
            """
            pass

    pop()方法必须有一个参数(注意与列表中pop()方法的区别),并删除该参数在字典中对应的值。

    e:

    >>> a = {1: 'ahaii', 2: 'nancy', 3: 'tom'}
    >>> a.pop(1)
    'ahaii'
    >>> a
    {2: 'nancy', 3: 'tom'}

    8、values()方法

    def values(self): # real signature unknown; restored from __doc__
            """ D.values() -> list of D's values """
            return []

    values()方法返回包含字典所有值的列表。

    e:

    >>> a = {1: 'ahaii', 2: 'nancy', 3: 'tom'}
    >>> a.values()
    ['ahaii', 'nancy', 'tom']

     9、itmes()方法

    使用items()方法可以取字典中的键值对。

    e:

    dic = {'1':'qwe','2':'uyd','3':'uhd'}
    for k,v in dic.items():
            print k,v
  • 相关阅读:
    Python的传递引用
    kafka的ACK
    分布式事务
    Java中的锁
    docker笔记
    MySQL数据库优化
    Centos7使用yum命令安装Mysql5.6.X
    ubuntu16.04安装workbench
    ubuntu下IDEA配置tomcat报错Warning the selected directory is not a valid tomcat home
    ubuntu配置JDK
  • 原文地址:https://www.cnblogs.com/ahaii/p/5121398.html
Copyright © 2020-2023  润新知