Articulo de referencia

Zipping (computer science)

In computer science , zipping is a function which maps a tuple of sequences into a sequence of tuples . This name zip derives from the action of a zipper in that it interleaves ...

In computer science, zipping is a function which maps a tuple of sequences into a sequence of tuples. This name zip derives from the action of a zipper in that it interleaves two formerly disjoint sequences. The inverse function is unzip.

Example

Given the three words cat, fish and be where |cat| is 3, |fish| is 4 and |be| is 2. Let {\displaystyle \ell } denote the length of the longest word which is fish; =4{\displaystyle \ell =4}. The zip of cat, fish, be is then 4 tuples of elements:

(c,f,b)(a,i,e)(t,s,#)(#,h,#){\displaystyle (c,f,b)(a,i,e)(t,s,\#)(\#,h,\#)}

where # is a symbol not in the original alphabet. In Haskell this truncates to the shortest sequence _{\displaystyle {\underline {\ell }}}, where _=2{\displaystyle {\underline {\ell }}=2}:

zip3"cat""fish""be"-- [('c','f','b'),('a','i','e')]

Definition

Let Σ be an alphabet, # a symbol not in Σ.

Let x1x2... x|x|, y1y2... y|y|, z1z2... z|z|, ... be nwords (i.e. finite sequences) of elements of Σ. Let {\displaystyle \ell } denote the length of the longest word, i.e. the maximum of |x|, |y|, |z|, ... .

The zip of these words is a finite sequence of n-tuples of elements of (Σ ∪ {#}), i.e. an element of ((Σ{#})n){\displaystyle ((\Sigma \cup \{\#\})^{n})^{*}}:

(x1,y1,)(x2,y2,)(x,y,){\displaystyle (x_{1},y_{1},\ldots )(x_{2},y_{2},\ldots )\ldots (x_{\ell },y_{\ell },\ldots )},

where for any index i > |w|, the wi is #.

The zip of x, y, z, ... is denoted zip(x, y, z, ...) or xyz ⋆ ...

The inverse to zip is sometimes denoted unzip.

A variation of the zip operation is defined by:

(x1,y1,)(x2,y2,)(x_,y_,){\displaystyle (x_{1},y_{1},\ldots )(x_{2},y_{2},\ldots )\ldots (x_{\underline {\ell }},y_{\underline {\ell }},\ldots )}

where _{\displaystyle {\underline {\ell }}} is the minimum length of the input words. It avoids the use of an adjoined element #{\displaystyle \#}, but destroys information about elements of the input sequences beyond _{\displaystyle {\underline {\ell }}}.

En lenguajes de programación

Las funciones zip suelen estar disponibles en los lenguajes de programación , a menudo denominadas zip . En los dialectos de Lisp, se puede simplemente aplicar la función deseada a las listas deseadas; map es variádica en Lisp, por lo que puede aceptar un número arbitrario de listas como argumento. Un ejemplo de Clojure : [ 1 ]

;; `nums' contiene una lista infinita de números (0 1 2 3 ...) ( def nums ( rango )) ( def tens [ 10 20 30 ]) ( def firstname "Alice" )Para combinar (0 1 2 3 ...) y [10 20 30] en un vector, invoque `map vector` sobre ellos; lo mismo con la lista ( map vector nums tens ) ; ⇒ ([0 10] [1 20] [2 30]) ( map list nums tens ) ; ⇒ ((0 10) (1 20) (2 30)) ( map str nums tens ) ; ⇒ ("010" "120" "230");; `map' trunca a la secuencia más corta; observe que faltan \c y \e en "Alice" ( map vector nums tens firstname ) ; ⇒ ([0 10 \A] [1 20 \l] [2 30 \i]) ( map str nums tens firstname ) ; ⇒ ("010A" "120l" "230i");; Para descomprimir, aplique `map vector' o `map list' ( apply map list ( map vector nums tens firstname )) ;; ⇒ ((0 1 2) (10 20 30) (\A \l \i))

En Common Lisp :

( defparameter nums ' ( 1 2 3 )) ( defparameter tens ' ( 10 20 30 )) ( defparameter firstname "Alice" )( lista de números de mapa de coches ) ;; ⇒ ((1 10) (2 20) (3 30))( lista mapcar #' nums decenas ( coerce firstname 'list )) ;; ⇒ ((1 10 #\A) (2 20 #\l) (3 30 #\i)) — trunca en la lista más corta;; Descomprime ( aplicar #' mapcar #' lista ( mapcar #' lista nums decenas ( coercer firstname 'lista ))) ;; ⇒ ((1 2 3) (10 20 30) (#\A #\l #\i))

Lenguajes como Python proporcionan una función zip() . [ 2 ] zip() junto con el operador * descomprime una lista: [ 2 ]

>>> nums = [ 1 , 2 , 3 ] >>> tens = [ 10 , 20 , 30 ] >>> firstname = 'Alice'>>> zipped = list ( zip ( nums , tens )) >>> zipped [(1, 10), (2, 20), (3, 30)]>>> lista ( zip ( * comprimido )) # descomprimir [(1, 2, 3), (10, 20, 30)]>>> zipped2 = list ( zip ( nums , tens , list ( firstname ))) >>> zipped2 # zip, trunca en el más corto [(1, 10, 'A'), (2, 20, 'l'), (3, 30, 'i')] >>> list ( zip ( * zipped2 )) # unzip [(1, 2, 3), (10, 20, 30), ('A', 'l', 'i')]

Haskell tiene un método para comprimir secuencias, pero requiere una función específica para cada aridad ( zip para dos secuencias, zip3 para tres, etc.), [ 3 ] de manera similar, las funciones unzip y unzip3 están disponibles para descomprimir:

-- nums contiene una lista infinita de números [1, 2, 3, ...] nums = [ 1 .. ] tens = [ 10 , 20 , 30 ] firstname = "Alice"zip nums tens -- ⇒ [(1,10), (2,20), (3,30)] — zip, trunca una lista infinita unzip $ zip nums tens -- ⇒ ([1,2,3], [10,20,30]) — unzipzip3 nums tens firstname -- ⇒ [(1,10,'A'), (2,20,'l'), (3,30,'i')] — zip, trunca unzip3 $ zip3 nums tens firstname -- ⇒ ([1,2,3], [10,20,30], "Ali") — unzip

Comparación de idiomas

Lista de idiomas según la compatibilidad con zip:

Véase también

Referencias

  1. mapa de ClojureDocs
  2. 1 2 map(función, iterable, ...) de la sección Funciones integradas de la documentación de Python v2.7.2
  3. zip  :: [a] -> [b] -> [(a, b)] de las bibliotecas Prelude y Basic
  4. "Declaraciones — Documentación de la capilla 1.25" .
  5. "std.range - Lenguaje de programación D" .
  6. "Clase: Array" .
  7. "IterableOps" . scala-lang.org .