Python 列表、元組、字典和集合

一、列表

In [1]: test = list('abcdefg')

In [2]: test
Out[2]: ['a', 'b', 'c', 'd', 'e', 'f', 'g']

In [3]: test.append('h')       # 添加元素到列表末

In [4]: test
Out[4]: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

In [5]: test.insert(2,'Kobe')  # 添加元素到指定下標(biāo)位置

In [6]: test
Out[6]: ['a', 'b', 'Kobe', 'c', 'd', 'e', 'f', 'g', 'h']

In [7]: test.pop()             # 刪除列表最后一個元素并彈出此元素
Out[7]: 'h'

In [8]: test
Out[8]: ['a', 'b', 'Kobe', 'c', 'd', 'e', 'f', 'g']

In [9]: test.pop(2)            # 刪除指定下標(biāo)元素并彈出此元素
Out[9]: 'Kobe'

In [10]: test
Out[10]: ['a', 'b', 'c', 'd', 'e', 'f', 'g']

In [11]: test.remove('g')      # 刪除指定元素

In [12]: test
Out[12]: ['a', 'b', 'c', 'd', 'e', 'f']

In [13]: del test[-1]          # 刪除指定下標(biāo)元素

In [14]: test
Out[14]: ['a', 'b', 'c', 'd', 'e']

In [15]: test.reverse()        # 列表倒序排列并賦值到此列表

In [16]: test
Out[16]: ['e', 'd', 'c', 'b', 'a']

In [17]: wate = ['Kobe', 'Nash']  

In [18]: test.extend(wate)     # 把列表 wate 添加到列表 test 的末尾

In [19]: test
Out[19]: ['e', 'd', 'c', 'b', 'a', 'Kobe', 'Nash']

In [20]: sorted(test)          # 對列表排序并打?。ㄔ斜聿蛔儯?Out[20]: ['Kobe', 'Nash', 'a', 'b', 'c', 'd', 'e']

In [21]: test
Out[21]: ['e', 'd', 'c', 'b', 'a', 'Kobe', 'Nash']

In [22]: test.sort()           # 對列表排序并賦值到此列表

In [23]: test
Out[23]: ['Kobe', 'Nash', 'a', 'b', 'c', 'd', 'e']

二、元組

# 元組不可變,不能增刪改,只能查
# 只有一個元素的元組,元素后要加逗號
In [23]: test
Out[23]: ['Kobe', 'Nash', 'a', 'b', 'c', 'd', 'e']

In [24]: t = tuple(test)

In [25]: t
Out[25]: ('Kobe', 'Nash', 'a', 'b', 'c', 'd', 'e')

In [26]: t[2:]
Out[26]: ('a', 'b', 'c', 'd', 'e')

三、字典

# 相比列表,字典占用內(nèi)存較多,但查詢速度較快
In [28]: d = {1:'Kobe', 2:'Nash', 3:'James', 4:'Wall'}

In [29]: d[5] = 'Irving'   # 添加元素

In [30]: d
Out[30]: {1: 'Kobe', 2: 'Nash', 3: 'James', 4: 'Wall', 5: 'Irving'}

In [31]: d.pop(5)          # 刪除元素,無此元素會報錯
Out[31]: 'Irving'

In [32]: d
Out[32]: {1: 'Kobe', 2: 'Nash', 3: 'James', 4: 'Wall'}

In [33]: del d[4]          # 刪除元素,無此元素會報錯

In [34]: d
Out[34]: {1: 'Kobe', 2: 'Nash', 3: 'James'}

In [35]: for key, value in d.items():
    ...:     print('{}: {}'.format(key, value))
    ...:     
1: Kobe
2: Nash
3: James

In [36]: for i in d.keys():
    ...:     print(i)
    ...:     
1
2
3

In [37]: for i in d.values():
    ...:     print(i)
    ...:     
Kobe
Nash
James

In [38]: d.get(3)             # 查詢 value ,查不到不會報錯
Out[38]: 'James'

In [39]: d.get(4, 'Nothing')  # 設(shè)置缺省值
Out[39]: 'Nothing'

四、集合

# 集合不能添加重復(fù)值
# True 與 1 被默認(rèn)為同一個值,F(xiàn)alse 與 0 被認(rèn)為是同一個值
In [64]: test
Out[64]: ['Kobe', 'Nash', 'c', 'd', 'e', 'e']

In [65]: s = set(test)   # 創(chuàng)建集合

In [66]: s
Out[66]: {'Kobe', 'Nash', 'c', 'd', 'e'}

In [67]: s.add('f')      # 添加元素

In [68]: s
Out[68]: {'Kobe', 'Nash', 'c', 'd', 'e', 'f'}

In [69]: s.remove('f')   # 刪除元素

In [70]: s
Out[70]: {'Kobe', 'Nash', 'c', 'd', 'e'}

In [71]: 'f' in s        # 測試集合中是否有該元素
Out[71]: False 

In [72]: ss = set(list('defg'))

In [73]: ss
Out[73]: {'d', 'e', 'f', 'g'}

In [74]: s | ss          # 打印兩個集合的并集
Out[74]: {'Kobe', 'Nash', 'c', 'd', 'e', 'f', 'g'}

In [75]: s & ss          # 打印兩個集合的交集
Out[75]: {'d', 'e'}

In [76]: s ^ ss          # 打印在兩個集合獨(dú)有的元素
Out[76]: {'Kobe', 'Nash', 'c', 'f', 'g'}

In [77]: s - ss          # 打印在 s 中且不在 ss 中的元素
Out[77]: {'Kobe', 'Nash', 'c'}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容