python学习之列表、元组、集合、字典随笔

数 据  结 构

  1 # 一、【列表】操作列表的方法如下:
  2 # 列表是可变序列,通常用于存放同类项目的集合。
  3 
  4 list_one = [1, 2, 3, 4, True, False, 'pig', 1, 1, 1, 1, 0, 0]
  5 list_two = [1, 8, 10, 50, 400, 1000, 600, 2, 3, 99]
  6 
  7 # 1、添加元素,在列表的末尾添加一个元素
  8 list_one.append('U')
  9 print(list_one)
 10 
 11 # 2、扩展列表,使用可迭代对象中的所有元素进行扩展
 12 list_one.extend(list_one)
 13 print(list_one)
 14 
 15 # 3、插入, 给指定位置插入元素
 16 list_one.insert(1, 'A')
 17 print(list_one)
 18 
 19 # 4、移除,移除列表中第一个值,如果没有就抛ValueError异常
 20 list_one.remove('A')
 21 print(list_one)
 22 
 23 # 5、删除,删除给定位置的元素,如果没有给定位置就默认删除列表中最后一个元素
 24 list_one.pop(1)     # 给定位置,删除的就是制定位置的元素
 25 print(list_one)
 26 list_one.pop()      # 没给定位置,默认删除列表中最后一个元素
 27 print(list_one)
 28 
 29 # 6、清空,清空列表中所有的元素
 30 list_one.clear()
 31 print(list_one)
 32 
 33 # 7、索引,返回列表中第一个元素的从零开始的索引
 34 print(list_one.index(2))
 35 print(list_one.index('pig', 6))
 36 
 37 # 8、元素出现的次数
 38 # True和False在列表中代表 1、0
 39 print(list_one.count(0))
 40 
 41 # 10、排序, 对列表中列表进行排序
 42 # str类型和int类型之间不支持排序
 43 list_two.sort()     # 不传参数正序排序
 44 print(list_two)
 45 list_two.sort(reverse=False)    # reverse=False 正序排序
 46 print(list_two)
 47 list_two.sort(reverse=True)     # reverse=True 逆序排序
 48 print(list_two)
 49 
 50 # 11、反转列表中的元素
 51 list_one.reverse()
 52 print(list_one)
 53 
 54 # 12、复制,浅拷贝
 55 list_tree = list_one.copy()
 56 print(list_tree)
 57 
 58 
 59 # 列表推导式创建新列表
 60 # 公式:[计算公式 for 循环 if 条件判断]
 61 squares = []
 62 for i in range(5):
 63     squares.append(i)
 64 print(squares)
 65 
 66 list_tour = [j+1 for j in range(5)]
 67 print(list_tour)
 68 
 69 list_five = [(x, y) for x in squares for y in list_tour if x != y]
 70 print(list_five)
 71 
 72 # del 语句,del语句从列表中移除切片或者清空整个列表
 73 del list_one[0]     # 移除一个元素
 74 print(list_one)
 75 del list_one[4:7]   # 移除下标 4-7的元素
 76 print(list_one)
 77 del list_one[:]     # 移除整个列表中的元素
 78 print(list_one)
 79 del list_one        # 删除整个list_one变量
 80 
 81 # 二、【元组】操作元组的方法如下:
 82 # 元组是不可变序列,通常用于储存异构数据的多项集
 83 
 84 # 1、一个元组由几个被都好隔开的值组成,例如:
 85 tuple_one = 1234, 5463, 888
 86 print('tuple_one类型:{}'.format(type(tuple_one)))
 87 
 88 # 元组是不可变的,不允许修改里面的值
 89 # 元组再输出时总要被圆括号包含,以便正确表示元组
 90 # 空元组可以直接使用一对括号创建
 91 # 含有一个元素的元组可以通过在这个元素后面添加一个逗号来构建
 92 tuple_two = (1, 2, 'hello')  # 元组
 93 print('tuple_two类型:{}'.format(type(tuple_two)))
 94 tuple_three = ()    # 空元组
 95 print('tuple_three类型:{}'.format(type(tuple_three)))
 96 tuple_four = ('hello baby',)    # 元组
 97 print('tuple_four类型:{}'.format(type(tuple_four)))
 98 
 99 # 2、元组取值,直接使用下标及切片取值即可
100 print(tuple_one[0])
101 print(tuple_one[:2])
102 print(tuple_one[:])
103 
104 # 三、【集合】操作集合的方法如下:
105 # 1、集合是由不重复的元素组成的无序的集,它会成员检测并消除重复元素
106 basket = {'hello world', 'apple', 'orange', 'banana', 'orange'}
107 print('basket类型{}:'.format(type(basket)))
108 
109 # 集合创建使用花括号及set(),如若创建空集合只能使用set(),不能使用{}创建,后者是创建了一个空字典
110 # 集合set()中只能放字符串(str)类型的元素
111 gather_one = set()
112 print('gather_one类型:{}'.format(type(gather_one)))
113 gather_two = set('hello')
114 print('gather_two类型:{}'.format(type(gather_two)))
115 
116 # 集合推导式创建集合
117 gather_three = {z for z in 'abcdefghijk'}
118 print(gather_three)
119 
120 # 四、【字典】操作字典的方法如下:
121 
122 # 字典的键几乎可以使任何值,但字典的键是不可变的
123 # 创建字典,字典可以通过将以逗号分隔的 键: 值 对列表包含于花括号之内来创建,也可以通过 dict 构造器来创建。
124 dict_one = {'jack': 4098, 'sjoerd': 4127}
125 print(dict_one)
126 dict_two = {4098: 'jack', 4127: 'sjoerd'}
127 print(dict_two)
128 dict_three = dict(one=1, wto=2, three=3)    # 构造器创建字典
129 print(dict_three)
130 dict_four = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
131 print(dict_four)
132 dict_five = dict({'one': 1, 'two': 2, 'three': 3})
133 print(dict_five)
134 dict_six = {}   # 创建空字典
135 print(dict_six)
136 
137 print(list(dict_one))           # 返回字典dict_one中使用的所有键的列表。
138 print(len(dict_one))            # 返回字典dict_one中的项数
139 print(dict_one['jack'])         # 返回字典dict_one中'jack'的值,如果是不存在的key则会抛KeyError
140 dict_one['jack'] = 'hello'      # 修改dict_one中'jack'的值
141 print(dict_one)
142 print(dict_one.copy())          # 浅复制dict_one字典
143 print(dict_one.get('jack'))     # 取字典中的值,如果存在就是返回值,不存在就返回默认值,如果未给默认值则默认我None
144 dict_two.clear()                # 清空字典
145 print(dict_two)
146 del dict_five['one']            # 将dict_five中的'one',从dict_one中移除,如果不存在就返回KeyError
147 print(dict_five)
148 print(dict_four.items())        # 返回由字典项 ((键, 值) 对) 组成的一个新视图
149 print(dict_four.keys())         # 返回由字典键组成的一个新视图
150 print(dict_four.values())       # 返回由字典值组成的一个新视图
151 
152 # 字典推导式创建字典
153 dict_seven = {x: y for x in [1, 2, 3, 4] for y in [4, 5, 6, 7]}
154 print(dict_seven)

 

学习总结,仅供参考,如有疑义,欢迎找茬......
 
posted @ 2019-11-04 20:56  一名小测试  阅读(231)  评论(0编辑  收藏  举报