python列表复制函数 python 复制函数( 六 )


'three':3,
'test':2
}
printd.values()
运算结果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
[2,3,2,123]
python列表怎么复制提问者这么短的描述,有点无法看懂,那就按照通常的几种方法来说吧:
简单列表的拷贝
已知一个列表 , 求生成一个新的列表,列表元素是原列表的复制
a=[1,2]
b=a
这种其实并未真正生成一个新的列表,b指向的仍然是a所指向的对象 。
后果:如果对a或b的元素进行修改,a,b的值同时发生变化 。
可以使用以下方法解决:
a=[1,2]
b=a[:]
这样修改a对b没有影响 。修改b对a没有影响 。
复杂列表的拷贝:
可以使用copy模块中的deepcopy函数 。修改测试如下:
import copy
a=[1,[2]]
b=copy.deepcopy(a)
如何使用python复制大概200G的数据[任何语言只要能实现]如果我理解的正确的话,楼主是要copy大文件吧 。
python最经常使用的copy函数,应该是shutil.copyfile()了,它默认以16384bytes 的大小作为缓冲区,对于小的文件,的确不错 。但是处理到大的文件的时候,性能下降就很严重 。过小的buffer会不合适 。
经过多次的尝试,发现10Mb的buffer最合适,再高也没有性能的提升 。重载后的copy函数如下
def copyFile(src, dst, buffer_size=10485760, perserveFileDate=True):
'''
Copies a file to a new location. Much faster performance than Apache Commons due to use of larger buffer
@param src:Source File
@param dst:Destination File (not file path)
@param buffer_size:Buffer size to use during copy
@param perserveFileDate:Preserve the original file date
'''
#Check to make sure destination directory exists. If it doesn't create the directory
dstParent, dstFileName = os.path.split(dst)
if(not(os.path.exists(dstParent))):
os.makedirs(dstParent)
#Optimize the buffer for small files
buffer_size = min(buffer_size,os.path.getsize(src))
if(buffer_size == 0):
buffer_size = 1024
if shutil._samefile(src, dst):
raise shutil.Error("`%s` and `%s` are the same file" % (src, dst))
for fn in [src, dst]:
try:
st = os.stat(fn)
except OSError:
# File most likely does not exist
pass
else:
# XXX What about other special files? (sockets, devices...)
if shutil.stat.S_ISFIFO(st.st_mode):
raise shutil.SpecialFileError("`%s` is a named pipe" % fn)
with open(src, 'rb') as fsrc:
with open(dst, 'wb') as fdst:
shutil.copyfileobj(fsrc, fdst, buffer_size)
if(perserveFileDate):
shutil.copystat(src, dst)
使用的时候记得添加相应的包 。
【python列表复制函数 python 复制函数】关于python列表复制函数和python 复制函数的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息 , 记得收藏关注本站 。

推荐阅读