Python学习笔记之基本数据结构方法

通用序列操作:

  • 索引,序列中元素从0开始递增,这些元素可以通过编号访问
  • 分片,使用索引只能访问单个元素,分片操作可以访问一定范围内的元素。list[a:b]:a和b是两个索引作为边界,包含索引a对应函数,不包含b
  • 序列相加,两种相同的序列才能进行连接操作
    >>> a = [1,2,3]
    >>> b = [4,5,6]
    >>> a + b
    [1, 2, 3, 4, 5, 6]
    >>> 'hello' + 'world'
    'helloworld'

     

  • 乘法:用数字n乘一个序列会得到一个新的序列,新的序列中原来的序列将被重复n次。
    >>> a * 5
    [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

    利用乘法和none可以对列表进行初始化

    >>> a = [None] * 10
    >>> a
    [None, None, None, None, None, None, None, None, None, None]
  • 成员资格in:检查一个值是否在列表中
    >>> str = 'hello'
    >>> 'h' in str
    True
  • 长度(len)、最小值(min)和最大值(max)

list:

  • 元素赋值  
    >>> x=[1,1,1]
    >>> x[1]=2
    >>> x
    [1, 2, 1]
  • 删除元素
    >>> del x[1]
    >>> x
    [1, 1]
  • 分片赋值,可用于替换,删除,插入
    >>> x[:]=[4,5,6]
    >>> x
    [4, 5, 6]
  • 列表方法,append(以下都是列表方法):在列表末尾追加新的对象,直接修改原来的列表
    >>> x.append(5)
    >>> x
    [4, 5, 6, 5]
  • count:统计某个元素在列表中出现的次数
    >>> x.count(5)
    2
  • extend:在列表末尾一次性追加另一个序列中多个值
    >>> y = [1,2,3]
    >>> y.extend(x)
    >>> y
    [1, 2, 3, 4, 5, 6, 5]
  • index:找出某个值第一个匹配项的索引位置
    >>> a = ['hello','python','world']
    >>> a.index('hello')
    0
  • insert:将对象插入到列表中
    >>> a.insert(0,'hi')
    >>> a
    ['hi', 'hello', 'python', 'world']
  • pop:移除列表中一个元素并返回该元素,默认最后一个
    >>> a
    ['hi', 'hello', 'python', 'world']
    >>> a.pop()
    'world'
    >>> a.pop(1)
    'hello'
  • remove:用于移除列表中某个值的第一个匹配项,修改列表且没有返回值
    >>> a
    ['hi']
    >>> a.remove('hi')
    >>> a
    []
  • reverse:将列表中的元素反向存放
    >>> a = [1,3,4]
    >>> a.reverse()
    >>> a
    [4, 3, 1]
  • sort:对列表进行排序,无返回值,改变原列表内容,sorted函数返回一个排好序的列表
    >>> a
    [4, 3, 1]
    >>> a.sort()
    >>> a
    [1, 3, 4]

     sort方法有两个可选参数,key和reverse,参数key提供一个在排序过程中使用的函数,这个函数为每一个元素创建一个键,所有元素根据键来排序。

    >>> x = ['add','subl','hello','to']
    >>> x.sort(key = len)
    >>> x
    ['to', 'add', 'subl', 'hello']

    reverse参数为true或是false,用来表明是否要进行反向排序

    >>> x.sort(key = len,reverse = True)
    >>> x
    ['hello', 'subl', 'add', 'to']

     

元组:不可变序列

 

  • tuple函数:以一个序列作为参数,并把它转换为元组
    >>> tuple('abc')
    ('a', 'b', 'c')

     

字符串:

  • find方法:在一个较长的字符串中查找子串,返回子串所在位置最左端索引,没有则返回-1。可提供匹配起点和结束点,包含起点不含终点。
    >>> 'hello world'.find(' ')
    5

     

  • join方法:连接序列中的元素,被连接的序列元素要是字符串
    >>> str1='+'
    >>> str2=['1','2','3']
    >>> str1.join(str2)
    '1+2+3'
    >>> str2.join(str1)
    Traceback (most recent call last):
      File "<pyshell#12>", line 1, in <module>
        str2.join(str1)
    AttributeError: 'list' object has no attribute 'join'

     

  • replace:返回字符串所有匹配项被替换之后得到字符串
    >>> 'it\'s a string'.replace('s','r')
    "it'r a rtring"

     

  • split:join的逆方法,将字符串分割成序列
    >>> '1/2/4/5/6'.split('/')
    ['1', '2', '4', '5', '6']

     

  • strip:去除两侧空格的字符串,也可以指定需要去除的参数
    >>> '        hi        '.strip()
    'hi'

    >>> ' !!!hi!!! '.strip(' !')
    'hi'

     

  • translate:替换字符串中某些部分,只处理单个字符,可以同时进行多个替换
    >>> s = 'hello'
    >>> table = s.maketrans('el','le')
    >>> s.translate(table)
    'hleeo'

     

字典:

  • clear:清除字典中所有项,无返回值
    >>> d
    {'name': 'joy', 'age': 14}
    >>> d.clear()
    >>> d
    {}

     

  • copy:返回一个具有相同键-值对的新字典,浅复制
    >>> a = {'name':'admin','age':[1,2,3]}
    >>> b = a.copy()
    >>> b['name'] = 'name'
    >>> b
    {'name': 'name', 'age': [1, 2, 3]}
    >>> a
    {'name': 'admin', 'age': [1, 2, 3]}
    >>> b['age'].remove(3)
    >>> a
    {'name': 'admin', 'age': [1, 2]}
    >>> b
    {'name': 'name', 'age': [1, 2]}

    深复制

    >>> from copy import deepcopy
    >>> c = deepcopy(b)
    >>> c
    {'name': 'name', 'age': [1, 2]}
    >>> c['age'].remove(1)
    >>> c
    {'name': 'name', 'age': [2]}
    >>> b
    {'name': 'name', 'age': [1, 2]}

     

  • get:在访问不存在项时返回none
    >>> d = {'name':'a','age':7}
    >>> d.get('name')
    'a'
    >>> d.get('na')
    >>> 

     

  • items:
    >>> x = d.items()
    >>> x
    dict_items([('name', 'a'), ('age', 7)])
    >>> list(x)
    [('name', 'a'), ('age', 7)]

     

  • keys:返回针对键的迭代器
    >>> d.keys()
    dict_keys(['name', 'age'])

     

  • pop:
    >>> d.pop('age')
    7
    >>> d
    {'name': 'a'}
    >>> 

     

  • values:返回字典中的值
posted @ 2017-08-21 17:13  HHello_World  阅读(220)  评论(0编辑  收藏  举报