最近使用python 看到這麼幾個知識點,收集下
zip
zip() 函數用於將可迭代的對象作爲參數,將對象中對應的元素打包成一個個元組,然後返回由這些元組組成的列表。
如果各個迭代器的元素個數不一致,則返回列表長度與最短的對象相同,利用 * 號操作符,可以將元組解壓爲列表。
https://docs.python.org/3.3/library/functions.html#zip
def zip(*iterables):
# zip('ABCD', 'xy') --> Ax By
sentinel = object()
iterators = [iter(it) for it in iterables]
while iterators:
result = []
for it in iterators:
elem = next(it, sentinel)
if elem is sentinel:
return
result.append(elem)
yield tuple(result)
例子參考:https://www.runoob.com/python/python-func-zip.html
list賦值
在具體賦值的時候,列表list賦值時整列重複賦值問題,具體如下:
l = [[0]*3]*3
最開始
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
執行 l[0][1] = 1
[0, 1, 0]
[0, 1, 0]
[0, 1, 0]
產生上述原因是最開始創建的時候利用的是第一行的淺拷貝,地址就是一樣的,給任何一個其餘的均會被賦值,解決辦法換成如下方法建立
l = [[0]*3 for i in range(3)]
https://blog.csdn.net/sjtuxx_lee/article/details/86709535