定义的异同:

列表(list):[]

list是一种有序的集合,能够随时加入和删除当中的元素。用 [] 表示。

列表的三个特性:①创建之后也能够加减改动元素。 ②元素能够是数字、字符、变量等。也能够混杂。 ③列表能够嵌套。

比如:

>>>f=3
>>>list_example = [1, 'dog', f, ['monkey', 'duck']]
>>>list_example
[1, 'dog', 3, ['monkey', 'duck']]

元组(tuple): ()

tuple也是有序的集合,它和list不同的是它仅仅能在初始化的时候赋值,之后就不能再进行加入删除元素了。用 () 表示。

元组的三个特性:①创建之后不能加减改动元素; ②元素也能够是数字、字符、变量或者混杂; ③元组也能够嵌套。

比如:

>>>f=3
>>>tuple_example = (1, 'dog', f, ('monkey', 'duck'))
>>>tuple_example
(1, 'dog', 3, ('monkey', 'duck'))

综上能够看出,tuple和list的不同主要在于: tuple在初始化之后不能再改动,可是list能够

各种同样:

两者除了在初始化后是否能再变化这个问题上不同样外。其它方面差点儿都是同样的。

初始化:

list_example = [1, 2, [3, 4], 5, 6]

tuple_example = (7, 8, (9, 10), 11, 12)

使用方法上的同样点:

1、假设仅仅有一个元素,要在后面加个’,’,否则仅仅相当于定义了一个变量:

>>>list_example = [1]
1
>>>list_example = [1,]
[1]
>>>tuple_example = (1,)
(1,)

2、利用索引输出元素(注意索引从0開始):

>>>list_example[0]
1
>>>tuple_example[2][0]
9

3、索引为负数,表示从尾往前搜索(-1表示最后一个元素):

>>>list_example[-1]
6
>>>tuple_example[-3][-1]
10

4、tuple和list能够相互嵌套:

>>>list_example = [1,(2,3)]
[1,(2,3)]
>>>tuple_example = (4,[5,6])
>(4,[5,6])

方法上的同样点:

1、count(elem):返回元素中elem的个数

>>>tuple_example = (7, 8, (7, 10), 7, 12)
>>>tuple_example.count(7)
2

2、index(elem):返回元素elem的索引

>>>list_example = [7, 8, (7, 10), 8, 12]
>>>list_example.index(8)
1

各种不同:

由于tuple不能改动删除。而已list能够。所以有些方法是list才有的。

初始化:

list_example = [1, 2, [3, 4], 5, 6]

1、append(object):在末尾加入元素:

>>>list_example.append(100)
[1, 2, [3, 4], 5, 6, 100]
>>>list_example.append([100, 200])
[1, 2, [3, 4], 5, 6, 100, [100, 200]]

2、extend(object):在末尾加入元素,但object必须是列表。object的元素将会加入到列表的末尾:

>>>list_example.append([100, 200])
[1, 2, [3, 4], 5, 6, 100, 200]

3、insert(index,object):将元素object加入到索引为index的位置:

>>>list_example.insert(1, 'abc')
[1, 'abc', 2, [3, 4], 5, 6]

4、pop(index):将索引为index的元素弹出列表:

>>>list_example.pop(1)
2
>>>list_example
[1, [3, 4], 5, 6]

5、remove(value):删除元素value(仅仅删除第一个出现的)

>>>list_example = [1, 2, 5, [3, 4], 5, 6]
>>>list_example.remove(5)
>>>list_example
[1, 2, [3, 4], 5, 6]

6、sort(cmp=None,key=None,reverse=False):对元素排序。

默认是从小到大,不同类型的元素按数字—>列表—>字符—>元组先后排序。

>>>list_example = [1, 'cd', ('b', 'f'), 8, 'ab', [4, 3], (1, 2), [2,9], 5, 6]
>>>list_example.sort(cmp=None, key=None, reverse=False)
>>>list_example
[1, 5, 6, 8, [2, 9], [4, 3], 'ab', 'cd', (1, 2), ('b', 'f')]

总之。tuple和list功能上是非常相近的,仅仅是tuple在初始化后就不能更改了。这也说明tuple具有更高的安全性。防止数据被误改动。对于无需改动的数据,相似于“仅仅读”性质的数据,最好选用tuple。

转载请注明出处,谢谢!(原文链接:http://blog.csdn.net/bone_ace/article/details/46633029

python元组、列表的异同总结的更多相关文章

  1. python元组 列表 (取值、替换、插入、添加、删除)

    1.元组 列表 字典 元组( 元组是不可变的) hello = (1,2,3,4,5) type(hello)

  2. python 元组 列表 字典

    type()查看类型 //取整除 **幂 成员运算符: in  x在y序列中,就返回true 反之  not in 身份运算符: is is not 逻辑运算符 and or not 字符编码 问题 ...

  3. python 元组列表合并

    #create a tuple l = [(,), (,), (,)] print(list(zip(*l)))

  4. python 元组列表转为字典

    #create a list l = [(), (), (), (), (), ()] d = {} for a, b in l: d.setdefault(a, []).append(b) prin ...

  5. python中列表 元组 字典 集合的区别

    列表 元组 字典 集合的区别是python面试中最常见的一个问题.这个问题虽然很基础,但确实能反映出面试者的基础水平. (1)列表 什么是列表呢?我觉得列表就是我们日常生活中经常见到的清单.比如,统计 ...

  6. Python基础------列表,元组的调用方法

    Python基础------列表,元组的调用方法@@@ 一. 列表 Python中的列表和歌曲列表类似,也是由一系列的按特定顺序排列的元素组成的,在内容上,可以将整数,实数,字符串,列表,元组等任何类 ...

  7. Python中列表、元组、集合、字典

    Python 列表(List) 列表是最常用的Python数据类型: 列表中的数据项不需要具有相同的类型: 列表也叫做数组,定义时使用[]: 通过下标访问列表中的元素,下标从 0  开始计数 列表的增 ...

  8. python 元组不变 列表可变

    python 元组不变 列表可变 1,   --元组,注意要有逗号: [1] --列表 竟然才开始写python blog:

  9. Python基础——列表、元组操作

    列表.元组操作 列表: 列表是Python中最基本的数据结构,列表是最常用的Python数据类型,列表的数据项不需要具有相同的类型.列表中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0 ...

  10. Python 元组和列表

    Python 元组 Python的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. 如下实例: tup1 ...

随机推荐

  1. Educational Codeforces Round 30 A[水题/数组排序]

    A. Chores time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  2. 大数据技术之_16_Scala学习_07_数据结构(上)-集合

    第十章 数据结构(上)-集合10.1 数据结构特点10.1.1 Scala 集合基本介绍10.1.2 可变集合和不可变集合举例10.2 Scala 不可变集合继承层次一览图10.2.1 图10.2.2 ...

  3. NOI 1.5编程基础之循环控制 44:第n小的质数

    描述 输入一个正整数n,求第n小的质数. 输入 一个不超过10000的正整数n. 输出 第n小的质数. 样例输入 10 样例输出 29

  4. Kattis - boxes (dfn序)

    Boxes There are N boxes, indexed by a number from 1 to N . Each box may (or not may not) be put into ...

  5. Compare, sort, and delete duplicate lines in Notepad ++

    Compare, sort, and delete duplicate lines in Notepad ++ Organize Lines: Since version 6.5.2 the app ...

  6. 【DFS】Codeforces Round #398 (Div. 2) C. Garland

    设sum是所有灯泡的亮度之和 有两种情况: 一种是存在结点U和V,U是V的祖先,并且U的子树权值和为sum/3*2,且U不是根,且V的子树权值和为sum/3. 另一种是存在结点U和V,他们之间没有祖先 ...

  7. 【DFS序】【莫队算法】【权值分块】bzoj2809 [Apio2012]dispatching

    题意:在树中找到一个点i,并且找到这个点子树中的一些点组成一个集合,使得集合中的所有点的c之和不超过M,且Li*集合中元素个数和最大 首先,我们将树处理出dfs序,将子树询问转化成区间询问. 然后我们 ...

  8. 统计正数和负数的个数,然后计算这些数的平均值 Exercise05_01

    import java.util.Scanner; /** * @author 冰樱梦 * * */ public class Exercise05_01{ public static void ma ...

  9. IO流--FileReader&&FileWriter

    (一)FileReader (1)第一种读取方式 package com.songyan.fileReader; import java.io.FileNotFoundException; impor ...

  10. OC语言基础之NSDictionary

    1.NSDictionary字典的创建 1: // key value 2: // key -==> value 3: NSDictionary *dict = [NSDictionary di ...