您的位置:首页 > 编程语言 > Python开发

Python列表详解(一)

2016-08-18 23:49 561 查看

交互解释器

Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:40:30) [MSC v.1500 64 bit (
AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.


建立空列表

>>> world = []


显示内建方法

dir()函数是查看函数或模块内的操作方法,输出方法列表。

>>> dir(world)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delsli
ce__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getit
em__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__',
'__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__r
educe__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__'
, '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'a
ppend', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'
]


显示内建方法的详细说明

help()函数是查看函数或模块用途的详细说明

append方法:列表尾部元素添加

>>> help(world.append)
Help on built-in function append:

append(...)
L.append(object) -- append object to end


实例1:字符串用引号引用

>>> world.append('God')
>>> world.append(1)
>>> world.append(2)
>>> world.append('all')


实例2:仅限一个参数

>>> world.append('ChangerLee','kai')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: append() takes exactly one argument (2 given)


实例3:列表尾部添加元素也包括:列表,字典,元组

>>> world.append(['ChangerLee','kai'])
>>> world
['God', 1, 2, 'all', ['ChangerLee', 'kai']]


count方法:返回元素在列表中出现的个数

>>> help(world.count)
Help on built-in function count:

count(...)
L.count(value) -> integer -- return number of occurrences of value


实例4:

>>> world
['God', 1, 2, 'all', ['ChangerLee', 'kai']]
>>> world.count(1)
1
>>> world.count('God')
1
>>> world.count('Gody')
0


extend方法:通过迭代器的元素添加来扩展列表,即将列表合并到原列表上

>>> help(world.extend)
Help on built-in function extend:

extend(...)
L.extend(iterable) -- extend list by appending elements from the iterable


实例5:

>>> world
['God', 1, 2, 'all', ['ChangerLee', 'kai']]
>>> world.extend(['flowers','fish'])
>>> world
['God', 1, 2, 'all', ['ChangerLee', 'kai'], 'flowers', 'fish']


index方法:返回列表中指定元素第一次出现时的所在下标,元素不存在则报错,可选参数表示从start下标到stop下标该元素第一次出现的位置

>>> help(world.index)
Help on built-in function index:

index(...)
L.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.


实例6:

>>> world
['God', 1, 2, 'all', ['ChangerLee', 'kai'], 'flowers', 'fish']
>>> world.index('God')
0
>>> world.index(2)
2
>>> list1 = [1,2,3,4,5,6]
>>> list1 *=5
>>> list1
[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2,
, 4, 5, 6]

>>> list1.index(1,6,10)
6
>>>


实例7:

>>> world.index(num)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'num' is not defined


insert方法:将元素插入指定index的位置

>>> help(world.insert)
Help on built-in function insert:

insert(...)
L.insert(index, object) -- insert object before index


实例8:

>>> world
['God', 1, 2, 'all', ['ChangerLee', 'kai'], 'flowers', 'fish']
>>> world.insert(1,'insertvar')
>>> world
['God', 'insertvar', 1, 2, 'all', ['ChangerLee', 'kai'], 'flowers', 'fish']


pop方法:将元素从index对应的列表中的位置删除,默认从列表尾部删除

>>> help(world.pop)
Help on built-in function pop:

pop(...)
L.pop([index]) -> item -- remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.


实例9:

>>> world
['God', 'insertvar', 1, 2, 'all', ['ChangerLee', 'kai'], 'flowers', 'fish']
>>> world.pop()
'fish'
>>> world
['God', 'insertvar', 1, 2, 'all', ['ChangerLee', 'kai'], 'flowers']
>>> world.pop(1)
'insertvar'
>>> world
['God',  1, 2, 'all', ['ChangerLee', 'kai'], 'flowers']


remove方法:删除列表中已存在指定的元素,删除列表中不存在的元素将报错

>>> help(world.remove)
Help on built-in function remove:

remove(...)
L.remove(value) -- remove first occurrence of value.
Raises ValueError if the value is not present.


实例10:

>>> world
['God', 1, 2, 'all', ['ChangerLee', 'kai'], 'flowers']
>>> world.remove(1)
>>> world
['God', 2, 'all', ['ChangerLee', 'kai'], 'flowers']
>>> world.remove(3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> world
['God', 2, 'all', ['ChangerLee', 'kai'], 'flowers']


实例11:del语句删除

>>> world
['God', 2, 'all', ['ChangerLee', 'kai'], 'flowers']
>>> del world[1]
>>> world
['God', 'all', ['ChangerLee', 'kai'], 'flowers']
>>> del world
>>> world
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'world' is not defined


sort方法 :排序

>>> help(world.sort)
Help on built-in function sort:

sort(...)
L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
cmp(x, y) -> -1, 0, 1


实例12:

>>> world = [1,2,3,33,22,11,'d','a','c',['qwer','asdf'],'ChangerLee','God']
>>> world.sort()
>>> world
[1, 2, 3, 11, 22, 33, ['qwer', 'asdf'], 'ChangerLee', 'God', 'a', 'c', 'd']
>>> world.sort(reverse=True)
>>> world
['d', 'c', 'a', 'God', 'ChangerLee', ['qwer', 'asdf'], 33, 22, 11, 3, 2, 1]
>>>


reverse方法:转置,倒序

>>> help(world.reverse)
Help on built-in function reverse:

reverse(...)
L.reverse() -- reverse *IN PLACE*


实例13:

>>> world
[1, 2, 3, 11, 22, 33, ['qwer', 'asdf'], 'ChangerLee', 'God', 'a', 'c', 'd']
>>> world.reverse()
>>> world
['d', 'c', 'a', 'God', 'ChangerLee', ['qwer', 'asdf'], 33, 22, 11, 3, 2, 1]
>>>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息