0

我目前正在使用本指南学习 python(如果您想要更多上下文)http://www.diveintopython3.net/comprehensions.html#dictionarycomprehension

>>> import os, glob, humansize
>>> metadata_dict = {f:os.stat(f) for f in glob.glob('*')}                                  ①
>>> humansize_dict = {os.path.splitext(f)[0]:humansize.approximate_size(meta.st_size) \     
...                   for f, meta in metadata_dict.items() if meta.st_size > 6000}          ②
>>> list(humansize_dict.keys())                                                             ③
['romantest9', 'romantest8', 'romantest7', 'romantest6', 'romantest10', 'pluraltest6']
>>> humansize_dict['romantest9']                                                            ④
'6.5 KiB'

为什么是“humansize.approximate_size(meta.st_size)”以及“for f, meta”中的元变量来自哪里?

4

1 回答 1

3

这与os.stat.

你似乎跳过了那章的主题,理解——这是一个字典理解。与前面对列表推导的讨论一样,dict comp 为它迭代的 dict 中的每个项目创建变量。这在该代码的脚注中进行了解释。

于 2015-07-07T19:22:38.577 回答