最近使用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
shallow copy/Deep Copy
原文链接:https://blog.csdn.net/huaxixidongbeishida/article/details/105614494