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

数 据  结 构

  1 # 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 # 一、添加元素,在列表的末尾添加一个元素
  8 list_one.append('U')
  9 print(list_one)
 10 
 11 # 二、扩展列表,使用可迭代对象中的全部元素进行扩展
 12 list_one.extend(list_one)
 13 print(list_one)
 14 
 15 # 三、插入, 给指定位置插入元素
 16 list_one.insert(1, 'A')
 17 print(list_one)
 18 
 19 # 四、移除,移除列表中第一个值,若是没有就抛ValueError异常
 20 list_one.remove('A')
 21 print(list_one)
 22 
 23 # 五、删除,删除给定位置的元素,若是没有给定位置就默认删除列表中最后一个元素
 24 list_one.pop(1)     # 给定位置,删除的就是制定位置的元素
 25 print(list_one)
 26 list_one.pop()      # 没给定位置,默认删除列表中最后一个元素
 27 print(list_one)
 28 
 29 # 六、清空,清空列表中全部的元素
 30 list_one.clear()
 31 print(list_one)
 32 
 33 # 七、索引,返回列表中第一个元素的从零开始的索引
 34 print(list_one.index(2))
 35 print(list_one.index('pig', 6))
 36 
 37 # 八、元素出现的次数
 38 # True和False在列表中表明 一、0
 39 print(list_one.count(0))
 40 
 41 # 十、排序, 对列表中列表进行排序
 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 # 十一、反转列表中的元素
 51 list_one.reverse()
 52 print(list_one)
 53 
 54 # 十二、复制,浅拷贝
 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 # 2、【元组】操做元组的方法以下:
 82 # 元组是不可变序列,一般用于储存异构数据的多项集
 83 
 84 # 一、一个元组由几个被都好隔开的值组成,例如:
 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 # 二、元组取值,直接使用下标及切片取值便可
100 print(tuple_one[0])
101 print(tuple_one[:2])
102 print(tuple_one[:])
103 
104 # 3、【集合】操做集合的方法以下:
105 # 一、集合是由不重复的元素组成的无序的集,它会成员检测并消除重复元素
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 # 4、【字典】操做字典的方法以下:
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)

 

学习总结,仅供参考,若有疑义,欢迎找茬......
相关文章
相关标签/搜索