python包含多个元组的元组_Python数据结构(元组,列表,字典)
Python內(nèi)置了 幾種數(shù)據(jù)結(jié)構(gòu),元組,列表 字典
1.元組
元組可以由不同的元素組成,所有元素通過(guò)圓括號(hào)( ?)包含起來(lái),并通過(guò)逗號(hào)","隔開.如變量名 = (元素1,元素2,...),如果a =1,3,56,'abc','aoe',也默認(rèn)a是元組
每個(gè)元素也可以是不同的數(shù)據(jù)類型,字符串,數(shù)字,元組,列表,字典
元組的元素不能修改,也不能刪除,可以將元組整個(gè)刪除,使用del 元組,如刪除元組tuple使用del tuple
當(dāng)元組只有一個(gè)元素時(shí),第一個(gè)元素后面跟上逗號(hào)"," .如,tuple只有一個(gè)元素50時(shí) ,tuple = (50,)
元組的元素以索引編號(hào),從0開始
讀取單個(gè)元素使用元組[m]的方式,m是索引編號(hào),倒數(shù)第一個(gè)元素元組[-1]
讀取多個(gè)元素使用"元組[m:n]"的方式,m和n就是索引的序號(hào),代表讀取元組從m到n的元素,但不包括n這個(gè)元素.例如:元組[1,-1]指索引從1開始到索引為-1但不包含-1的元素.
步長(zhǎng)打印 元組[m:n:k],以間隔逗號(hào)個(gè)數(shù)為k讀取元組中索引從m到n但不包含n的元素tuple = (1,2,3,4,5,6,7) print(tuple[1:6:2]) 結(jié)果:[2,4,6] k=2即間隔兩個(gè)逗號(hào)
讀取元組中的列表中的單個(gè)元素,使用"元組[m][n]的方式,m是元組中列表的索引,n是列表中元素的索引編號(hào).例如:元組[2][-2]指元組中索引為2的列表中倒數(shù)第2個(gè)元素.
讀取元組中列表或元組中的多個(gè)元素,使用"元組[x][m:n]"的方式,x是元組中列表/元組的索引,m:n即列表/元組中從索引為m到n但不包含n的元素.
元組的合并:tuple = tuple1+tuple2
讀取結(jié)果:看最終取自哪里,是單個(gè)還是多個(gè)元素,如果單個(gè)元素,那么讀取結(jié)果是該元素(可以是字符串,數(shù)字,元組,列表,字典),如果是多個(gè)元素,那么若多個(gè)元素最終取自列表,讀取結(jié)果是多個(gè)元素組成的列表,若多個(gè)元素最終取自元組,那么讀取結(jié)果是多個(gè)元素組成的元組
實(shí)例代碼如下:
1 a = 1,3,56,'abc','aoe'
2 print(type(a))3 print(a)4 m = 1,5 n = 1
6 print(type(m))7 print(m)8 print(type(n))9 print(m*3)10 print(m)11 #運(yùn)行結(jié)果
12
13 (1, 3, 56, 'abc', 'aoe')14
15 (1,)16
17 (1, 1, 1)18 (1,)
1 變量名 =(元素1,元素2,...)2 tuple = (1,2,3,[4,5,6,7,8,9],{"A":"abc","B":"789","C":"NBA"},(11,22,'monkey','pig'))3 print(tuple[3])4 print(tuple[3][-1])5 print(tuple[3][1:5])6 print(tuple[4])7 print(tuple[4]["C"])8 print(tuple[5])9 print(tuple[5][1:4])10 #運(yùn)行結(jié)果如下
11 [4, 5, 6, 7, 8, 9]12 9
13 [5, 6, 7, 8]14 {'A': 'abc', 'B': '789', 'C': 'NBA'}15 NBA16 (11, 22, 'monkey', 'pig')17 (22, 'monkey', 'pig')
1 tuple1 = (12, 34.56)2 tuple2 = ('abc', 'xyz')3 tuple3 = tuple1+tuple24 print(tuple3)5 #運(yùn)行結(jié)果
6 (12, 34.56, 'abc', 'xyz')
元組常用的方法/函數(shù):
①排序sorted(tuple):不支持字符串,元組元素不能修改,刪除.使用sorted()排序后,得到的是一個(gè)列表,但是元組本身并沒(méi)有因此變化
②最大值max(tuple)
③最小值min(tuple)
④長(zhǎng)度len(tuple)
⑤重復(fù)tuple*n
1 tuple = (12, 34.56,13,5)2 list =sorted(tuple)3 print(type(tuple))4 print(tuple)5 print(type(list))6 print(list)7 print(len(tuple))8 print(max(tuple))9 print(min(tuple))10 #運(yùn)行結(jié)果
11
12 (12, 34.56, 13, 5)13
14 [5, 12, 13, 34.56]15 4
16 34.56
17 5
1 a =(1,3,56,'abc','aoe')2 print(a)3 print(a*3)4 print(a)5 #運(yùn)行結(jié)果
6 (1, 3, 56, 'abc', 'aoe')7 (1, 3, 56, 'abc', 'aoe', 1, 3, 56, 'abc', 'aoe', 1, 3, 56, 'abc', 'aoe')8 (1, 3, 56, 'abc', 'aoe')
1 #python數(shù)據(jù)類型之元組練習(xí)
2 L1 = (1,2,3,'A','B','C')3 L2 = (1,2,3,4,5,6)4 L3 = ('A',)#元組只有一個(gè)元素時(shí)需要加逗號(hào)","
5 L4 = 'A','B','C','D','E'
6 print(L1[1])#打印元組L1中索引為1的元素
7 print(L1[2:5])#打印(截取)元組L1中索引從2到5,但不包含索引為5的元素
8 print(L1[2:])#打印(截取)元組L1中索引從2開始到結(jié)束的元素
9 print(L1[:5])#打印(截取)元組L1中索引從0開始到5,但不包含索引為5的元素
10 print(L1[-3])#打印元組L1中倒數(shù)第三個(gè)元素
11 print(L1[:-3])#打印元組L1中索引從0開始到倒數(shù)第3(不含倒數(shù)第3)的元素
12 print(L1)#打印元組L1
13 print(L1[0:])#打印元組L1
14 print(L1[:])#打印元組L1
15 print(L1[:6])#打印元組L1
16 print(L1+L2)#元組的組合
17 L5 = L1+L2#元組的拼接
18 print(L5)19 print(L4)20 print(L1*3)#元組的重復(fù)
21 print(len(L2))#打印元組L2的長(zhǎng)度
22 del L2#元組的元素不可以修改,也不可以刪除,可以將整個(gè)元組刪除
23 #運(yùn)行結(jié)果
24 2
25 (3, 'A', 'B')26 (3, 'A', 'B', 'C')27 (1, 2, 3, 'A', 'B')28 A29 (1, 2, 3)30 (1, 2, 3, 'A', 'B', 'C')31 (1, 2, 3, 'A', 'B', 'C')32 (1, 2, 3, 'A', 'B', 'C')33 (1, 2, 3, 'A', 'B', 'C')34 (1, 2, 3, 'A', 'B', 'C', 1, 2, 3, 4, 5, 6)35 (1, 2, 3, 'A', 'B', 'C', 1, 2, 3, 4, 5, 6)36 ('A', 'B', 'C', 'D', 'E')37 (1, 2, 3, 'A', 'B', 'C', 1, 2, 3, 'A', 'B', 'C', 1, 2, 3, 'A', 'B', 'C')38 6
2.列表
列表和元組很相似,主要差別在于列表的元素可以增加,修改和刪除,而元組是不行滴!
列表的元素由[ ?]括起來(lái),元素間用逗號(hào)","隔開.
列表中的元素可以是數(shù)字,字符串,列表,元組,列表,字典
列表的每個(gè)元素也可以是不同的數(shù)據(jù)類型,字符串,數(shù)字,元組,列表,字典
列表的元素以索引編號(hào),從0開始
讀取單個(gè)列使用"列表[m]"的方式,m是索引編號(hào),倒數(shù)第一個(gè)元素"列表[-1]"
讀取多個(gè)元素使用"列表[m:n]"的方式,m和n就是索引的序號(hào),代表讀取列表從m到n的元素,但不包括n這個(gè)元素.例如:列表[1,-1]指索引從1開始到索引為-1但不包含-1的元素.
步長(zhǎng)打印 列表[m:n:k],以間隔逗號(hào)個(gè)數(shù)為k讀取列表中索引從m到n但不包含n的元素list = [1,2,3,4,5,6,7] print(list[1:6:2]) 結(jié)果:[2,4,6] k=2即間隔兩個(gè)逗號(hào)
讀取列表中的元組或列表中的單個(gè)元素,使用"列表[m][n]的方式,m是列表中列表/元組的索引,n是列表/元組中元素的索引編號(hào).例如:列表[2][-2]指列表中索引為2的列表/元組中倒數(shù)第2個(gè)元素.
讀取列表中中列表或元組中的多個(gè)元素.使用"列表[x][m:n]"的方式,x是列表中列表/元組的索引,m:n即列表/元組中從索引為m到n但不包含n的元素.
列表的合并:list1 = list1+list2(列表可以修改) ?,或者list1.extend(list2)
讀取結(jié)果:看最終取自哪里,是單個(gè)還是多個(gè)元素,如果單個(gè)元素,那么讀取結(jié)果是該元素(可以是字符串,數(shù)字,元組,列表,字典),如果是多個(gè)元素,那么若多個(gè)元素最終取自列表,讀取結(jié)果是多個(gè)元素組成的列表,若多個(gè)元素最終取自元組,那么讀取結(jié)果是多個(gè)元素組成的元組
1 變量名 =(元素1,元素2,...)2 list = [1,2,3,[4,5,6,7,8,9],{"A":"abc","B":"789","C":"NBA"},(11,22,'monkey','pig')]3 print(list[3])4 print(list[3][-1])5 print(list[3][1:5])6 print(list[4])7 print(list[4]["C"])8 print(list[5])9 print(list[5][1:4])10 #運(yùn)行結(jié)果
11 [4, 5, 6, 7, 8, 9]12 9
13 [5, 6, 7, 8]14 {'A': 'abc', 'B': '789', 'C': 'NBA'}15 NBA16 (11, 22, 'monkey', 'pig')17 (22, 'monkey', 'pig')
列表常用方法/函數(shù)
1.增刪改的相關(guān)語(yǔ)法:
①增加元素list.append(元素) :調(diào)用列表的添加方法加入元素,并將元素添加到列表最后.其中增加的元素可以是任何數(shù)據(jù)類型
②插入元素list.insert(索引位置,元素):調(diào)用列表的插入方法加入元素到指定的索引位置,之后的元素索引位置依次向后瞬移
③移除元素list.remove(元素):調(diào)用列表的移除方法刪除元素,之后的元素索引位置依次向前瞬移
④修改元素list[n]=元素 將列表中索引為n的元素賦值新元素
⑤刪除元素del list[n] 刪除列表中的元素.刪除整個(gè)列表:del list
⑥列表的合并:
1.list1.extend(list2) ? ?將list2中的元素?cái)U(kuò)展到list1中,并將list2的元素放到list1元素的后面
2.list1 = list1+list2 ? ?直接通過(guò)列表相加的方法并重新賦值到列表1中
實(shí)例代碼如下:
1 a = [1,2,3,'a','abc','dnf']2 a.append(235)3 print(a)4 a.remove(235)5 print(a)6 a.insert(2,4)7 print(a)8 a.insert(6,'xyz')9 print(a)10 a.insert(8,'888')11 print(a)12 a[-1] = '999'
13 print(a)14 print(a[3:7])15 print(a[0:-1])16 #運(yùn)行結(jié)果:
17 [1, 2, 3, 'a', 'abc', 'dnf', 235]18 [1, 2, 3, 'a', 'abc', 'dnf']19 [1, 2, 4, 3, 'a', 'abc', 'dnf']20 [1, 2, 4, 3, 'a', 'abc', 'xyz', 'dnf']21 [1, 2, 4, 3, 'a', 'abc', 'xyz', 'dnf', '888']22 [1, 2, 4, 3, 'a', 'abc', 'xyz', 'dnf', '999']23 [3, 'a', 'abc', 'xyz']24 [1, 2, 4, 3, 'a', 'abc', 'xyz', 'dnf']
1 1 a = [1,2,3,('a','b','c')]2 2 b = [4,5,6,['x','y','z']]3 3 c = [444,555,666,(7,8,9)]4 4a.extend(b)5 5 print(a)6 6 c = b+c7 7 print(c)8 8
9 9 #運(yùn)行結(jié)果
10 10 [1, 2, 3, ('a', 'b', 'c'), 4, 5, 6, ['x', 'y', 'z']]11 11 [4, 5, 6, ['x', 'y', 'z'], 444, 555, 666, (7, 8, 9)]
1#python數(shù)據(jù)類型之列表練習(xí)
2 L1 = [1,2,3,'A','B','C']3 L2 = [1,2,3,4,5,6]4 print(L1[1])#打印列表L1中索引為1的元素
5 print(L1[2:5])#打印(截取)列表L1中索引從2到5,但不包含索引為5的元素
6 print(L1[2:])#打印(截取)列表L1中索引從2開始到結(jié)束的元素
7 print(L1[:5])#打印(截取)列表L1中索引從0開始到5,但不包含索引為5的元素
8 print(L1[-3])#打印列表L1中倒數(shù)第三個(gè)元素
9 print(L1[:-3])#打印列表L1中索引從0開始到倒數(shù)第3(不含倒數(shù)第3)的元素
10 print(L1)#打印列表L1
11 print(L1[0:])#打印列表L1
12 print(L1[:])#打印列表L1
13 print(L1[:6])#打印列表L1
14 print(L1+L2)#列表的組合
15 print(L1*3)#列表的重復(fù)
16 print(len(L2))#打印列表的長(zhǎng)度
17 L2[0] = 'AOE'#列表的值可以修改,將所索引為0的元素更改為'AOE'
18 del L2[5]#列表的元素可以刪除,將索引為5的元素刪除
19 print(L2)20 #運(yùn)行結(jié)果
21 2
22 [3, 'A', 'B']23 [3, 'A', 'B', 'C']24 [1, 2, 3, 'A', 'B']25 A26 [1, 2, 3]27 [1, 2, 3, 'A', 'B', 'C']28 [1, 2, 3, 'A', 'B', 'C']29 [1, 2, 3, 'A', 'B', 'C']30 [1, 2, 3, 'A', 'B', 'C']31 [1, 2, 3, 'A', 'B', 'C', 1, 2, 3, 4, 5, 6]32 [1, 2, 3, 'A', 'B', 'C', 1, 2, 3, 'A', 'B', 'C', 1, 2, 3, 'A', 'B', 'C']33 6
34 ['AOE', 2, 3, 4, 5]
3.字典
字典由一系列"鍵":"值"組成,每一組可以理解為一個(gè)元素,并通過(guò){ ? ?}包含起來(lái),創(chuàng)建字典語(yǔ)法格式:dictionary = {鍵1:值1,鍵2:值2,鍵3:值3,.......}
字典中的鍵是唯一的,如果重復(fù)最后的一個(gè)鍵值對(duì)會(huì)替換前面的,值不需要唯一,值可以取任何數(shù)據(jù)類型。
鍵必須不可變,所以可以用數(shù)字,字符串或元組充當(dāng),所以用列表就不行,因?yàn)榱斜硎强勺兊穆?
字典內(nèi)的元素沒(méi)有順序,所以不能通過(guò)下標(biāo)引用,必須通過(guò)鍵來(lái)訪問(wèn)
訪問(wèn)單個(gè)元素:字典["key"]
訪問(wèn)全部元素:字典
訪問(wèn)字典中沒(méi)有的鍵會(huì)輸出錯(cuò)誤
字典的拼接:dict1.update(dict2),將字典dict2合并到字典dict1之中,如果dict2中存在與dict1中相同的鍵,那么該鍵會(huì)被替換成dict2的值.
1 m = {"a":1,"b":2,"c":3,"d":"nba"}2 print(m["a"])3 print(m["d"])4 #運(yùn)行結(jié)果
5 1
6 nba
字典的增刪改:
字典的增加和修改的語(yǔ)法一樣,都是通過(guò)給某個(gè)鍵進(jìn)行對(duì)應(yīng)的賦值,當(dāng)鍵對(duì)應(yīng)的值存在時(shí)將原值替換為新的賦值,當(dāng)鍵不存在時(shí)創(chuàng)建一個(gè)新的鍵值對(duì).
1 n = {"a":1,"b":2}2 n["c"] = 3
3 n["a"] = 'x'
4 print(n)5 #運(yùn)行結(jié)果
6 {'a': 'x', 'b': 2, 'c': 3}
字典的拼接:dict1.update(dict2),將字典dict2合并到字典dict1之中,如果dict2中存在與dict1中相同的鍵,那么該鍵會(huì)被替換成dict2的值.
與列表的合并區(qū)別在于,列表可以合并重復(fù)的數(shù)據(jù)并且不會(huì)被替代,而字典中如果有重復(fù)的鍵,就會(huì)被新的鍵對(duì)應(yīng)的值所取代
1 #dictionary = {鍵1:值1,鍵2:值2,鍵3:值3,.......}
2 m = {"a":1,"b":2,"c":3,"d":"nba"}3 m["x"] = 2019
4 m['y'] = 'abc'
5 print(m["x"])6 print(m["y"])7 m["a"] = 123
8 m["b"] = "HK666"
9 print(m)10 n = {"a":1,"b":2}11 y = {"c":3,"d":"nba"}12 n.update(y)13 print(n)14 z = {"a":1,"b":2}15 del(z["a"])16 print(z)17 x = {"a":1,"b":2}18 y = {"a":1,"b":2}19 x.update(y)20 print(x)21 #運(yùn)行結(jié)果:
22 2019
23 abc24 {'a': 123, 'b': 'HK666', 'c': 3, 'd': 'nba', 'x': 2019, 'y': 'abc'}25 {'a': 1, 'b': 2, 'c': 3, 'd': 'nba'}26 {'b': 2}27 {'a': 1, 'b': 2}
字典常用的方法/函數(shù)
①取出所有的鍵dict.keys()
②取出所有的值dict.values()
③取出所有的鍵值對(duì)dict.items()
④清空字典dict.clear()
⑤刪除:只能刪除單個(gè)鍵值或者整個(gè)字典
刪除單個(gè)鍵值 del dict['key1']
刪除整個(gè)字典del dict
1 dict_aoe = {'key1':'value1','key2':'value2','key3':'value3','key4':'value4'}2 print(dict_aoe)3 print(dict_aoe.keys())4 print(dict_aoe.values())5 print(dict_aoe.clear())6 print(dict_aoe)7 dict1 = {'key1':'value1','key2':'value2','key3':'value3','key4':'value4'}8 del dict1['key1']9 print(dict1)10 deldict111 print(dict1)12 #運(yùn)行結(jié)果
13 {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'}14 dict_keys(['key1', 'key2', 'key3', 'key4'])15 dict_values(['value1', 'value2', 'value3', 'value4'])16 None17 {}18 {'key2': 'value2', 'key3': 'value3', 'key4': 'value4'}19 NameError: name 'dict1' is not defined
====================================================
注意:由于列表和字典的可變性,對(duì)其執(zhí)行相關(guān)操作(增加,刪除,修改))后,列表已經(jīng)發(fā)生變化
====================================================
總結(jié)
以上是生活随笔為你收集整理的python包含多个元组的元组_Python数据结构(元组,列表,字典)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: php 缩略图 失真,WORDPRESS
- 下一篇: python贪心算法求删数问题_贪心算法