开发者

python chaining

开发者 https://www.devze.com 2022-12-19 07:33 出处:网络
Updated Let\'s say I have: dic={\"z\":\"zv\", \"a\":\"av\"} ##开发者_如何转开发 why doesn\'t the following return a sorted list of keys?

Updated

Let's say I have:

dic={"z":"zv", "a":"av"}
##开发者_如何转开发 why doesn't the following return a sorted list of keys?
keys=dic.keys().sort()

I know I could do the following and have the proper result:

dic={"z":"zv", "a":"av"}
keys=dic.keys()
skeys=keys.sort()  ### skeys will be None

Why doesn't the first example work?


.sort doesn't return the list. You could do:

keys = sorted(dic.keys())


sort() modifies the contents of the existing list. it doesn't return a list. See the manual.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号