如何在Julia中使用zip(*list)解包机制进行迭代?

alv*_*vas 3 python zip list julia

在Python中,我可以同时循环遍历2个列表来提取ngrams:

>>> s = 'the lazy fox jumps over the brown dog'
>>> list(zip(*[s[i:] for i in range(2)]))
[('t', 'h'), ('h', 'e'), ('e', ' '), (' ', 'l'), ('l', 'a'), ('a', 'z'), ('z', 'y'), ('y', ' '), (' ', 'f'), ('f', 'o'), ('o', 'x'), ('x', ' '), (' ', 'j'), ('j', 'u'), ('u', 'm'), ('m', 'p'), ('p', 's'), ('s', ' '), (' ', 'o'), ('o', 'v'), ('v', 'e'), ('e', 'r'), ('r', ' '), (' ', 't'), ('t', 'h'), ('h', 'e'), ('e', ' '), (' ', 'b'), ('b', 'r'), ('r', 'o'), ('o', 'w'), ('w', 'n'), ('n', ' '), (' ', 'd'), ('d', 'o'), ('o', 'g')]
>>> list(map(''.join, zip(*[s[i:] for i in range(2)])))
['th', 'he', 'e ', ' l', 'la', 'az', 'zy', 'y ', ' f', 'fo', 'ox', 'x ', ' j', 'ju', 'um', 'mp', 'ps', 's ', ' o', 'ov', 've', 'er', 'r ', ' t', 'th', 'he', 'e ', ' b', 'br', 'ro', 'ow', 'wn', 'n ', ' d', 'do', 'og']
Run Code Online (Sandbox Code Playgroud)

在朱莉娅,我可以做类似的步骤,但我不知道如何把它们拼凑起来就像我用Python做的那样.

首先,我可以生成需要同时迭代的2个列表,因此[s[i:] for i in range(2)]在Julia中代替Python :

> s = "abc def ghi lmnopq"
> [s[i:end] for i in range(1,2)]
2-element Array{String,1}:
 "abc def ghi lmnopq"
 "bc def ghi lmnopq" 
Run Code Online (Sandbox Code Playgroud)

要同时迭代它们,我可以这样做:

> c1, c2 = [line[i:end] for i in range(1,2)]
> [i for i in zip(c1, c2)]
> [join(i) for i in zip(c1, c2)]
Run Code Online (Sandbox Code Playgroud)

是否有像zip(*list)朱莉娅那样的拆包机制?如果是这样,如何避免在Julia的最后一个列表理解之前创建c1,c2?

Mat*_* B. 5

等效操作在Julia中称为splatting,并由尾部省略号表示.这是Python代码的粗略音译:

julia> s = "the lazy fox jumps over the brown dog";
julia> collect(zip([s[i:end] for i in 1:2]...))
36-element Array{Tuple{Char,Char},1}:
 ('t', 'h')
 ('h', 'e')
 ('e', ' ')
 (' ', 'l')
 ('l', 'a')
 ('a', 'z')
 ?

julia> map(join, zip([s[i:end] for i in 1:2]...))
36-element Array{String,1}:
 "th"
 "he"
 "e "
 " l"
 "la"
 "az"
 ?
Run Code Online (Sandbox Code Playgroud)

请注意,该range功能没有按照您的想法执行...并且很少使用.范围通常使用start:stop冒号语法构造.

您还需要小心,因为Julia的字符串由字节偏移量索引,因此使用1:2将断开unicode字符串.相反,我只是准确地写出zip参数并drop用来跳过第一个字符:

collect(zip(s, drop(s, 1)))
map(join, zip(s, drop(s, 1)))
Run Code Online (Sandbox Code Playgroud)