python中list常用操作实例详解

本文实例讲述了python中list常用操作。分享给大家供大家参考。具体分析如下:

1.定义list

>>> li = [“a”, “b”, “mpilgrim”, “z”, “example”]
>>> li
[‘a’, ‘b’, ‘mpilgrim’, ‘z’, ‘example’]
>>> li[0]
‘a’
>>> li[4]
‘example’

2.负的list 索引

>>> li
[‘a’, ‘b’, ‘mpilgrim’, ‘z’, ‘example’]
>>> li[-1]
‘example’
>>> li[-3]
‘mpilgrim’
>>> li
[‘a’, ‘b’, ‘mpilgrim’, ‘z’, ‘example’]
>>> li[1:3]
[‘b’, ‘mpilgrim’]
>>> li[1:-1]
[‘b’, ‘mpilgrim’, ‘z’]
>>> li[0:3]
[‘a’, ‘b’, ‘mpilgrim’]

3.向 list 中增加元素

>>> li
[‘a’, ‘b’, ‘mpilgrim’, ‘z’, ‘example’]
>>> li.append(“new”)
>>> li
[‘a’, ‘b’, ‘mpilgrim’, ‘z’, ‘example’, ‘new’]
>>> li.insert(2, “new”)
>>> li
[‘a’, ‘b’, ‘new’, ‘mpilgrim’, ‘z’, ‘example’, ‘new’]
>>> li.extend([“two”, “elements”])
>>> li
[‘a’, ‘b’, ‘new’, ‘mpilgrim’, ‘z’, ‘example’, ‘new’, ‘two’, ‘elements’]

4.搜索 list

>>> li
[‘a’, ‘b’, ‘new’, ‘mpilgrim’, ‘z’, ‘example’, ‘new’, ‘two’, ‘elements’]
>>> li.index(“example”)
5
>>> li.index(“new”)
2
>>> li.index(“c”)
traceback (innermost last):
file “”, line 1, in ?
valueerror: list.index(x): x not in list
>>> “c” in li
false

5.从 list 中删除元素

>>> li
[‘a’, ‘b’, ‘new’, ‘mpilgrim’, ‘z’, ‘example’, ‘new’, ‘two’, ‘elements’]
>>> li.remove(“z”)
>>> li
[‘a’, ‘b’, ‘new’, ‘mpilgrim’, ‘example’, ‘new’, ‘two’, ‘elements’]
>>> li.remove(“new”)
>>> li
[‘a’, ‘b’, ‘mpilgrim’, ‘example’, ‘new’, ‘two’, ‘elements’]
>>> li.remove(“c”)
traceback (innermost last):
file “”, line 1, in ?
valueerror: list.remove(x): x not in list
>>> li.pop()
‘elements’
>>> li
[‘a’, ‘b’, ‘mpilgrim’, ‘example’, ‘new’, ‘two’]

remove 从 list 中删除一个值的首次出现。
remove 仅仅 删除一个值的首次出现。 在这里, ‘new’ 在 list 中出现了两次, 但 li.remove(“new”) 只删除了 ‘new’ 的首次出现。
如果在 list 中没有找到值, python 会引发一个异常来响应 index 方法。
pop 会做两件事: 删除 list 的最后一个元素, 然后返回删除元素的值。

6.list 运算符

>>> li = [‘a’, ‘b’, ‘mpilgrim’]
>>> li = li + [‘example’, ‘new’]
>>> li
[‘a’, ‘b’, ‘mpilgrim’, ‘example’, ‘new’]
>>> li += [‘two’]
>>> li
[‘a’, ‘b’, ‘mpilgrim’, ‘example’, ‘new’, ‘two’]
>>> li = [1, 2] * 3
>>> li
[1, 2, 1, 2, 1, 2]

7.使用join链接list成为字符串

>>> params = {“server”:”mpilgrim”, “database”:”master”, “uid”:”sa”, “pwd”:”secret”}
>>> [“%s=%s” % (k, v) for k, v in params.items()]
[‘server=mpilgrim’, ‘u, ‘database=master’, ‘pwd=secret’]
>>> “;”.join([“%s=%s” % (k, v) for k, v in params.items()])
‘server=mpilgrim;u

join 只能用于元素是字符串的 list; 它不进行任何的类型强制转换。连接一个存在一个或多个非字符串元素的 list 将引发一个异常。

8.分割字符串

>>> li = [‘server=mpilgrim’, ‘u, ‘database=master’, ‘pwd=secret’]
>>> s = “;”.join(li)
>>> s
‘server=mpilgrim;u
>>> s.split(“;”)
[‘server=mpilgrim’, ‘u, ‘database=master’, ‘pwd=secret’]
>>> s.split(“;”, 1)
[‘server=mpilgrim’, ‘u]

split 与 join 正好相反, 它将一个字符串分割成多元素 list。
注意, 分隔符 (“;”) 被完全去掉了, 它没有在返回的 list 中的任意元素中出现。
split 接受一个可选的第二个参数, 它是要分割的次数。

9.list的映射解析

>>> li = [1, 9, 8, 4]
>>> [elem*2 for elem in li]
[2, 18, 16, 8]
>>> li
[1, 9, 8, 4]
>>> li = [elem*2 for elem in li]
>>> li
[2, 18, 16, 8]

10.dictionary中的解析

>>> params = {“server”:”mpilgrim”, “database”:”master”, “uid”:”sa”, “pwd”:”secret”}
>>> params.keys()
[‘server’, ‘uid’, ‘database’, ‘pwd’]
>>> params.values()
[‘mpilgrim’, ‘sa’, ‘master’, ‘secret’]
>>> params.items()
[(‘server’, ‘mpilgrim’), (‘uid’, ‘sa’), (‘database’, ‘master’), (‘pwd’, ‘secret’)]
>>> [k for k, v in params.items()]
[‘server’, ‘uid’, ‘database’, ‘pwd’]
>>> [v for k, v in params.items()]
[‘mpilgrim’, ‘sa’, ‘master’, ‘secret’]
>>> [“%s=%s” % (k, v) for k, v in params.items()]
[‘server=mpilgrim’, ‘u, ‘database=master’, ‘pwd=secret’]

11.列表过滤

>>> li = [“a”, “mpilgrim”, “foo”, “b”, “c”, “b”, “d”, “d”]
>>> [elem for elem in li if len(elem) > 1]
[‘mpilgrim’, ‘foo’]
>>> [elem for elem in li if elem != “b”]
[‘a’, ‘mpilgrim’, ‘foo’, ‘c’, ‘d’, ‘d’]
>>> [elem for elem in li if li.count(elem) == 1]
[‘a’, ‘mpilgrim’, ‘foo’, ‘c’]

希望本文所述对大家的python程序设计有所帮助。

Posted in 未分类

发表评论