循环 - >列表/生成器理解 [英] loops -> list/generator comprehensions
问题描述
我写了这段小代码来获取当前目录中或下面所有文件的相对路径列表(* NIX):
walkList = [(x [0],x [2])for os inwalk("。")]
filenames = []
for dir,walkList中的文件:
filenames.extend(文件中f的[" /" .join([dir,f])]
它工作正常,我不需要改变它,但我知道有一个
班轮列表/生成器理解来做到这一点 - 我只是不太好
足够精通理解来弄明白。有人可以吗
告诉我它是什么?
更好的是,是否存在将简单循环转换为
的通用方法有人可以指点我的理解吗?
james
I wrote this little piece of code to get a list of relative paths of
all files in or below the current directory (*NIX):
walkList = [(x[0], x[2]) for x in os.walk(".")]
filenames = []
for dir, files in walkList:
filenames.extend(["/".join([dir, f]) for f in files])
It works fine, I don''t need to change it, but I know there is a one
liner list/generator comprehension to do this - I''m just not well
enough versed in comprehensions to figure it out. Can someone please
show me what it is?
Even better, is there a generalized way to transform simple loops into
comprehensions that someone can point me to?
james
推荐答案
JA **************** @ gmail.com 一>写道:
我写了这段小代码来获取当前目录中或下面所有文件的相对路径列表(* NIX):
> walkList = [(x [0],x [2])for os.walk中的x(。)
filenames = []
for dir,walkList中的文件:
filenames.extend([" /" .join([dir,f])for f in files])
它工作正常,我不需要改变它,但是我知道有一个衬里列表/生成器理解来做到这一点 - 我只是不太好
足够精通理解来弄明白。有人可以请给我看看它是什么?
我使用了os.path.join而不是/.join,因为它更通用,但
除了这应该是等价的:
filenames = [os.path.join(dirpath,filename)
for dirpath,_,os.walk中的文件名(''''')
文件名中的文件名]
更好的是,是否有一种将简单循环转换为
理解的通用方法有人可以指点我吗?
I wrote this little piece of code to get a list of relative paths of
all files in or below the current directory (*NIX):
walkList = [(x[0], x[2]) for x in os.walk(".")]
filenames = []
for dir, files in walkList:
filenames.extend(["/".join([dir, f]) for f in files])
It works fine, I don''t need to change it, but I know there is a one
liner list/generator comprehension to do this - I''m just not well
enough versed in comprehensions to figure it out. Can someone please
show me what it is?
I''ve used os.path.join instead of "/".join since it''s more general, but
other than that, this should be eqivalent:
filenames = [os.path.join(dirpath, filename)
for dirpath, _, filenames in os.walk(''.'')
for filename in filenames]
Even better, is there a generalized way to transform simple loops into
comprehensions that someone can point me to?
嗯,一般来说,你需要编写循环来使用追加,然后
翻译到LC更简单。
filenames = []
for dirpath,_,os.walk中的文件名(''。''):
文件名中的文件名:
filenames.append(os.path.join(dirpath,filename))
现在你知道了什么它看起来像是一个附加物,你只需将
表达式移到顶部附近,然后将fors保持为相同的顺序:
filenames = [os.path.join(dirpath,filename)
for dirpath,_,os.walk中的文件名(''。'')
$ b文件名中的文件名为$ b]
HTH,
STeVe
Well, generally, you need to write your loops to use an append, and then
the translation to LC is simpler.
filenames = []
for dirpath, _, filenames in os.walk(''.''):
for filename in filenames:
filenames.append(os.path.join(dirpath, filename))
Now that you know what it looks like with an append, you simply move the
expression in the append to the top, and leave the fors in the same order:
filenames = [os.path.join(dirpath, filename)
for dirpath, _, filenames in os.walk(''.'')
for filename in filenames]
HTH,
STeVe
I有兴趣看到一个更简洁的代码示例,但是* b $ b仍然是* clear *就像你已经拥有的那样。我实际上可以阅读,并且b $ b了解你到底有什么。那很酷:)
2005年2月6日11:28:37 -0800,< ja **************** @ gmail.com> ;写道:
I would be interested to see an example of code that is more concise but
yet as *clear* as the one you already have. I can actually read and
understand what you''ve got there. That''s cool :)
On 6 Feb 2005 11:28:37 -0800, <ja****************@gmail.com> wrote:
我写了这段小代码来获取当前目录中或下面所有文件的相对路径列表(* NIX):
walkList = [(x [0],x [2])for os.walk中的x(。)
filenames = []
for dir,walkList中的文件:
filenames.extend(文件中f的[" /.join([dir,f])])
它工作正常,我不需要改变它,但我知道有一个衬里列表/生成器理解这样做 - 我只是不太好
足够精通理解来弄明白。有人可以请你告诉我它是什么吗?
更好的是,有一种通用的方法可以将简单的循环转换为有人可以指向我的理解吗?
james
I wrote this little piece of code to get a list of relative paths of
all files in or below the current directory (*NIX):
walkList = [(x[0], x[2]) for x in os.walk(".")]
filenames = []
for dir, files in walkList:
filenames.extend(["/".join([dir, f]) for f in files])
It works fine, I don''t need to change it, but I know there is a one
liner list/generator comprehension to do this - I''m just not well
enough versed in comprehensions to figure it out. Can someone please
show me what it is?
Even better, is there a generalized way to transform simple loops into
comprehensions that someone can point me to?
james
> HTH,
确实如此。谢谢。
> HTH,
It does. Thanks.
这篇关于循环 - >列表/生成器理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!