我有这样的文件结构:
root
|_ fruits
|___ apple
|______images
|________ apple001.jpg
|________ apple002.jpg
|_ animals
|___ cat
|______images
|________ cat001.jpg
|________ cat002.jpg
Run Code Online (Sandbox Code Playgroud)
我想,使用Javascript和Node.js,监听这个根目录和所有子目录,并创建一个镜像这个目录结构的JSON,每个节点包含类型,名称,路径和子节点:
data = [
{
type: "folder",
name: "animals",
path: "/animals",
children: [
{
type: "folder",
name: "cat",
path: "/animals/cat",
children: [
{
type: "folder",
name: "images",
path: "/animals/cat/images",
children: [
{
type: "file",
name: "cat001.jpg",
path: "/animals/cat/images/cat001.jpg"
}, {
type: "file",
name: "cat001.jpg",
path: "/animals/cat/images/cat002.jpg"
}
]
}
]
}
]
}
];
Run Code Online (Sandbox Code Playgroud)
这是一个coffeescript JSON:
data =
[
type: …
Run Code Online (Sandbox Code Playgroud) 这对我来说真的很奇怪,因为默认情况下我认为解包会给出元组。
就我而言,我想使用prefix
键进行缓存,因此首选元组。
# The r.h.s is a tuple, equivalent to (True, True, 100)
*prefix, seed = ml_logger.get_parameters("Args.attn", "Args.memory_gate", "Args.seed")
assert type(prefix) is list
Run Code Online (Sandbox Code Playgroud)
但我认为解包会返回一个元组。
以下是相关的 PEP:https ://www.python.org/dev/peps/pep-3132/
- 更新 -
鉴于下面的评论和答案,特别是我期望解包给出一个元组,因为在函数参数中,展开参数始终是一个元组而不是一个列表。
正如 Jason 指出的那样,在解包过程中,人们无法提前知道结果的长度,因此在实现方面,包罗万象的内容必须作为动态附加的列表开始。大多数情况下,将其转换为列表是浪费精力。
从语义上讲,我更喜欢有一个元组以保持一致性。
无论我尝试什么,我似乎无法通过我的大脑实现这一点,但必须有一种方法来映射生成器功能.
例如,如果我想将列表[1,2,3,4,5,6,7,8,9,10]中的每个成员与列表[1,2,3]中的相应成员相乘,那么之后[1,2,3]列表用完它会重启:
[1 2 3 4 5 6 7 8 9 10]
x [1 2 3 1 2 3 1 2 3 1 ]
------------------------------------
[1 4 9 4 10 18 7 16 27 10]
Run Code Online (Sandbox Code Playgroud)
如果您不知道[1,2,3,4,5,6,7,8,9,10]列表的长度,您会怎么做?
python ×2
d3.js ×1
dictionary ×1
filesystems ×1
generator ×1
javascript ×1
list ×1
node.js ×1
pep ×1