码迷,mamicode.com
首页 > 编程语言 > 详细

Python 列表

时间:2016-08-25 21:03:51      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:

python 列表

列表的特点

    1、列表是一种可变的数据类型,这点是跟元组有区别的

    2、列表中的值是有序的,并且可存放重复的值,这点跟set有区别的

    3、python中的列表类似于其它语言中的数组

    4、列表中元素的值可以多种数据类型并存

 


列表基本操作方法

 

    1、元素赋值

1
2
3
>>> list1 = [3,9,‘python‘,‘java‘,[9,3,5]]
>>> print list1
[3, 9, ‘python‘, ‘java‘, [9,3,5]]

 

2、列表分片

1
2
3
4
5
6
7
8
9
10
11
#列表中的索引从0开始,第一个冒号前面的值代表列表索引的起始值,第一个冒号后面的值表示分片结束的索引值(不包含)
>>>print list1[0:4]
[3, 9, ‘python‘, ‘java‘]
#[::-1] 第二个冒号后面的值表示步长,可为负,负数表示按照步长倒序输出
>>>print list1[::2]
>>>print list1[::2]
[3, ‘python‘, [9, 3, 5]]
>>>print list1[::-1]
[[9, 3, 5], ‘java‘, ‘python‘, 9, 3]
>>>print list1[-1:-4:-2]
[[9, 3, 5], ‘python‘]

 

    3、删除元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
>>> print list1
[3, 9, ‘python‘, ‘java‘, [9, 3, 5]]
#remove方法是根据列表中的值进行删除
>>> list1.remove(3)
#如果remove的值在列表中的值不存在,那么会解释器会毫不留情的报错
>>> remove(1)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name ‘remove‘ is not defined
>>> print list1
[9, ‘python‘, ‘java‘, [9, 3, 5]]
#del是根据索引进行删除
>>> del list1[2]
>>> print list1
[9, ‘python‘, [9, 3, 5]]

 

   4、len方法

 

1
2
3
4
5
6
7
>>> list1 = [1,8,23,56,32,9,23,[5,9,3,9]]
#内嵌列表在外面列表看来长度为1
>>> print len(list1)
8
#计算内嵌列表的长度
>>> print len(list1[-1])
4

 

    5、count方法

 

1
2
3
4
5
6
7
8
>>> print list1.count(23)
2
#如果要统计的值在列表中不存在则返回0
>>> print list1.count(25)
0
#内嵌列表以及元组的值不会被统计进来
>>> print list1.count(9)
1

 

    6、append方法

 

1
2
3
4
5
6
#append可以添加各种数据类型
>>> list1.append(‘Kingway‘)
>>> list1.append([2,3,4,5])
>>> list1.append(9)
>>> print list1
[1, 8, 23, 56, 32, 9, 23, [5, 9, 3, 9], ‘Kingway‘, [2, 3, 4, 5], 9]

 

    7、extend方法

 

1
2
3
4
5
6
7
8
9
#extend属于列表的扩展,只能对其他列表进行扩展,而无法像append那样添加其它元素的值
>>> list1.extend([6,7,8])
>>> print list1
[1, 8, 23, 56, 32, 9, 23, [5, 9, 3, 9], ‘Kingway‘, [2, 3, 4, 5], 9, 6, 7, 8]
#直接添加其它元素的值会报错
>>> list1.extend(9)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: ‘int‘ object is not iterable

 

    8、insert方法

 

1
2
3
4
5
6
7
8
9
10
11
12
13
#insert方法中第一个参数的值为索引,第二个参数为需要插入的值,插入值可以是各种数据类型
>>> list1.insert(3,‘four‘)
>>> print list1
[1, 8, 23, ‘four‘, 56, 32, 9, 23, [5, 9, 3, 9], ‘Kingway‘, [2, 3, 4, 5], 9, 6, 7, 8]
#如果索引的值大于列表长度,那么将在列表的最后一位插入该值
>>> list1.insert(100,"end")
>>> print list1
[1, 8, 23, ‘four‘, 56, 32, 9, 23, [5, 9, 3, 9], ‘Kingway‘, [2, 3, 4, 5], 9, 6, 7, 8, ‘end‘]
#索引的值不能为负,否则将会报错
>>> list1.index(-10,‘first‘)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: slice indices must be integers or None or have an __index__ method

 

    9、pop方法

 

1
2
3
4
5
6
7
8
#pop方法用于删除相应的值并将删除的值返回,默认删除列表的最后一位
>>> list1.pop()
‘end‘
#删除并返回指定位置的值
>>> list1.pop(2)
23
>>> print list1
[1, 8, ‘four‘, 56, 32, 9, 23, [5, 9, 3, 9], ‘Kingway‘, [2, 3, 4, 5], 9, 6, 7, 8]


    10、sort方法

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#sort方法可以将列表进行排序
>>> list1 = [23,9,15,3]
>>> list1.sort()
>>> print list1
[3, 9, 15, 23]
#sort方法会改变原列表的值,sorted方法不会对原列表的值进行修改,sort为列表内置的方法,sorted为python内置的方法
>>> list1 = [23,9,15,3]
>>> list2 = sorted(list1)
>>> print list1
[23, 9, 15, 3]
>>> print list2
[3, 9, 15, 23]
#sort可以以字符串值的长度进行排序
>>> list1 = [‘a‘,‘dddd‘,‘ccc‘,‘bb‘]
>>> list1.sort(key = len)
>>> print list1
[‘a‘, ‘bb‘, ‘ccc‘, ‘dddd‘]

 

    11、其它方法

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#reverse方法,将列表中的元素倒序排列
>>> list1 = [23,5,9,18,6]
>>> list1.reverse()
>>> print list1
[6, 18, 9, 5, 23]
#max方法,将列表中的最大值输出
>>> print max(list1)
23
#min方法,将列表中的最小值输出
>>> print min(list1)
5
#index方法,将对应元素的索引输出,如果该元素在列表中不存在,则报错,如果该元素在列表中有多个,则返回第一个被查到的值的索引值
>>> list1 = [32,8,234,9,8,12]
>>> list1.index(8)
1
>>> list1.index(3)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ValueError: 3 is not in list
#cmp方法,比较两个列表的值,如果第一个大于第二个则返回1,反之结果为-1,两个列表相等则为0
>>> list1 = [9,5]
>>> list2 = [9,7]
>>> cmp(list1,list2)
-1
>>> list2 = [9,5]
>>> cmp(list1,list2)
0
>>> list2 = [7,9]
>>> cmp(list1,list2)
1

 







Python 列表

标签:

原文地址:http://www.cnblogs.com/Kingway-Python/p/5808338.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!