python 基础教程之8 字典,元祖,list slices切片

linux python基础教程 python 3.5

Posted by ivo on March 20, 2018

0x17 Dictionaries 字典

  • 字典是一种数据结构,map keys to values.list 可以理解为一种特殊的字典,字典与list有相同的索引方式,[ ]
    >>> ages = {"Dave":24,"Mary":42,"John":58}
    >>> print(ages["Dave"])
    24
    >>> print(ages["John"])
    58
    
  • 键入一个没有的index keys,会报KeyError
    >>> primary = {
    ...     "red":[255,0,0],
    ...     "green":[0,255,0],
    ...     "blue":[0,0,255]
    ... }
    >>> print(primary[red])
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    NameError: name 'red' is not defined
    #key有""
    >>> print(primary["red"])
    [255, 0, 0]
    >>> print(primary["yellow"])
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    KeyError: 'yellow'
    
    >>> test = {}
    >>> print(test[0])
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    KeyError: 0
    
  • 只有不可变的对象才能当作key使用。
    >>> bad_dict = {
    ...     [1,2,3]:"one two three"
    ... }
    Traceback (most recent call last):
    File "<stdin>", line 2, in <module>
    TypeError: unhashable type: 'list'
    
  • 字典中的操作,替换值添加值
    >>> squares = {1:1,2:4,3:"error",4:16}
    >>> squares[8] = 125
    >>> squares[3] = 68
    >>> print(squares)
    {8: 125, 1: 1, 2: 4, 3: 68, 4: 16}
    
    >>> print(squares)
    {8: 125, 1: 1, 2: 4, 3: 68, 4: 16}
    >>> print(squares[squares[1]])
    1
    
  • 使用in或not in 来确定key是否在字典中
    >>> nums = {
    ...     1:"one",
    ...     2:'two',
    ...     3:'three'
    ... }
    >>> print(1 in nums)
    True
    >>> print(5 in nums)
    False
    >>> print(5 not in nums)
    True
    
  • 字典中一个常用的方法是get,它像index一样,但出现不存在的key的时候会返回特殊的值,默认为None
    >>> pairs = {1:'apples',
    ...     'orange':[1,2,4],
    ...     True:False,
    ...     None:'True'
    ... }
    >>> print(pairs.get('orange'))
    [1, 2, 4]
    >>> print(pairs.get(7))
    None
    >>> print(pairs.get('apples'))
    None
    >>> print(pairs.get(12345,"not in dictionary"))
    not in dictionary
    
    >>> fib = {1:1,2:1,3:2,4:3}
    >>> print(fib.get(4,0) + fib.get(7,5))
    8
    #get(4,0) return 3       get(7,5) because 7 not exist so return 5
    
  • 字典中的每一个元素都是由 key:value来表示
  • 创建空字典用{}

    0x18 tuples 元祖

  • tuples与list极其相似,tuples是不可变的。它用()创建
    >>> words = ('spam','eggs','sausages')
    >>> print(words[0])
    spam
    >>> words[0] = 'good'
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: 'tuple' object does not support item assignment
    
  • tuples可以不用()创建,只要将values用,隔开即可
    >>> words = 'spam','eggs','sausages'
    >>> print(words[0])
    spam
    
  • 创建一个空的tuple用()
    tpl = ()
    
    >>> tpl = (1,(1,2,3))
    >>> print(tpl[1])
    (1, 2, 3)
    >>> print(tpl[1][0])
    1
    >>> print(tpl[1][1])
    2
    >>> print(tpl[1][2])
    3
    >>> print(tpl[1][2][0])
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: 'int' object is not subscriptable
    
  • tuples are faster than list,but they cannot be change
  • tuples与list dictionary 一样都能够被互相嵌套

0x19 list slices 切片

  • list slices是一种高级的list操作。写法是中括号中用:隔开的两个整数
    >>> squares = [0,1,4,9,16,25,36,47,64,81]
    >>> print(squares[2:6])
    [4, 9, 16, 25]
    >>> print(squares[3:8])
    [9, 16, 25, 36, 47]
    >>> print(squares[0:1])
    [0]
    # 第一个index的数包含在里面,第二个没有。和range一样
    
  • 第一个数被省略的话从头开始算,第二个省略的话从数到尾
    >>> squares = [0,1,4,9,16,25,36,47,64,81]
    >>> print(squares[:4])
    [0, 1, 4, 9]
    >>> print(squares[4:])
    [16, 25, 36, 47, 64, 81]
    
  • slicing 在tuple上面也能使用
  • slices 可添加3个参数,第3个参数代表step
    >>> squares = [0,1,4,9,16,25,36,47,64,81]
    >>> print(squares[::2])
    [0, 4, 16, 36, 64]
    >>> print(squares[2:8:2])
    [4, 16, 36]
    >>> print(squares[2:8:3])
    [4, 25]
    >>> print(squares[2:8:4])
    [4, 36]
    >>> print(squares[2:8:5])
    [4, 47]
    >>> print(squares[2:8:6])
    [4]
    
  • slices中的负数代表从list的尾部算
    >>> squares = [0,1,4,9,16,25,36,47,64,81]
    >>> print(squares[1:-1])
    [1, 4, 9, 16, 25, 36, 47, 64]
    >>> print(squares[1:-2])
    [1, 4, 9, 16, 25, 36, 47]
    >>> print(squares[-5:-2])
    [25, 36, 47]
    
  • slice中step的负数,代表向后完成,通常用[::-1]反转list
    >>> squares = [0,1,4,9,16,25,36,47,64,81]
    >>> print(squares[::-1])
    [81, 64, 47, 36, 25, 16, 9, 4, 1, 0]
    >>> print(squares[::-2])
    [81, 47, 25, 9, 1]
    >>> print(squares[::-5])
    [81, 16]
    >>> print(squares[7:2:-2])
    [47, 25, 9]
    >>> print(squares[7:2:-3])
    [47, 16]