Python列表详解(一)

交互解释器

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]
>>>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JaysenLeo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值