數 據 結 構
一、【清單】操作清單的方法如下:
清單是可變序列,通常用于存放同類項目的集合。
list_one = [1, 2, 3, 4, True, False, 'pig', 1, 1, 1, 1, 0, 0]
list_two = [1, 8, 10, 50, 400, 1000, 600, 2, 3, 99]
# 1、添加元素,在清單的末尾添加一個元素
list_one.append('U')
print(list_one)
# 2、擴充清單,使用可疊代對象中的所有元素進行擴充
list_one.extend(list_one)
print(list_one)
# 3、插入, 給指定位置插入元素
list_one.insert(1, 'A')
print(list_one)
# 4、移除,移除清單中第一個值,如果沒有就抛ValueError異常
list_one.remove('A')
print(list_one)
# 5、删除,删除給定位置的元素,如果沒有給定位置就預設删除清單中最後一個元素
list_one.pop(1) # 給定位置,删除的就是制定位置的元素
print(list_one)
list_one.pop() # 沒給定位置,預設删除清單中最後一個元素
print(list_one)
# 6、清空,清空清單中所有的元素
list_one.clear()
print(list_one)
# 7、索引,傳回清單中第一個元素的從零開始的索引
print(list_one.index(2))
print(list_one.index('pig', 6))
# 8、元素出現的次數
# True和False在清單中代表 1、0
print(list_one.count(0))
# 10、排序, 對清單中清單進行排序
# str類型和int類型之間不支援排序
list_two.sort() # 不傳參數正序排序
print(list_two)
list_two.sort(reverse=False) # reverse=False 正序排序
print(list_two)
list_two.sort(reverse=True) # reverse=True 逆序排序
print(list_two)
# 11、反轉清單中的元素
list_one.reverse()
print(list_one)
# 12、複制,淺拷貝
list_tree = list_one.copy()
print(list_tree)
# 清單推導式建立新清單
# 公式:[計算公式 for 循環 if 條件判斷]
squares = []
for i in range(5):
squares.append(i)
print(squares)
list_tour = [j+1 for j in range(5)]
print(list_tour)
list_five = [(x, y) for x in squares for y in list_tour if x != y]
print(list_five)
# del 語句,del語句從清單中移除切片或者清空整個清單
del list_one[0] # 移除一個元素
print(list_one)
del list_one[4:7] # 移除下标 4-7的元素
print(list_one)
del list_one[:] # 移除整個清單中的元素
print(list_one)
del list_one # 删除整個list_one變量
二、【元組】操作元組的方法如下:
元組是不可變序列,通常用于儲存異構資料的多項集
# 1、一個元組由幾個被都好隔開的值組成,例如:
tuple_one = 1234, 5463, 888
print('tuple_one類型:{}'.format(type(tuple_one)))
# 元組是不可變的,不允許修改裡面的值
# 元組再輸出時總要被圓括号包含,以便正确表示元組
# 空元組可以直接使用一對括号建立
# 含有一個元素的元組可以通過在這個元素後面添加一個逗号來建構
tuple_two = (1, 2, 'hello') # 元組
print('tuple_two類型:{}'.format(type(tuple_two)))
tuple_three = () # 空元組
print('tuple_three類型:{}'.format(type(tuple_three)))
tuple_four = ('hello baby',) # 元組
print('tuple_four類型:{}'.format(type(tuple_four)))
# 2、元組取值,直接使用下标及切片取值即可
print(tuple_one[0])
print(tuple_one[:2])
print(tuple_one[:])
三、【集合】操作集合的方法如下:
# 1、集合是由不重複的元素組成的無序的集,它會成員檢測并消除重複元素
basket = {'hello world', 'apple', 'orange', 'banana', 'orange'}
print('basket類型{}:'.format(type(basket)))
# 集合建立使用花括号及set(),如若建立空集合隻能使用set(),不能使用{}建立,後者是建立了一個空字典
# 集合set()中隻能放字元串(str)類型的元素
gather_one = set()
print('gather_one類型:{}'.format(type(gather_one)))
gather_two = set('hello')
print('gather_two類型:{}'.format(type(gather_two)))
# 集合推導式建立集合
gather_three = {z for z in 'abcdefghijk'}
print(gather_three)
四、【字典】操作字典的方法如下:
# 字典的鍵幾乎可以使任何值,但字典的鍵是不可變的
# 建立字典,字典可以通過将以逗号分隔的 鍵: 值 對清單包含于花括号之内來建立,也可以通過 dict 構造器來建立。
dict_one = {'jack': 4098, 'sjoerd': 4127}
print(dict_one)
dict_two = {4098: 'jack', 4127: 'sjoerd'}
print(dict_two)
dict_three = dict(one=1, wto=2, three=3) # 構造器建立字典
print(dict_three)
dict_four = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
print(dict_four)
dict_five = dict({'one': 1, 'two': 2, 'three': 3})
print(dict_five)
dict_six = {} # 建立空字典
print(dict_six)
print(list(dict_one)) # 傳回字典dict_one中使用的所有鍵的清單。
print(len(dict_one)) # 傳回字典dict_one中的項數
print(dict_one['jack']) # 傳回字典dict_one中'jack'的值,如果是不存在的key則會抛KeyError
dict_one['jack'] = 'hello' # 修改dict_one中'jack'的值
print(dict_one)
print(dict_one.copy()) # 淺複制dict_one字典
print(dict_one.get('jack')) # 取字典中的值,如果存在就是傳回值,不存在就傳回預設值,如果未給預設值則預設我None
dict_two.clear() # 清空字典
print(dict_two)
del dict_five['one'] # 将dict_five中的'one',從dict_one中移除,如果不存在就傳回KeyError
print(dict_five)
print(dict_four.items()) # 傳回由字典項 ((鍵, 值) 對) 組成的一個新視圖
print(dict_four.keys()) # 傳回由字典鍵組成的一個新視圖
print(dict_four.values()) # 傳回由字典值組成的一個新視圖
# 字典推導式建立字典
dict_seven = {x: y for x in [1, 2, 3, 4] for y in [4, 5, 6, 7]}
print(dict_seven)
标簽:python,list,two,元組,dict,print,清單,字典
來源: https://www.cnblogs.com/lifeng0402/p/11788760.html