目录
  1. 1. 数据结构
    1. 1.1. 列表
      1. 1.1.1. 用于栈
    2. 1.2. 列表用于队列
      1. 1.2.1. queue
    3. 1.3. 列表推导式(列表生成式)
      1. 1.3.1. list-comprehensions
      2. 1.3.2. list-comprehensions1
      3. 1.3.3. list-comprehensions2
      4. 1.3.4. 嵌套列表生成式
    4. 1.4. del语句
    5. 1.5. 元组与序列
    6. 1.6. sets
    7. 1.7. 字典
  • 循环技术
    1. 0.1. 更多的条件语句
      1. 0.1.1. 比较类型
    2. 0.2. 序列与其他类型的比较
  • 数据结构

    数据结构

    列表

    • functions on lists
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    list.append(x) #向列表末尾添加元素,a【len(a):】=【x】
    list.extend(itrable) #使用一个序列扩展另一个序列,序列中的元素将添加到列表尾部a【len(a):】=itrable
    list.insert(i,x) #将x插入第i个位置
    list.remove(x) #删除x
    list.pop(【i】) #将第i个位置的数据退栈
    list.clear() #del a【:】
    list.index(x【,start盘【,end】】) #查找索引
    list.count(x) #返回x在列表中所在位置
    list.sort(key=none,reverse=false)#参数是用来自定义排序
    list.reverse() #将列表中元素颠倒
    list.copy() #复制列表
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    #!/usr/bin/env python
    # coding=utf-8
    fruits = ['orange','apple','pear','banana','kiwi','apple', 'banana']
    #fruits.count('apple')
    #fruits.count('tangerine')
    #fruits.index('banana')
    #fruits.index('banana',4) #从第4个位置开始查找banana
    #fruits.pop() #显示退栈数据,退栈是从列表尾部开始

    #实例
    print(fruits.count('apple'))
    print(fruits.count('tangerine'))
    print(fruits.index('banana'))
    print(fruits.index('banana',4))
    fruits.reverse() #颠倒顺序
    print(fruits)
    fruits.append('grape') #在列表末尾添加元素
    print(fruits)
    fruits.sort() #排序
    print(fruits)
    print(fruits.pop())
    • 输出
    1
    2
    3
    4
    5
    6
    7
    8
    2
    0
    3
    6
    ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']
    ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']
    ['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear']
    pear
    用于栈
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    #!/usr/bin/env python
    # coding=utf-8
    '''
    栈在列表中的应用
    '''
    stack = [3,4,5]
    #在列表末尾插入元素
    stack.append(6)
    print(stack)
    stack.append(7)
    print(stack)
    #显示弹出栈的元素
    stack.pop()
    stack.pop()
    • 输出
    1
    2
    [3, 4, 5, 6]
    [3, 4, 5, 6, 7]

    列表用于队列

    queue
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    #!/usr/bin/env python
    # coding=utf-8

    from collections import deque #被设计来从末尾更快速的append and pop元素
    queue = deque(["Eric","Join","Michae"])
    #从末尾增加元素
    queue.append("Terry")
    print(queue)
    queue.append("Graham")
    print(queue)
    #从队列左边退出队列
    queue.popleft()
    print(queue)
    queue.popleft()
    print(queue)
    • 输出
    1
    2
    3
    4
    deque(['Eric', 'Join', 'Michae', 'Terry'])
    deque(['Eric', 'Join', 'Michae', 'Terry', 'Graham'])
    deque(['Join', 'Michae', 'Terry', 'Graham'])
    deque(['Michae', 'Terry', 'Graham'])

    列表推导式(列表生成式)

    list-comprehensions
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    #!/usr/bin/env python
    # coding=utf-8
    '''
    列表生成式
    '''
    #example1
    squares = [] #构建一个空队列
    for x in range(10):
    squares.append(x**2)#将构建的列表中的元素用x解出再**运算
    #实例
    print(squares)

    #example2
    squares = list(map(lambda x:x*2,range(10))) #实际上结果和上面的一样
    print(squares)

    #example3
    squares = [x**2 for x in range(10)]#第三种表达形式
    print(squares)
    • 输出
    1
    2
    3
    [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
    [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
    [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
    list-comprehensions1
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    #!/usr/bin/env python
    # coding=utf-8
    #example1
    a = [(x,y) for x in [1,2,3] for y in [3,1,4] if x!=y]
    print(a) #生成以元组为子集的列表

    #example2

    combs = [] #构建空列表

    for x in [1,2,3]:
    for y in [3,1,4]:
    if x!=y:
    combs.append((x,y))

    print(combs)
    • 输出
    1
    2
    [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
    [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
    list-comprehensions2
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    #!/usr/bin/env python
    # coding=utf-8
    vec = [-4,-2,0,2,4]
    #创建新的列表,值加倍递增
    a = [x*2 for x in vec]
    print(a)

    #过滤列表中的负数
    b = [x for x in vec if x >= 0]
    print(b)

    #对所有元素应用abs()函数
    c = [abs(x) for x in vec]
    print(c)

    #在每个元素中调用方法
    freshfruit = ['banana','loganberry','passion fruit']
    d = [weapon.strip() for weapon in freshfruit]
    print(d)
    #创建一个有两个参数的二元组
    e = [(x , x**2) for x in range(6)]
    print(e)
    #用两个for循环将双重列表转转为列表
    vec = [[1,2,3],[4,5,6],[7,8,9]]
    f = [num for elem in vec for num in elem] #用elem将vec列表中的各个子集提取出来,用num将各个子集中的元素提取出来
    print(f)

    #包含合成生成式和嵌套函数的列表生成式
    from math import pi #求圆周率
    g = [str(round(pi,i)) for i in range(1,6)]
    print(g) #得出来的pi为字符串
    • 输出
    1
    2
    3
    4
    5
    6
    7
    [-8, -4, 0, 4, 8]
    [0, 2, 4]
    [4, 2, 0, 2, 4]
    ['banana', 'loganberry', 'passion fruit']
    [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
    [1, 2, 3, 4, 5, 6, 7, 8, 9]
    ['3.1', '3.14', '3.142', '3.1416', '3.14159']
    嵌套列表生成式
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    #!/usr/bin/env python
    # coding=utf-8
    '''
    嵌套列表生成式
    '''
    matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12],] #每个子集后面要加逗号
    a = [[row[i] for row in matrix] for i in range(4)]
    print(a)

    #等价于
    transposed = []
    for i in range(4):
    transposed.append([row[i] for row in matrix])
    print(transposed)

    #等价于
    transposed = [] #初始化
    for i in range(4):
    transposed_row=[]
    for row in matrix: #行初始化
    transposed_row.append(row[i])#往列表第row个元素的第i个位置增加元素
    transposed.append(transposed_row) #往列表的第i个位置增加元素
    print(transposed)

    #相似
    e = list(zip(*matrix))
    print(e)
    • 输出
    1
    2
    3
    4
    [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
    [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
    [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
    [(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]

    del语句

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    #!/usr/bin/env python
    # coding=utf-8
    '''
    删除语句
    '''
    a = [-1,0,1,2,3,4,77,99,3444]
    print(a)

    del a[0]
    print(a)

    #切片删除
    del a[2:4]
    print(a)

    #全部删除
    del a[:]
    print(a)

    #删除全部变量
    del a #输出错误,因为变量已经被删除
    • 输出
    1
    2
    3
    4
    [-1, 0, 1, 2, 3, 4, 77, 99, 3444]
    [0, 1, 2, 3, 4, 77, 99, 3444]
    [0, 1, 4, 77, 99, 3444]
    []

    元组与序列

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    #!/usr/bin/env python
    # coding=utf-8
    '''
    元组与序列
    '''
    print("example")
    t = 12345,54321,'hello' #元组可以只用逗号分开,不需要括号
    print(t[0])
    print(t)

    #元组嵌套
    u = t,(1,2,3,4,5)
    print(u)#嵌套后的元组成为有两个子集元组的大元组

    #元组值不可变,t[0]=9999(元组重置元素是不可行的,会报错)
    #元组可以包含可变的参数,如列表
    v = ([1,2,3],[3,2,1])
    print(v)


    print("example2")
    empty = ()
    sing = 'woow'
    singleton = 'hello', #后面多加一个逗号表示元组
    print(len(empty)) #显示empty长度
    print(len(sing)) #显示sing长度
    print(len(singleton))#显示singleton长度
    print(empty)
    print(sing)
    print(singleton)

    #在解释器中,如果,t = 1,2,3 ,可以颠倒,1,2,3=t
    • 输出
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    example
    12345
    (12345, 54321, 'hello')
    ((12345, 54321, 'hello'), (1, 2, 3, 4, 5))
    ([1, 2, 3], [3, 2, 1])
    example2
    0
    4
    1
    ()
    woow
    ('hello',)

    sets

    • set_example
      用花括号或者set()函数创建sets(创建空的sets需要用set()函数)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    #!/usr/bin/env python
    # coding=utf-8
    '''
    sets集合
    '''

    basket = {'apple','orange','apple','pear','orange','banana'} #用{}创建sets
    print(basket)#不重复输出元素

    #判断sets中的value是否在字典中
    a = 'orange' in basket
    print(a)
    b = 'cranfg' in basket
    print(b)

    #对两个单词的操作
    a = set('abracadabra')
    b = set('alacazam')
    print(a) #不重复输出a中的字母
    c = a - b #相减
    print(c)
    d = a | b #或
    print(d)
    e= a ^ b #取反
    print(e)
    f = a & b #取并集
    print(f)

    #用列表生成式
    g = {x for x in 'abracadabra' if x not in 'abc'} #输出不在abc范围中的字符串中的字母
    print(g)
    • 输出
    1
    2
    3
    4
    5
    6
    7
    8
    9
    {'apple', 'pear', 'banana', 'orange'}
    True
    False
    {'d', 'r', 'c', 'a', 'b'}
    {'d', 'r', 'b'}
    {'d', 'c', 'z', 'r', 'b', 'l', 'm', 'a'}
    {'d', 'z', 'r', 'b', 'l', 'm'}
    {'c', 'a'}
    {'d', 'r'}

    字典

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    #!/usr/bin/env python
    # coding=utf-8
    '''
    字典
    '''
    #字典与集合一样需要用花括号
    tel = {'jack':4098,'sape':4139}
    print(tel)
    tel['guido'] = 4127 #通过关键字添加元素
    print(tel)

    del tel['sape'] #通过关键字删除元素
    print(tel)

    tel['irv'] = 4098 #添加键值与字典中键值相同的元素
    print(tel)

    a = list(tel.keys())#列出字典中的关键字
    print(a)

    b = sorted(tel.keys())#将字典按关键字排序
    print(b)

    #两个相同键值的元素,只能识别一个
    c = 'jack' in tel
    d = 'irc' in tel
    e = 'guido' in tel
    print(c)
    print(d)
    print(e)

    #用dict()函数构建字典
    f = dict([('sape',4139),('guido',4127),('jack',4098)])
    print(f)

    #用于字典的生成式
    g = {x: x**2 for x in (2,4,6)} #关键字为(2,4,6),键值为x**2
    print(g)

    h = {x: x**2 for x in [2,4,6]}
    print(h)

    #用关键字参数
    i = dict(sape=4139,guido=4127,jack=4089)
    print(i)
    • 输出
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    {'jack': 4098, 'sape': 4139}
    {'jack': 4098, 'guido': 4127, 'sape': 4139}
    {'jack': 4098, 'guido': 4127}
    {'jack': 4098, 'guido': 4127, 'irv': 4098}
    ['jack', 'guido', 'irv']
    ['guido', 'irv', 'jack']
    True
    False
    True
    {'jack': 4098, 'guido': 4127, 'sape': 4139}
    {2: 4, 4: 16, 6: 36}
    {2: 4, 4: 16, 6: 36}
    {'jack': 4089, 'guido': 4127, 'sape': 4139}

    循环技术

    • looping through dictionaries

    • looping through sequence

    • loop over more sequence

    • loop over a sequence in reverse

    • loop over a sequence in sorted order

    • loop create a list

    • loop_dict

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    #!/usr/bin/env python
    # coding=utf-8
    '''
    用字典循环
    '''
    #looping through dictionaries
    print("looping through dictionaries:\n")
    knights = {'gallahad':'the pure','robin':'the brave'}
    for k,v in knights.items(): #items()用作显示字典键值
    print(k,v)

    #looping through sequence
    print("looping through sequence:\n")
    for i,n in enumerate(['tic','tac','toe']): #enumerate()函数在字典上是枚举、列举的意思
    print(i,n)

    #loop over more sequence
    print("loop over more sequence:\n")
    questions = ['name','quest','favorite color']
    answers = ['lancelot','the holy grail','blue']
    for q,a in zip(questions,answers): #zip函数接受任意多个序列做为参数,返回一个元组列表
    print('what is your{0}? it is {1}.'.format(q,a))#format格式化函数用{},:代替传统%

    #loop over a sequence in reverse
    print("to loop over a sequence in reverse:\n")
    for i in reversed(range(1,10,2)):#颠倒顺序
    print(i)

    #loop over a sequence in sorted order
    print("loop over a sequence in sorted order\n")
    basket = ['apple','orange','apple','pear','orange','banana']
    for f in sorted(set(basket)):#按照集合排序(用字母顺序排序),set输出不能重复
    print(f)

    #loop create a list
    print("loop create a list :\n")
    import math #导入数学公式
    raw_data = [56.2,float('NaN'),51.7,55.3,52.5,float('NaN'),47.8]
    filtered_data = [] #创建一个空列表
    for value in raw_data:
    if not math.isnan(value): #nan is not a number,这一句表示输出是number的
    filtered_data.append(value)
    print(filtered_data)
    • 输出
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    looping through dictionaries:

    robin the brave
    gallahad the pure
    looping through sequence:

    0 tic
    1 tac
    2 toe
    loop over more sequence:

    what is yourname? it is lancelot.
    what is yourquest? it is the holy grail.
    what is yourfavorite color? it is blue.
    to loop over a sequence in reverse:

    9
    7
    5
    3
    1
    loop over a sequence in sorted order

    apple
    banana
    orange
    pear
    loop create a list :

    [56.2, 51.7, 55.3, 52.5, 47.8]

    更多的条件语句

    • 用在while if语句中
    比较类型
    • in and not in 检查一个值是否在序列中
    • is and is not 比较 =两个对象是否相同
    • a小于b连等于c 几个比较可以被链接
    • (A and (not B)) or C 几个比较可以用布尔操作符联合
    1
    2
    3
    4
    5
    6
    7
    8
    #!/usr/bin/env python
    # coding=utf-8
    '''
    将比较的结果分配给变量
    '''
    string1,string2,string3='','Trondheim','Hammer Dance' #三个变量分别赋值
    non_null = string1 or string2 or string3 #比较三个变量中的数据,并赋值给另一个变量
    print(non_null)
    • 输出
    1
    Trondheim

    序列与其他类型的比较

    1
    2
    3
    4
    5
    6
    7
    (1, 2, 3)              < (1, 2, 4)
    [1, 2, 3] < [1, 2, 4]
    'ABC' < 'C' < 'Pascal' < 'Python'
    (1, 2, 3, 4) < (1, 2, 4)
    (1, 2) < (1, 2, -1)
    (1, 2, 3) == (1.0, 2.0, 3.0)
    (1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
    文章作者: rack-leen
    文章链接: http://yoursite.com/2019/05/03/Python/python-tutorial-notes/chapter5_data_structures/data-structures/
    版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 rack-leen's blog
    打赏
    • 微信
    • 支付宝

    评论