Python 列表基本操作

1、列表值获取

>>> number = [1,2,3,4,5,6,7,8,9,10]
#获取列表中单个元素的值
>>> print(number[5])
6
#根据索引对列表分片,放空默认情况下是获取列表中的所有元素
>>> number[:]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> number[0:3]
[1, 2, 3]
#索引-1代表倒数第二个元素
>>> number[7:-1]
[8, 9]
#根据步长获取列表中数据,注意:如果步长为负数时,必须让开始点大于结束点,否则有可能获取到的值为空
>>> number[0:7:2]
[1, 3, 5, 7]
>>> number[0:7:-2]
[]
>>> number[7:0:-2]
[8, 6, 4, 2]

2、列表简单运算操作

#列表乘法,列表只能乘简单的整型数字
>>> num = [8]
>>> num * 5
[8, 8, 8, 8, 8]
#两个列表之间能直接相加,list2追加在list1后面
>>> num = [1,2,3,4] + [5,6,7,8]
>>> num
[1, 2, 3, 4, 5, 6, 7, 8]
#注意:列表之间不能进行 减法、乘法、除法运算
>>> a = [1,2,3] * [4,5,6]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'list'
>>> a = [1,2,3,4,5,6] - [1,2,3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'list' and 'list'

3、计算列表长度、最大值、最小值

>>> number
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> len(number)
10
>>> max(number)
10
>>> min(number)
1

 4、list转换函数

#list函数能将字符串转换为列表
>>> list('hello')
['h', 'e', 'l', 'l', 'o']
>>> tmpList=list('hello')
>>> tmpList
['h', 'e', 'l', 'l', 'o']

5、删除列表中元素

#删除列表中的元素
>>> listA
[9, 8, 7, 6, 5, 4, 3, 1]
#remove(元素),remove移除的是列表中的元素,remove只能移除列表中首个该元素
>>> listA.remove(4)
>>> listA
[9, 8, 7, 6, 5, 3, 1]
#pop(),弹出列表末尾元素或移除指定索引的值
>>> listA.pop()
1
>>> listA
[9, 8, 7, 6, 5, 3]
#del 删除列表中索引对应的元素
>>> del listA[5]
>>> listA
[9, 8, 7, 6, 5]

6、列表常用方法

>>> dir(listA)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> numList
[1, 2, 3, 4]
#append(元素) 往列表默追加内容,能往列表中加入任何类型元素
>>> numList.append('5')
>>> numList
[1, 2, 3, 4, '5']
#count(元素) 计算列表中某元素出现次数
>>> numList
[1, 2, 3, 4, 6, 6, 6]
>>> numList.count(6)
3
#index(元素)查找某个元素在列表中的索引
>>> numList.index(6)
4
>>> numList.index(4)
3
#extend(列表),能往列表中追加列表,extend()是往原始列表中直接追加内容,而不是新建个列表。连接操作则是新生产一个列表
>>> numList.extend([7,8,9,10])
>>> numList
[1, 2, 3, 4, 6, 6, 6, 7, 8, 9, 10]
>>> numList.pop()
10
>>> numList
[1, 2, 3, 4, 6, 6, 6, 7, 8, 9]
#将整个列表取反
>>> numList.reverse()
>>> numList
[9, 8, 7, 6, 6, 6, 4, 3, 2, 1]
>>> numList.extend([8,2,5,10])
>>> numList
[9, 8, 7, 6, 6, 6, 4, 3, 2, 1, 8, 2, 5, 10]
>>> numList.reverse()
>>> numList
[10, 5, 2, 8, 1, 2, 3, 4, 6, 6, 6, 7, 8, 9]
#sort() 列表从小到达排列
>>> numList.sort()
>>> numList
[1, 2, 2, 3, 4, 5, 6, 6, 6, 7, 8, 8, 9, 10]
# enumerate 打印列表中对应的索引
myList = ['hello', 'ios', 'object', 'python', 'swift', 'zws', 'zxk']
for k,v in enumerate(myList):
print (k, v)

 

posted @ 2017-03-29 20:33  漫舞沧海  阅读(166)  评论(0编辑  收藏  举报