Python 从零开始程序员

[Python] (Day-18) - List 列表实际操作

2017-10-27  本文已影响8人  已重置2020
The shortest distance between two people is a smile. 人与人之间最短的距离是微笑。

List 列表实际操作练习


1、List 定义

>>> list = ["Mazy", "Joy", "Vivian", "Vicky", "Eric"]
>>> list
['Mazy', 'Joy', 'Vivian', 'Vicky', 'Eric']

>>> list[3]
'Vicky'

2、List 负数索引

>>> list
['Mazy', 'Joy', 'Vivian', 'Vicky', 'Eric']

>>> list[-1]
'Eric'

>>> list[-3]
'Vivian'

>>> list
['Mazy', 'Joy', 'Vivian', 'Vicky', 'Eric']
>>> list[1:3]
['Joy', 'Vivian']

>>> list[1:-1]
['Joy', 'Vivian', 'Vicky']

>>> list[0:3]
['Mazy', 'Joy', 'Vivian']

3、List 增加元素

>>> list
['Mazy', 'Joy', 'Vivian', 'Vicky', 'Eric']

>>> list.append("Owen")
>>> list
['Mazy', 'Joy', 'Vivian', 'Vicky', 'Eric', 'Owen']

>>> list.insert(2,"Lora")
>>> list
['Mazy', 'Joy', 'Lora', 'Vivian', 'Vicky', 'Eric', 'Owen']

>>> list.extend(["666", "888"])
>>> list
['Mazy', 'Joy', 'Lora', 'Vivian', 'Vicky', 'Eric', 'Owen', '666', '888']

4、List 搜索

>>> list
['Mazy', 'Joy', 'Lora', 'Vivian', 'Vicky', 'Eric', 'Owen', '666', '888']

>>> list.index("Vicky")
4

>>> list.index("666")
7

>>> list.index("Joy")
1

>>> "555" in list
False

>>> list.index("555")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: '555' is not in list

5、List 删除元素

>>> list
['Mazy', 'Lora', 'Vivian', 'Vicky', 'Eric', 'Owen', '666', '888']

>>> list.insert(1, "Joy")
>>> list
['Mazy', 'Joy', 'Lora', 'Vivian', 'Vicky', 'Eric', 'Owen', '666', '888']

>>> list.insert(3, "666")
>>> list
['Mazy', 'Joy', 'Lora', '666', 'Vivian', 'Vicky', 'Eric', 'Owen', '666', '888']

>>> list.remove("Joy")
>>> list
['Mazy', 'Lora', '666', 'Vivian', 'Vicky', 'Eric', 'Owen', '666', '888']

>>> list.remove("666") # 删除首次出现的一个值
>>> list
['Mazy', 'Lora', 'Vivian', 'Vicky', 'Eric', 'Owen', '666', '888'] # 第二个 '666' 未删除

>>> list.pop() # pop 会做两件事: 删除 list 的最后一个元素, 然后返回删除元素的值。
'888'

>>> list
['Mazy', 'Lora', 'Vivian', 'Vicky', 'Eric', 'Owen', '666']

>>> list.remove("555")  #list 中没有找到值, Python 会引发一个异常
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list

6、List 运算符

>>> list = ['Mazy', 'Lora', 'Vivian', 'Vicky']

>>> list = list + ['Eric', 'Owen']
>>> list
['Mazy', 'Lora', 'Vivian', 'Vicky', 'Eric', 'Owen']

>>> list += ["888"]
>>> list
['Mazy', 'Lora', 'Vivian', 'Vicky', 'Eric', 'Owen', '888']

>>> num = [1, 2] * 3
>>> num
[1, 2, 1, 2, 1, 2]

7.使用 Join 链接 List 成为字符串

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

>>> params = {"username":"Mazy", "password":"123456","uid":"001"}

>>> ["%s=%s" % (k, v) for k, v in params.items()]
['username=Mazy', 'password=123456', 'uid=001']

>>> ";".join(["%s=%s" % (k, v) for k, v in params.items()])
'username=Mazy;password=123456;uid=001'

8、List 分割字符串

>>> list = ['username=Mazy', 'password=123456', 'uid=001']

>>> s = ";".join(list)
>>> s
'username=Mazy;password=123456;uid=001'

>>> s.split(";")
['username=Mazy', 'password=123456', 'uid=001']

>>> s.split(";",1)
['username=Mazy', 'password=123456;uid=001']

9、List 的映射解析

>>> li = [0, 8, 2 ,1]

>>> [elem*2 for elem in li]
[0, 16, 4, 2]
>>> li
[0, 8, 2, 1]

>>> li = [elem*2 for elem in li]
>>> li
[0, 16, 4, 2]

10、Dictionary中的解析

>>> params = {"username":"Mazy", "password":"123456","uid":"001"}

>>> params.keys()
['username', 'password', 'uid']

>>> params.values()
['Mazy', '123456', '001']

>>> params.items()
[('username', 'Mazy'), ('password', '123456'), ('uid', '001')]

>>> [k for k, v in params.items()]
['username', 'password', 'uid']

>>> [v for k, v in params.items()]
['Mazy', '123456', '001']

>>> ["%s=%s" % (k, v) for k, v in params.items()]
['username=Mazy', 'password=123456', 'uid=001']

11、List 过滤

>>> li = ["a", "ABC", "foo", "b", "y", "b", "d", "d"]

>>> [elem for elem in li if len(elem) > 1]
['ABC', 'foo']

>>> [elem for elem in li if elem != "b"]
['a', 'ABC', 'foo', 'y', 'd', 'd']

>>> [elem for elem in li if li.count(elem) == 1]
['a', 'ABC', 'foo', 'y']
上一篇下一篇

猜你喜欢

热点阅读