Python 復讀筆記(3)

  1. List 列表和切片
  • list定義
    <pre>
    nums = [1, 2, 3]
    </pre>
  • 切片語法
    list[起始索引:終止索引:步長]
    利用切片可以取值和賦值,兩者的結果和對象都是list
  • 追加數(shù)據,使用切片的方式可以(也只能)追加一個list
    <pre>

nums = [1,2,3]
nums
[1, 2, 3]
nums.append(4)
nums
[1, 2, 3, 4]
nums[4:] = [5]
nums
[1, 2, 3, 4, 5]
nums[len(nums):] = [6]
nums
[1, 2, 3, 4, 5, 6]
nums[len(nums):] = [7,8]
nums
[1, 2, 3, 4, 5, 6, 7, 8]
</pre>

  • 在指定索引位置插入數(shù)據
    <pre>

nums
[1, 2, 3, 4, 5, 6, 7, 8]
nums.insert(0, 0)
nums
[0, 1, 2, 3, 4, 5, 6, 7, 8]
nums.insert(3, 2.5)
nums
[0, 1, 2, 2.5, 3, 4, 5, 6, 7, 8]
nums.insert(len(nums), 9)
nums
[0, 1, 2, 2.5, 3, 4, 5, 6, 7, 8, 9]
</pre>

  • 刪除元素
    <pre>

nums
[0, 1, 2, 2.5, 3, 4, 5, 6, 7, 8, 9]
nums.append(9)
nums
[0, 1, 2, 2.5, 3, 4, 5, 6, 7, 8, 9, 9]
nums.remove(9)
nums
[0, 1, 2, 2.5, 3, 4, 5, 6, 7, 8, 9]
nums.remove(9)
nums
[0, 1, 2, 2.5, 3, 4, 5, 6, 7, 8]
nums.pop()
8
nums
[0, 1, 2, 2.5, 3, 4, 5, 6, 7]
nums.pop(3)
2.5
nums
[0, 1, 2, 3, 4, 5, 6, 7]
</pre>

  • 取值和修改
    <pre>

nums
[0, 1, 2, 3, 4, 5, 6, 7]
nums[1]
1
nums.index(1)
1
nums[0] = -1
nums
[-1, 1, 2, 3, 4, 5, 6, 7]
</pre>

  • 排序和統(tǒng)計
    <pre>

nums
[-1, 1, 2, 3, 4, 5, 6, 7]
nums.append(4)
nums
[-1, 1, 2, 3, 4, 5, 6, 7, 4]
nums.count(4)
2
nums.sort()
nums
[-1, 1, 2, 3, 4, 4, 5, 6, 7]
nums.reverse()
nums
[7, 6, 5, 4, 4, 3, 2, 1, -1]
nums[:]
[-1, 1, 2, 3, 4, 4, 5, 6, 7]
</pre>

  • 列表生成式和map
  • 例子
    例子中定義了一個list,并往里面追加了10個數(shù)據,最后for循環(huán)的x變量還存在。
    <pre>

nums = []
for x in range(10):
nums[4:] = [5]
>>> nums
[1, 2, 3, 4, 5]
>>> nums[len(nums):] = [6]
>>> nums
[1, 2, 3, 4, 5, 6]
>>> nums[len(nums):] = [7,8]
>>> nums
[1, 2, 3, 4, 5, 6, 7, 8]
</pre>
* 在指定索引位置插入數(shù)據
<pre>
>>> nums
[1, 2, 3, 4, 5, 6, 7, 8]
>>> nums.insert(0, 0)
>>> nums
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> nums.insert(3, 2.5)
>>> nums
[0, 1, 2, 2.5, 3, 4, 5, 6, 7, 8]
>>> nums.insert(len(nums), 9)
>>> nums
[0, 1, 2, 2.5, 3, 4, 5, 6, 7, 8, 9]
</pre>
* 刪除元素
<pre>
>>> nums
[0, 1, 2, 2.5, 3, 4, 5, 6, 7, 8, 9]
>>> nums.append(9)
>>> nums
[0, 1, 2, 2.5, 3, 4, 5, 6, 7, 8, 9, 9]
>>> nums.remove(9)
>>> nums
[0, 1, 2, 2.5, 3, 4, 5, 6, 7, 8, 9]
>>> nums.remove(9)
>>> nums
[0, 1, 2, 2.5, 3, 4, 5, 6, 7, 8]
>>> nums.pop()
8
>>> nums
[0, 1, 2, 2.5, 3, 4, 5, 6, 7]
>>> nums.pop(3)
2.5
>>> nums
[0, 1, 2, 3, 4, 5, 6, 7]
</pre>
* 取值和修改
<pre>
>>> nums
[0, 1, 2, 3, 4, 5, 6, 7]
>>> nums[1]
1
>>> nums.index(1)
1
>>> nums[0] = -1
>>> nums
[-1, 1, 2, 3, 4, 5, 6, 7]
</pre>
* 排序和統(tǒng)計
<pre>
>>> nums
[-1, 1, 2, 3, 4, 5, 6, 7]
>>> nums.append(4)
>>> nums
[-1, 1, 2, 3, 4, 5, 6, 7, 4]
>>> nums.count(4)
2
>>> nums.sort()
>>> nums
[-1, 1, 2, 3, 4, 4, 5, 6, 7]
>>> nums.reverse()
>>> nums
[7, 6, 5, 4, 4, 3, 2, 1, -1]
>>> nums[:]
[-1, 1, 2, 3, 4, 4, 5, 6, 7]
</pre>

* 列表生成式和map
* 例子
例子中定義了一個list,并往里面追加了10個數(shù)據,最后for循環(huán)的x變量還存在。
<pre>
>>> nums = []
>>> for x in range(10):
... nums.append(x ** 2)
...
nums
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
x
9
</pre>

  • map函數(shù)
    語法:list = list(map(函數(shù), 可迭代對象))
    可迭代對象每一次產生數(shù)據,都將作為參數(shù)傳遞到函數(shù)中,函數(shù)中的邏輯處理完了之后,map自動追加數(shù)據到list中,當整個迭代完成后,每一次迭代出來的數(shù)據都已被函數(shù)進行過了處理,最后通過list函數(shù)將其結果轉換為一個list,但這一步并非必要。
    <pre>

nums1 = list(map(lambda x1 : x1 ** 2, range(10)))
nums1
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
x1 # x1不存在
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x1' is not defined
</pre>

  • 列表生成式
  • 語法:[表達式 for if]
  • 注釋:列表生成式由中括號包圍,里面可以有且最少有一個表達式和一個for,再之后可以跟隨零或多個 for 或 if 子句,疑問的是[1 * 2]這算一個列表生成式嗎?

列表推導式由包含一個表達式的括號組成,表達式后面跟隨一個 for 子句,之后可以有零或多個 for 或 if 子句。結果是一個列表,由表達式依據其后面的 for 和 if 子句上下文計算而來的結果構成。

<pre>

第一個列表生成式

[x3 ** 2 for x3 in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
x3 # x3不存在
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x3' is not defined

第二個列表生成式

[(x, y) for x in [1, 2, 3] for y in [3, 1, 4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

第三個列表生成式

[(x, y) for x in [1, 2, 3] if x > 1 for y in [3, 1, 4] if x != y]
[(2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
</pre>

  • 備注
    第一個列表生成式等同于上面的for循環(huán)例子,第三個列表生成式拆解開來等同于:
    <pre>

nums3 = []
nums3
[]
for x in [1, 2, 3]:
for x in range(10):
... nums.append(x ** 2)
...
>>> nums
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> x
9
</pre>
* map函數(shù)
語法:list = list(map(函數(shù), 可迭代對象))
可迭代對象每一次產生數(shù)據,都將作為參數(shù)傳遞到函數(shù)中,函數(shù)中的邏輯處理完了之后,map自動追加數(shù)據到list中,當整個迭代完成后,每一次迭代出來的數(shù)據都已被函數(shù)進行過了處理,最后通過list函數(shù)將其結果轉換為一個list,但這一步并非必要。
<pre>
>>> nums1 = list(map(lambda x1 : x1 ** 2, range(10)))
>>> nums1
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> x1 # x1不存在
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x1' is not defined
</pre>
* 列表生成式
* 語法:[表達式 for if]
* 注釋:列表生成式由中括號包圍,里面可以有且最少有一個表達式和一個for,再之后可以跟隨零或多個 for 或 if 子句,疑問的是[1 * 2]這算一個列表生成式嗎?
> 列表推導式由包含一個表達式的括號組成,表達式后面跟隨一個 for 子句,之后可以有零或多個 for 或 if 子句。結果是一個列表,由表達式依據其后面的 for 和 if 子句上下文計算而來的結果構成。

<pre>
>>> # 第一個列表生成式
>>> [x3 ** 2 for x3 in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> x3 # x3不存在
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x3' is not defined
>>>
>>> # 第二個列表生成式
>>> [(x, y) for x in [1, 2, 3] for y in [3, 1, 4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
>>>
>>> # 第三個列表生成式
>>> [(x, y) for x in [1, 2, 3] if x > 1 for y in [3, 1, 4] if x != y]
[(2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
</pre>
* 備注
第一個列表生成式等同于上面的for循環(huán)例子,第三個列表生成式拆解開來等同于:
<pre>
>>> nums3 = []
>>> nums3
[]
>>> for x in [1, 2, 3]:
... if x > 1:
... for y in [3, 1, 4]:
... if x != y:
... nums3.append((x, y))
...
nums3
[(2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
</pre>

  • 嵌套的列表生成式
    先看這個例子,nums4是一個二維列表,如何將每個子列表中相同索引的元素合并到一個列表里,普通的方式如下:
    <pre>

nums4 = [
... [1, 2, 3, 4],
... [5, 6, 7, 8],
... [9, 10, 11, 12]
... ]
nums4
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
nums5 = []
for i in range(4):
... rows = []
... for row in nums4:
... rows.append(row[i])
... print(nums5, rows)
... nums5.append(rows)
...
[] [1, 5, 9]
[[1, 5, 9]] [2, 6, 10]
[[1, 5, 9], [2, 6, 10]] [3, 7, 11]
[[1, 5, 9], [2, 6, 10], [3, 7, 11]] [4, 8, 12]
</pre>

通過嵌套式的列表生成式,方式如下:

<pre>

[[row[i] for row in nums4] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
</pre>

**列表生成式是從表達式右邊的for循環(huán)開始解釋**,如上列表生成式中的表達式本身就是一個列表生成式`[row[i] for row in nums4]`,而右邊的就是for循環(huán)`for i in range(4)`,循環(huán)四次依次獲得[0,1,2,3]作為左邊表達式中的參數(shù),最終左邊表達式得到參數(shù)后作為索引依次獲取了nums4中每一個子列表在該索引位置上的元素。
  • del 語句
    del可以刪除變量或其值,對于list支持切片操作
    <pre>

nums

>>> # 第三個列表生成式
>>> [(x, y) for x in [1, 2, 3] if x > 1 for y in [3, 1, 4] if x != y]
[(2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
</pre>
* 備注
第一個列表生成式等同于上面的for循環(huán)例子,第三個列表生成式拆解開來等同于:
<pre>
>>> nums3 = []
>>> nums3
[]
>>> for x in [1, 2, 3]:
... if x > 1:
... for y in [3, 1, 4]:
... if x != y:
... nums3.append((x, y))
...
>>> nums3
[(2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
</pre>
* 嵌套的列表生成式
先看這個例子,nums4是一個二維列表,如何將每個子列表中相同索引的元素合并到一個列表里,普通的方式如下:
<pre>
>>> nums4 = [
... [1, 2, 3, 4],
... [5, 6, 7, 8],
... [9, 10, 11, 12]
... ]
>>> nums4
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
>>> nums5 = []
>>> for i in range(4):
... rows = []
... for row in nums4:
... rows.append(row[i])
... print(nums5, rows)
... nums5.append(rows)
...
[] [1, 5, 9]
[[1, 5, 9]] [2, 6, 10]
[[1, 5, 9], [2, 6, 10]] [3, 7, 11]
[[1, 5, 9], [2, 6, 10], [3, 7, 11]] [4, 8, 12]
</pre>

通過嵌套式的列表生成式,方式如下:
<pre>
>>> [[row[i] for row in nums4] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
</pre>

列表生成式是從表達式右邊的for循環(huán)開始解釋,如上列表生成式中的表達式本身就是一個列表生成式[row[i] for row in nums4],而右邊的就是for循環(huán)for i in range(4),循環(huán)四次依次獲得[0,1,2,3]作為左邊表達式中的參數(shù),最終左邊表達式得到參數(shù)后作為索引依次獲取了nums4中每一個子列表在該索引位置上的元素。
* del 語句
del可以刪除變量或其值,對于list支持切片操作
<pre>
>>> nums
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
del nums[0]
nums
[1, 4, 9, 16, 25, 36, 49, 64, 81]
del nums[1:3]
nums
[1, 16, 25, 36, 49, 64, 81]
del nums[:]
nums
[]
del nums
nums
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'nums' is not defined
</pre>

  • Tuple 元祖
  • 定義:變量 = (1, 2, 3)
  • 注釋:其中括號在有兩個或多個值的情況下可以省略不寫,前提是這個元祖的定義不能是某個表達式的一部分,在只有一個元素的時候,定義的時候需要注意在第一個元素后面加上逗號。
    <pre>

t = 123, 456, 789, "hello"
t
(123, 456, 789, 'hello')
t[2:3]

>>> nums4 = [
... [1, 2, 3, 4],
... [5, 6, 7, 8],
... [9, 10, 11, 12]
... ]
>>> nums4
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
>>> nums5 = []
>>> for i in range(4):
... rows = []
... for row in nums4:
... rows.append(row[i])
... print(nums5, rows)
... nums5.append(rows)
...
[] [1, 5, 9]
[[1, 5, 9]] [2, 6, 10]
[[1, 5, 9], [2, 6, 10]] [3, 7, 11]
[[1, 5, 9], [2, 6, 10], [3, 7, 11]] [4, 8, 12]
</pre>

通過嵌套式的列表生成式,方式如下:
<pre>
>>> [[row[i] for row in nums4] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
</pre>

列表生成式是從表達式右邊的for循環(huán)開始解釋,如上列表生成式中的表達式本身就是一個列表生成式[row[i] for row in nums4],而右邊的就是for循環(huán)for i in range(4),循環(huán)四次依次獲得[0,1,2,3]作為左邊表達式中的參數(shù),最終左邊表達式得到參數(shù)后作為索引依次獲取了nums4中每一個子列表在該索引位置上的元素。
* del 語句
del可以刪除變量或其值,對于list支持切片操作
<pre>
>>> nums
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> del nums[0]
>>> nums
[1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> del nums[1:3]
>>> nums
[1, 16, 25, 36, 49, 64, 81]
>>> del nums[:]
>>> nums
[]
>>> del nums
>>> nums
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'nums' is not defined
</pre>
* Tuple 元祖
* 定義:變量 = (1, 2, 3)
* 注釋:其中括號在有兩個或多個值的情況下可以省略不寫,前提是這個元祖的定義不能是某個表達式的一部分,在只有一個元素的時候,定義的時候需要注意在第一個元素后面加上逗號。
<pre>
>>> t = 123, 456, 789, "hello"
>>> t
(123, 456, 789, 'hello')
>>> t[2:3]
(789,)
t[2:4]
(789, 'hello')
u = t, ("t", "u", "p", "l", "e")
u
((123, 456, 789, 'hello'), ('t', 'u', 'p', 'l', 'e'))
p = ([1, 2, 3], ["a", "b", "c"])
p
([1, 2, 3], ['a', 'b', 'c'])
l = ()
l
()
e = ("nihao", )
e
('nihao',)
tu, ple = u
tu
(123, 456, 789, 'hello')
ple
('t', 'u', 'p', 'l', 'e')
</pre>

  • Set 集合
  • 語法:color = {"red", "green", "blue"}
  • 定義只有一個set集合的時候,需要用set()函數(shù),如king = set("me")
  • 通過in關鍵字可以判斷元素是否存在于集合里,如 "red" in color
  • Set集合可以通過集合生成式(類似列表生成式)來生成,由大括號包圍
    <pre>

a = {x for x in 'abracadabra' if x not in 'abc'}
a
{'r', 'd'}
</pre>

  • 下面是官方例子
    <pre>

basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(basket) # show that duplicates have been removed
{'orange', 'banana', 'pear', 'apple'}
'orange' in basket # fast membership testing
True
'crabgrass' in basket
False

Demonstrate set operations on unique letters from two words

...

a = set('abracadabra')
b = set('alacazam')
a # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
a - b # letters in a but not in b
{'r', 'd', 'b'}
a | b # letters in either a or b
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
a & b # letters in both a and b
{'a', 'c'}
a ^ b # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}
</pre>

  • Dict 字典
  • 定義:kw = {"a": 1, "b": 2, "c": 3}
  • 取值:kw["a"],如不存在則報錯
  • 增加或修改可以通過kw["d"] = 4
  • in和not in可以檢查字典中是否存在指定key
  • dict()構造函數(shù)也可以生成字典
    <pre>

dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'sape': 4139, 'jack': 4098, 'guido': 4127}

dict(sape=4139, guido=4127, jack=4098)
{'sape': 4139, 'jack': 4098, 'guido': 4127}
</pre>

  • 可以通過字典生成式生成字典,字典生成式中表達式里包含冒號(:),用來區(qū)分key和value
    <pre>

{x: x**2 for x in (2, 4, 6)}

{2: 4, 4: 16, 6: 36}
</pre>

  • 生成式
  • 列表生成式:[表達式 for if],由中括號包圍,表達式可以是一個列表生成式,for 和 if 可以有多個
  • 集合生成式:{表達式 for if},由大括號包圍
  • 字典生成式:{key: value for if} ,由大括號包圍
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 背景 一年多以前我在知乎上答了有關LeetCode的問題, 分享了一些自己做題目的經驗。 張土汪:刷leetcod...
    土汪閱讀 12,899評論 0 33
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,533評論 19 139
  • Python 是一種相當高級的語言,通過 Python 解釋器把符合語法的程序代碼轉換成 CPU 能夠執(zhí)行的機器碼...
    Python程序媛閱讀 2,037評論 0 3
  • 如何配置aria2(RPC) + webui + TOKEN 下載必要文件 aria2 webuiRPC http...
    艾雨寒閱讀 14,544評論 0 1

友情鏈接更多精彩內容