How to iterate with unpacking zip (* list) in Julia?

In Python, I can scroll through two lists at the same time to extract ngrams as such:

>>> 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',  ', ' o', 'ov', 've', 'er', 'r ', ' t', 'th', 'he', 'e ', ' b', 'br', 'ro', 'ow', 'wn', 'n ', ' d', 'do', 'og']

In Julia, I could take similar steps, but I'm not sure how to put them together, as I did with Python.

At first, I could generate 2 lists that should be iterated at the same time, so instead of Python [s[i:] for i in range(2)], in Julia:

> 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" 

And to repeat them at the same time, I could do this:

> 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)]

Does Julia have an unpacking mechanism, for example zip(*list)? If yes, then how to avoid creating c1, c2 until the last understanding of the list in Julia?

+3
source share
1 answer

splatting Julia ​​ . 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"

, range , , . start:stop.

, Julia , 1:2 unicode. zip drop, :

collect(zip(s, drop(s, 1)))
map(join, zip(s, drop(s, 1)))
+5

Source: https://habr.com/ru/post/1668995/


All Articles