Skip to content

Commit 9357bcf

Browse files
committed
dict pop
1 parent ac56101 commit 9357bcf

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

117.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ get的含义是:
317317
D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
318318
If key is not found, d is returned if given, otherwise KeyError is raised
319319

320-
`D.pop(k[,d])`是以字典的键为参数,删除指定键的键值对,当然,如果输入对应的值也可以,那个是可选的
320+
`D.pop(k[,d])`是以字典的键为参数,删除指定键的键值对。
321321

322322
>>> dd
323323
{'lang': 'python', 'web': 'www.itdiffer.com', 'name': 'qiwsir'}
@@ -343,6 +343,8 @@ get的含义是:
343343
File "<stdin>", line 1, in <module>
344344
KeyError: 'name'
345345

346+
`pop`的参数,可以是两个,上面的例子中只写了一个。如果写两个,那么就先检查k是不是存在于字典中的键,如果是,就返回它所对应的值,如果不是,就返回参数中的第二个,当然,如果不写第二个参数,就会如同上面举例一样报错。
347+
346348
有意思的是`D.popitem()`倒是跟`list.pop()`有相似之处,不用写参数(list.pop是可以不写参数),但是,`D.popitem()`不是删除最后一个,前面已经交代过了,dict没有顺序,也就没有最后和最先了,它是随机删除一个,并将所删除的返回。
347349

348350
popitem(...)

0 commit comments

Comments
 (0)