请您解释为什么这个Python代码适用于C程序员?
这个习语用于递归地检索文件列表
[os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser(dir)) for f in fn]
我在Recursive os.listdir?找到了这种风格。它没有标点符号或嵌套。我不明白为什么它有效。你能用一种我能理解的更乏味的风格来写同样的东西,这样我就不会感到无助吗? :)
人们通常会说"给我们一个例子说明你使用它的原因"并且我会告诉你整个程序。我很震惊,我在博客上写了很多文章,并附有图片http://pj.freefaculty.org/blog/?p=285
Ubuntu 14.10的XFCE4桌面和Compiz窗口管理器/合成器。它将通过gsettings与Dconf交互来替换视口上的背景墙纸。
我不明白的用法是在random.choice()里面:
#!/usr/bin/env python3
import argparse
import subprocess
import sys
import os
import random
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--dir", help = "directory path",
default = "/usr/local/share/Backgrounds")
parser.add_argument("-w", "--workspace",
help = "workspace number, 0, 1-n, or > n", default = "-1",
type = int)
parser.add_argument("-schema", help = "gsettings shema",
metavar = "SCHEMA", default = "org.compiz.wallpaper:/org/compiz/profiles/Default/plugins/wallpaper/")
parser.add_argument("-key", help = "gsettings key", metavar = "KEY", default = "bg-image")
args = parser.parse_args()
array = eval(subprocess.check_output(["gsettings", "get", args.schema, args.key]))
## print(array)
arraylen = len(array)
filename = random.choice([os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser(args.dir)) for f in fn])
print("The newly found filename is:")
print(filename)
ws = args.workspace - 1
## If ws 0 or smaller, we are going to reset whole collection back to
## just one image. if ws > N of images, then add a new image.
if ws < 0:
array=[str(filename)]
subprocess.call(["gsettings", "set", args.schema, args.key, str(array)])
subprocess.call(["gsettings", "set", args.schema, "bg-fill-type", str("[0]")])
subprocess.call(["gsettings", "set", args.schema, "bg-image-pos", str("[0]")])
subprocess.call(["gsettings", "set", args.schema, "bg-color1", str("['#000000ff']")])
subprocess.call(["gsettings", "set", args.schema, "bg-color2", str("['#000000ff']")])
elif ws < arraylen:
array[ws]=str(filename)
subprocess.call(["gsettings", "set", args.schema, args.key, str(array)])
else:
array.append(str(filename))
subprocess.call(["gsettings", "set", args.schema, args.key, str(array)])
arraylen = len(array)
subprocess.call(["gsettings", "set", args.schema, "bg-fill-type", str([0]*arraylen)])
subprocess.call(["gsettings", "set", args.schema, "bg-image-pos", str([0]*arraylen)])
subprocess.call(["gsettings", "set", args.schema, "bg-color1", str(['#000000ff']*arraylen)])
subprocess.call(["gsettings", "set", args.schema, "bg-color2", str(['#000000ff']*arraylen)])
subprocess.call(["gsettings", "set", args.schema, args.key, str(array)])
print("HELLO, corrected image array is:")
print('\n '.join(array))
如果你说&#34;我们不明白你不明白,&#34;我知道了。以下是我想问的具体问题
当人们使用if语句和一行结束时,而不是在开头时,我在Perl中得到了相同的过敏感。
答案 0 :(得分:0)
当你跑步时:
a=[[1,2],[3,4]]
[x for y in a for x in y]
你得到:
[1,2,3,4]
换句话说,它为y
中的每个条目运行a
,而不是x
作为y
的每个条目,因此它相当于:
for y in a:
for x in y:
code with x
您可以将代码重写为:
result=[]
for dp, dn, fn in os.walk(os.path.expanduser(args.dir)):
for f in fn:
result.append(os.path.join(dp, f))
重要的是,第一个始终是左侧循环,因此可以这样使用变量。
至于括号,可以在那里写,但隐含地清楚它必须是元组,所以写它不是必需的。