首页 > 其他 > 详细

Lesson 2-2(列表,元组)

时间:2019-02-10 18:00:56      阅读:187      评论:0      收藏:0      [点我收藏+]

2.3 列表

2.3.1 列表的创建方法

--- 使用方括号 [] ,括号中的元素之间使用逗号隔开。

1 >>> [1, 2, 3, 4, 5]
2 [1, 2, 3, 4, 5]
3 >>> lis = [H, e, l, l, o]
4 >>> lis
5 [H, e, l, l, o]
6 >>> 

--- 使用函数list创建列表,可将任何序列作为list的参数。

1 >>> list(Hello worl!)
2 [H, e, l, l, o,  , w, o, r, l, !]
3 >>> lis = list("Hello world!")
4 >>> lis
5 [H, e, l, l, o,  , w, o, r, l, d, !]
6 >>> lis = list([H, e, l, l, o])
7 >>> lis
8 [H, e, l, l, o]
9 >>> 

 

2.3.2 列表操作

--- 可对列表执行序列的通用操作,如索引,切片,拼接,复制。

--- 修改列表,通过索引指定要修改位置的元素,可修改为相似的元素,也可修改为不一致的元素。

 1 >>> lis = list("Hello world!")
 2 >>> lis
 3 [H, e, l, l, o,  , w, o, r, l, d, !]
 4 >>> lis[1] = a
 5 >>> lis
 6 [H, a, l, l, o,  , w, o, r, l, d, !]
 7 >>> lis[1] = [a, b, c]
 8 >>> lis
 9 [H, [a, b, c], l, l, o,  , w, o, r, l, d, !]
10 >>> 

--- del 删除列表中的元素,也可删除整个列表。

 1 >>> lis = [1, 2, 3, 4, 5]
 2 >>> lis
 3 [1, 2, 3, 4, 5]
 4 >>> del lis[1]
 5 >>> lis
 6 [1, 3, 4, 5]
 7 >>> del lis
 8 >>> lis
 9 Traceback (most recent call last):
10   File "<stdin>", line 1, in <module>
11 NameError: name lis is not defined
12 >>> 

--- 给切片赋值,可同时给多个元素赋值,也可将切片替换为长度不同的序列,也可不改变原列表的元素而插入新元素。

 1 >>> lis = list("Hello world!")
 2 >>> lis
 3 [H, e, l, l, o,  , w, o, r, l, d, !]
 4 >>> lis[-6:-1] = list("python")
 5 >>> lis
 6 [H, e, l, l, o,  , p, y, t, h, o, n, !]
 7 >>> lis = list("Hello")
 8 >>> lis[2:2] = list(abc)
 9 >>> lis
10 [H, e, a, b, c, l, l, o]
11 >>> 

--- 多重赋值,接收赋值的变量的数量必须和列表的长度严格相等。

 1 >>> lis = [1, 2, 3]
 2 >>> a,b,c = lis
 3 >>> a
 4 1
 5 >>> b
 6 2
 7 >>> c
 8 3
 9 >>> a,b = lis
10 Traceback (most recent call last):
11   File "<stdin>", line 1, in <module>
12 ValueError: too many values to unpack (expected 2)
13 >>> 

 

2.3.3 列表的方法

--- append,就地修改列表,将一个对象附加到列表末尾。(列表专属方法)

1 >>> lis = [1, 2, 3]
2 >>> lis.append(4)
3 >>> lis
4 [1, 2, 3, 4]
5 >>> lis = [1, 2, 3]
6 >>> lis.append([4, 5])
7 >>> lis
8 [1, 2, 3, [4, 5]]
9 >>> 

--- insert,将一个对象插入列表,insert带有两个参数,第一个表示插入位置的索引,第二个表示要插入的对象。(列表专属方法)

1 >>> lis = [1, 2, 3]
2 >>> lis.insert(1,5)
3 >>> lis
4 [1, 5, 2, 3]
5 >>> lis = [1, 2, 3]
6 >>> lis.insert(1,[4, 5])
7 >>> lis
8 [1, [4, 5], 2, 3]
9 >>> 

--- clear,清空列表中的元素,使其成为空列表。

1 >>> lis = [1, 2, 3]
2 >>> lis.clear()
3 >>> lis
4 []
5 >>> 

--- copy,使用常规的‘=’号赋值方式来复制,只是起到关联内存位置的作用,copy则是拷贝了一个副本到新的内存位置。

 1 >>> lis_1 = [1, 2, 3]
 2 >>> lis_2 = lis_1
 3 >>> id(lis_1)
 4 140151340507336
 5 >>> id(lis_2)
 6 140151340507336
 7 >>> lis_2[0] = 5
 8 >>> lis_2
 9 [5, 2, 3]
10 >>> lis_1
11 [5, 2, 3]
12 >>> 
 1 >>> lis_1 = [1, 2, 3]
 2 >>> lis_2 = lis_1.copy()
 3 >>> id(lis_1)
 4 140151340498312
 5 >>> id(lis_2)
 6 140151340507208
 7 >>> lis_2[0] = 5
 8 >>> lis_2
 9 [5, 2, 3]
10 >>> lis_1
11 [1, 2, 3]
12 >>> 

--- extend,同时将多个值附加到列表末尾。常规通过“+”运算符拼接两个列表,新的结果放到新的内存地址空间。而extend则在原内存地址空间把值附加进去。

 1 >>> lis_1 = [1 ,2]
 2 >>> lis_2 = [3 ,4]
 3 >>> lis_1 + lis_2
 4 [1, 2, 3, 4]
 5 >>> lis_1 
 6 [1, 2]
 7 >>> lis_1 = [1 ,2]
 8 >>> lis_2 = [3 ,4]
 9 >>> lis_1.extend(lis_2)
10 >>> lis_1
11 [1, 2, 3, 4]
12 >>> 

--- count,计算指定元素在列表中存在的次数。

1 >>> lis = list(Hello world!)
2 >>> lis.count(e)
3 1
4 >>> lis.count(l)
5 3
6 >>> lis.count(o)
7 2
8 >>> 

--- index,在列表中查找指定值第一次出现的位置的索引。

1 >>> lis = [1, 2, 3, 1, 2, 1]
2 >>> lis.index(2)
3 1
4 >>> lis.index(1)
5 0
6 >>> 

--- remove,删除指定值在列表中第一次出现的元素。

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

--- pop,删除指定索引位置的一个元素,并把删除元素返回(默认则删除列表末尾一个元素)。

 1 >>> lis = [1, 2, 3, 4, 5]
 2 >>> lis.pop(2)
 3 3
 4 >>> lis
 5 [1, 2, 4, 5]
 6 >>> lis.pop()
 7 5
 8 >>> lis
 9 [1, 2, 4]
10 >>> 

 --- reverse,按相反的顺序排列列表中的元素。

1 >>> lis = [1, 2, 3]
2 >>> lis.reverse()
3 >>> lis
4 [3, 2, 1]
5 >>> 

--- sort,对列表就地排序,按ASCII码表顺序,且列表中元素必须是同类型数据。

 1 >>> lis = [a, A, z, Z]
 2 >>> lis.sort()
 3 >>> lis
 4 [A, Z, a, z]
 5 >>> lis = [1, 2, a, A]
 6 >>> lis.sort()
 7 Traceback (most recent call last):
 8   File "<stdin>", line 1, in <module>
 9 TypeError: < not supported between instances of str and int
10 >>> 

2.3.4 列表可用于循环

1 >>> for i in [1, 2, 3, 4]:
2 ...     print(i*2)
3 ... 
4 2
5 4
6 6
7 8
8 >>> 
 1 >>> lis = [1, 2, 3, 4, 5]
 2 >>> for i in range(len(lis)):
 3 ...     print("lis[" + str(i) + "]:" + str(lis[i])) 
 4 ... 
 5 lis[0]:1
 6 lis[1]:2
 7 lis[2]:3
 8 lis[3]:4
 9 lis[4]:5
10 >>> 

 

2.4 元组

--- 与列表一样,元组也是序列,唯一的差别是元组不能修改。

--- 元组的创建,使用逗号分隔,使用圆括号括起。(逗号很重要)

 1 >>> (1, 2, 3)
 2 (1, 2, 3)
 3 >>> (1)
 4 1
 5 >>> (1,)
 6 (1,)
 7 >>> type((1))
 8 <class int>
 9 >>> type((1,))
10 <class tuple>
11 >>> 3 * (40 + 2)
12 126
13 >>> 3 * (40 + 2,)
14 (42, 42, 42)
15 >>> 

--- 元组的创建,使用函数tuple,将一个序列作为参数,并将其转换为元组。如果参数已经是元组,则原样返回。

1 >>> tuple([1, 2, 3])
2 (1, 2, 3)
3 >>> tuple("Hello world!")
4 (H, e, l, l, o,  , w, o, r, l, d, !)
5 >>> tuple((1, 2, 3, 4, 5))
6 (1, 2, 3, 4, 5)
7 >>> 

 

Lesson 2-2(列表,元组)

原文:https://www.cnblogs.com/jlufcs/p/10358729.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!