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 denote the length of the longest word which is fish; . The zip of cat, fish, be is then 4 tuples of elements:
where # is a symbol not in the original alphabet. In Haskell this truncates to the shortest sequence , where :
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 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 :
- ,
where for any index i > |w|, the wi is #.
The zip of x, y, z, ... is denoted zip(x, y, z, ...) or x ⋆ y ⋆ z ⋆ ...
The inverse to zip is sometimes denoted unzip.
A variation of the zip operation is defined by:
where is the minimum length of the input words. It avoids the use of an adjoined element , but destroys information about elements of the input sequences beyond .
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") — unzipComparación de idiomas
Lista de idiomas según la compatibilidad con zip:
Véase también
Referencias
- ↑ mapa de ClojureDocs
- 1 2 map(función, iterable, ...) de la sección Funciones integradas de la documentación de Python v2.7.2
- ↑ zip :: [a] -> [b] -> [(a, b)] de las bibliotecas Prelude y Basic
- ↑ "Declaraciones — Documentación de la capilla 1.25" .
- ↑ "std.range - Lenguaje de programación D" .
- ↑ "Clase: Array" .
- ↑ "IterableOps" . scala-lang.org .
- Mapeo de datos