python中的pop

pop()将列表指定位置的元素移除,同时可以将移除的元素赋值给某个变量,不填写位置参数则默认删除最后一位

pop()根据键将字典中指定的键值对删除,同时可以将删除的值赋值给变量

举个例子:

1 a = ["hello", "world", "dlrb"]
2 b = ["hello", "world", "dlrb"]
3 a.pop(1)
4 b1 = b.pop(0)
5 print(a)
6 print(b1)

输出结果:

['hello', 'dlrb']
hello

我们将列表a的位置1的元素移除

将列表b的位置0的元素移除并赋值给变量b1

 

1 b = {
2     "name":"dlrb",
3     "age":25,
4     "height":168
5 }
6 b1 = b.pop("age")
7 print(b)
8 print(b1)

输出结果:

{'name': 'dlrb', 'height': 168}
25

 

posted @ 2017-09-16 10:18  萌萌哒小强儿  阅读(7345)  评论(0编辑  收藏  举报