I have the following problem.

Given a list of integers L, I need to generate all of the sublists L[k:] for k in [0, len(L) - 1], without generating copies.

How do I accomplish this in Python? With a buffer object somehow?

解决方案

The short answer

Slicing lists does not generate copies of the objects in the list; it just copies the references to them. That is the answer to the question as asked.

The long answer

Testing on mutable and immutable values

First, let's test the basic claim. We can show that even in the case of immutable objects like integers, only the reference is copied. Here are three different integer objects, each with the same value:

>>> a = [1000 + 1, 1000 + 1, 1000 + 1]

They have the same value, but you can see they are three distinct objects because they have different ids:

>>> map(id, a)

[140502922988976, 140502922988952, 140502922988928]

When you slice them, the references remain the same. No new objects have been created:

>>> b = a[1:3]

>>> map(id, b)

[140502922988952, 140502922988928]

Using different objects with the same value shows that the copy process doesn't bother with interning -- it just directly copies the references.

Testing with mutable values gives the same result:

>>> a = [{0: 'zero', 1: 'one'}, ['foo', 'bar']]

>>> map(id, a)

[4380777000, 4380712040]

>>> map(id, a[1:]

... )

[4380712040]

Examining remaining memory overhead

Of course the references themselves are copied. Each one costs 8 bytes on a 64-bit machine. And each list has its own memory overhead of 72 bytes:

>>> for i in range(len(a)):

... x = a[:i]

... print('len: {}'.format(len(x)))

... print('size: {}'.format(sys.getsizeof(x)))

...

len: 0

size: 72

len: 1

size: 80

len: 2

size: 88

As Joe Pinsonault reminds us, that overhead adds up. And integer objects themselves are not very large -- they are three times larger than references. So this saves you some memory in an absolute sense, but asymptotically, it might be nice to be able to have multiple lists that are "views" into the same memory.

Saving memory by using views

Unfortunately, Python provides no easy way to produce objects that are "views" into lists. Or perhaps I should say "fortunately"! It means you don't have to worry about where a slice comes from; changes to the original won't affect the slice. Overall, that makes reasoning about a program's behavior much easier.

If you really want to save memory by working with views, consider using numpy arrays. When you slice a numpy array, the memory is shared between the slice and the original:

>>> a = numpy.arange(3)

>>> a

array([0, 1, 2])

>>> b = a[1:3]

>>> b

array([1, 2])

What happens when we modify a and look again at b?

>>> a[2] = 1001

>>> b

array([ 1, 1001])

But this means you have to be sure that when you modify one object, you aren't inadvertently modifying another. That's the trade-off when you use numpy: less work for the computer, and more work for the programmer!

如何在python中创建列表副本,在Python中切片列表而不生成副本相关推荐

  1. 在python中创建一个具有特定大小的空列表

    本文翻译自:Create an empty list in python with certain size I want to create an empty list (or whatever i ...

  2. 【Hive】如何在 Hive 中创建外部表映射 Hbase 中已存在的表

    文章目录 一.上传完整的jar文件到hive/lib中 二.修改hive-site.xml 三.修改hive-env.sh 四.在hive和hbase中分别创建相关联的表并通过hive向hbase表中 ...

  3. Exchange2007中创建收件人对象、通讯组和地址列表和客户端访问

    什么是收件人对象? 收件人在Exchange中被定义为具有发送或接收邮件能力的Active Directory对象 Exchange有3种收件人对象:用户.联系人.组 1.创建用户邮箱帐户 使用exc ...

  4. Python 线程创建和传参 - Python零基础入门教程

    目录 一.Python 线程解释 二.Python 线程创建和启动 1.导入线程模块 2.创建线程并初始化线程 3.启动线程 三.Python 线程传参 四.Python 线程结束 五.Python ...

  5. Kubernetes 中创建 Pod 时集群中到底发生了些什么?

    想象一下,如果我想将 nginx 部署到 Kubernetes 集群,我可能会在终端中输入类似这样的命令: $ kubectl run --image=nginx --replicas=3 然后回车. ...

  6. python 批量创建线程_【Python】批量创建线程

    在<[Python]线程的创建.执行.互斥.同步.销毁>(点击打开链接)中介绍了Python中线程的使用,但是里面线程的创建,使用了很原始的方式,一行代码创建一条.其实,Python里是可 ...

  7. 人工智能之配置环境教程二:在Anaconda中创建虚拟环境并在VsCode中使用

    人工智能之配置环境教程二:在Anaconda中创建虚拟环境安装pytorch并在VsCode中使用虚拟环境 作者介绍 一. 在Anaconda中创建虚拟环境 1. 进入本地终端 1.1 键盘使用**w ...

  8. 在linux系统中创建文件夹,Linux系统中创建文件夹命令详解

    Linux系统中创建一个新的文件夹我们可以使用命令来执行,下面由学习啦小编为大家整理了Linux系统中创建文件夹命令详解,希望对大家有帮助! Linux系统中创建文件夹命令详解 一.mkdir命令使用 ...

  9. html中正方形列表标签属性,如何在HTML中创建带有方形项目符号的无序列表?

    Developed Countries The list of developed countries : US Australia New Zealand

最新文章

  1. cad表示计算机辅助,CAD计算机辅助设计之快捷键篇~( ̄▽ ̄)
  2. 移动Web怎么做屏幕适配
  3. python科学计算基础教程pdf下载-用Python做科学计算 高清晰PDF
  4. 73-递归函数1:阶乘
  5. 2、ShardingSphere 之 Sharding-JDBC实现水平分表
  6. Scala _06集合_数组(二)
  7. maven仓库理解、下载及设置
  8. Socket编程之TCP实例(附C/C++代码详解)
  9. SQL_Server_2008完全学习之第五章操作架构、索引和视图
  10. 计算机专业英语高等教育出版社2013版
  11. Error: Some file crunching failed
  12. 银行账户模拟java_使用Java模拟银行账户存、取款、转账功能
  13. 配置 nginx server 出现nginx: [emerg] root directive is duplicate in /etc/nginx/server/blogs.conf:107...
  14. matlab导弹追踪,导弹追踪代码
  15. mac电脑修改网卡mac地址
  16. 原来这才是折叠屏的刚需...
  17. DAMA数据治理学习笔记-大数据和数据科学
  18. pptp 能连接 但是不能上网
  19. MFS分布式文件系统(一) ——MFS简介+部署+使用
  20. android将一个long型转成时间字符串

热门文章

  1. SSL协议安全系列:PKI体系中的证书吊销
  2. JPA 2.2改进了易用性
  3. 面对不同用户,数据中心如何将服务做到极致
  4. Saltstack笔记
  5. vs2010 使用vs online账号 需要安装的插件
  6. Android Handler 深入学习(1)
  7. hbase参数配置及优化
  8. 【数据安全案例】北京破获贩卖个人信息案 涉及上千万条公民信息
  9. 2010年7月blog汇总:OpenTest、MetaModelEngine和敏捷个人
  10. 妙用终截者密码锁防***注入Explorer