Why we have tuple and list in python
The most notable difference between tuple and list is that tuple is immutable and list is mutable. But why we have this two ?
- Performance and cost
Tuple is immutable so you can not change it. Once you create it, it is done. It will be a fixed memory area(may not be continues). But list is difference. Once you create a list you may need to add more element to it. So python will reserve some space for you when you create a list. Even you will not add element to it.
So the size of tuple will be less than the size of list
>>> t = ('a','b','c')
>>> l = ['a','b','c']
>>> import sys
>>> sys.getsizeof(l)
96
>>> sys.getsizeof(t)
80
- Usage scenario
Suppose you have a data structure record a location in a book. You can use (100,28) to represent the location. 100 is the page number and 28 is the line. Because the location in a book can not be changed once it got printed. So you should never change the location data structure. Then tuple will be more suitable for it.
If you want to save multi-locations, you can put use a list to contain these tuple.
Why we have tuple and list in python的更多相关文章
- python基础——使用list和tuple
python基础——使用list和tuple list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出班里所有同学的名字,就可以用 ...
- Python元组(tuple)
元组(tuple)是Python中另一个重要的序列结构,与列表类型,也是由一系列按特定顺序排列的元素组成,但是他是不可变序列.在形式上元组的所有元素都放在"()"中,两个元素使用& ...
- Python 3 -- 数据结构(list, dict, set,tuple )
看了<Head First Python>后,觉得写的很不错,适合新手.此处为读书笔记,方便日后查看. Python 提供了4中数据结构:list,dict,set,tuple. 每种结构 ...
- [Python]字典Dictionary、列表List、元组Tuple差异化理解
概述:Python中这三种形式的定义相近,易于混淆,应注意区分. aDict={'a':1, 'b':2, 'c':3, 'd':4, 'e':5} aList=[1,2,3,4,5] aTuple= ...
- python 类C数组的两种形式:list -->内容可变, tuple --->内容不可变
python 中的列表相当与 C 中的数组,列表:list 初始化使用[ ], 元组:tuple 初始化使用(): 一.列表list 1 #!/usr/bin/python 2 3 #list初 ...
- 老Python带你从浅入深探究Tuple
元组 Python中的元组容器序列(tuple)与列表容器序列(list)具有极大的相似之处,因此也常被称为不可变的列表. 但是两者之间也有很多的差距,元组侧重于数据的展示,而列表侧重于数据的存储与操 ...
- Python基础之list和tuple的使用
list和tuple的使用 list Python内置的一种数据类型列表:list list是一种有序的集合,可以随身添加和删除其中的元素. 比如列出办理所有同学的名字,就可以用一个list表示: & ...
- Python基础二
1.for循环后接else __author__ = "zhou" age_of_oldboy = 56 for i in range(3): guess_age = int(in ...
- python知识点总结
此知识要点,是根据学习廖雪峰phthon3.0教程总结的,所以结构基本和这个教程的结构相同. 背景知识 python是什么?(1)python是一门编程语言,意味着可以用python编写程序,完成一定 ...
随机推荐
- 机器学习-Logistic function(Sigmoid function)
下面给出H函数 由这个函数生成的曲线称为Sigmoid曲线 先不从数学上说为什么这个模型中二元分类上比线性模型好,单纯从图形上看就可以得到直观的结论 首先Y值域在[0,1],其次图形中中间陡峭而两 ...
- Jquery+ashx实现Ajax
一 Ajax的实现方式 1.使用一般的webform,在页面用jQuery ajax调用,再从取得的html数据中取得<body>内的内容,写入DOM 优点:不用改变现有的asp.net开 ...
- python mail
转载一个不错python mail封装 #!/usr/bin/python from email.MIMEText import MIMEText from email.MIMEMultipart i ...
- 拼字符串 showArray.push(data); showArray.join(",")
//---// var showColumns = getShowColsRows("table");// var showArray = [];// $.each(showCol ...
- 03XML Schema Definition
1. XML Schema Definition 1. XML Schema Definition XML Schema(XML Schema Definition,XSD)用于描述 XML 文档的结 ...
- 子例程 subroutine
#! /usr/bin/perluse strict;use warnings;print "\n---------summation_STDIN_parameter----------\n ...
- Spring Cloud和Dubbo的对比
- thinkphp5中extend的使用?
1.创建处理数组的类ArrayList.php <?php /** * ArrayList实现类 * @author liu21st <liu21st@gmail.com> */ c ...
- BZOJ 2406 LuoguP4194 矩阵 有上下界可行流
分析: 这道题乍一看……卧槽这都什么玩意…… 然后发现给了个A矩阵,要求一个可行的B矩阵,使得矩阵C=A-B的每一行的和的绝对值和每一列的和的绝对值的最大值最小…… 好拗口啊…… 什么最大值最小之类的 ...
- [LUOGU] P3354 [IOI2005]Riv 河流
题目描述 几乎整个Byteland王国都被森林和河流所覆盖.小点的河汇聚到一起,形成了稍大点的河.就这样,所有的河水都汇聚并流进了一条大河,最后这条大河流进了大海.这条大河的入海口处有一个村庄--名叫 ...