Python

Python 复读笔记(3)

2016-03-21  本文已影响81人  57fc17b7d598
  1. List 列表和切片

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>

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>

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>

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>

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>

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>
* 在指定索引位置插入数据
<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>
* 排序和统计
<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个数据,最后for循环的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>

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 子句,之后可以有零或多个 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>

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函数
语法:list = list(map(函数, 可迭代对象))
可迭代对象每一次产生数据,都将作为参数传递到函数中,函数中的逻辑处理完了之后,map自动追加数据到list中,当整个迭代完成后,每一次迭代出来的数据都已被函数进行过了处理,最后通过list函数将其结果转换为一个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循环例子,第三个列表生成式拆解开来等同于:
<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 = [
... [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循环开始解释**,如上列表生成式中的表达式本身就是一个列表生成式`[row[i] for row in nums4]`,而右边的就是for循环`for i in range(4)`,循环四次依次获得[0,1,2,3]作为左边表达式中的参数,最终左边表达式得到参数后作为索引依次获取了nums4中每一个子列表在该索引位置上的元素。

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循环例子,第三个列表生成式拆解开来等同于:
<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循环开始解释,如上列表生成式中的表达式本身就是一个列表生成式[row[i] for row in nums4],而右边的就是for循环for i in range(4),循环四次依次获得[0,1,2,3]作为左边表达式中的参数,最终左边表达式得到参数后作为索引依次获取了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>

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循环开始解释,如上列表生成式中的表达式本身就是一个列表生成式[row[i] for row in nums4],而右边的就是for循环for i in range(4),循环四次依次获得[0,1,2,3]作为左边表达式中的参数,最终左边表达式得到参数后作为索引依次获取了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>

a = {x for x in 'abracadabra' if x not in 'abc'}
a
{'r', 'd'}
</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([('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>

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

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

上一篇下一篇

猜你喜欢

热点阅读