Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Zipping (computer science)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Function which maps a tuple of sequences into a sequence of tuples}} {{about|the data mapping function|zipping in software user programs|file compression}} In [[computer science]], '''zipping''' is a [[function (computer programming)|function]] which [[data mapping|maps]] a [[tuple]] of [[sequence]]s into a [[sequence]] of [[tuple]]s. 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 <math>\ell</math> denote the length of the longest word which is ''fish''; <math>\ell = 4</math>. The zip of ''cat'', ''fish'', ''be'' is then 4 tuples of elements: : <math> (c,f,b)(a,i,e)(t,s,\#)(\#,h,\#)</math> where ''#'' is a symbol not in the original alphabet. In [[Haskell (programming language)|Haskell]] this truncates to the shortest sequence <math>\underline{\ell}</math>, where <math>\underline{\ell} = 2</math>: <syntaxhighlight lang="haskell"> zip3 "cat" "fish" "be" -- [('c','f','b'),('a','i','e')] </syntaxhighlight> == Definition == Let Σ be an [[Alphabet (computer science)|alphabet]], # a symbol not in Σ. Let ''x''<sub>1</sub>''x''<sub>2</sub>... ''x''<sub>|''x''|</sub>, ''y''<sub>1</sub>''y''<sub>2</sub>... ''y''<sub>|''y''|</sub>, ''z''<sub>1</sub>''z''<sub>2</sub>... ''z''<sub>|''z''|</sub>, ... be ''n'' [[word (formal language theory)|words]] (i.e. finite [[sequence]]s) of elements of Σ. Let <math>\ell</math> 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 {{math|(Σ βͺ {#})}}, i.e. an element of <math>((\Sigma\cup\{\# \})^n)^*</math>: :<math> (x_1,y_1,\ldots)(x_2,y_2,\ldots)\ldots(x_\ell,y_\ell,\ldots)</math>, where for any index {{math|''i'' > {{abs|''w''}}}}, the ''w<sub>i</sub>'' 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: :<math> (x_1,y_1,\ldots)(x_2,y_2,\ldots)\ldots(x_{\underline{\ell}},y_{\underline{\ell}},\ldots)</math> where <math>\underline{\ell}</math> is the ''minimum'' length of the input words. It avoids the use of an adjoined element <math>\#</math>, but destroys information about elements of the input sequences beyond <math>\underline{\ell}</math>. == In programming languages == Zip [[Subroutine|functions]] are often available in [[programming language]]s, often referred to as {{mono|zip}}. In [[Lisp (programming language)|Lisp]]-dialects one can simply {{mono|[[Map (higher-order function)|map]]}} the desired function over the desired lists, {{mono|map}} is [[Variadic function|variadic]] in Lisp so it can take an arbitrary number of lists as argument. An example from [[Clojure]]:<ref>[http://clojuredocs.org/clojure_core/1.2.0/clojure.core/map map] from ClojureDocs</ref> <syntaxhighlight lang="clojure"> ;; `nums' contains an infinite list of numbers (0 1 2 3 ...) (def nums (range)) (def tens [10 20 30]) (def firstname "Alice") ;; To zip (0 1 2 3 ...) and [10 20 30] into a vector, invoke `map vector' on them; same with list (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' truncates to the shortest sequence; note missing \c and \e from "Alice" (map vector nums tens firstname) ; β ([0 10 \A] [1 20 \l] [2 30 \i]) (map str nums tens firstname) ; β ("010A" "120l" "230i") ;; To unzip, apply `map vector' or `map list' (apply map list (map vector nums tens firstname)) ;; β ((0 1 2) (10 20 30) (\A \l \i)) </syntaxhighlight> In [[Common Lisp]]: <syntaxhighlight lang="lisp"> (defparameter nums '(1 2 3)) (defparameter tens '(10 20 30)) (defparameter firstname "Alice") (mapcar #'list nums tens) ;; β ((1 10) (2 20) (3 30)) (mapcar #'list nums tens (coerce firstname 'list)) ;; β ((1 10 #\A) (2 20 #\l) (3 30 #\i)) β truncates on shortest list ;; Unzips (apply #'mapcar #'list (mapcar #'list nums tens (coerce firstname 'list))) ;; β ((1 2 3) (10 20 30) (#\A #\l #\i)) </syntaxhighlight> Languages such as [[Python (programming language)|Python]] provide a {{mono|zip()}} function.<ref name="pydocs">[https://docs.python.org/library/functions.html#map map(function, iterable, ...)] from section Built-in Functions from Python v2.7.2 documentation</ref> {{mono|zip()}} in conjunction with the {{mono|*}} operator unzips a list:<ref name="pydocs"/> <syntaxhighlight lang="pycon"> >>> nums = [1, 2, 3] >>> tens = [10, 20, 30] >>> firstname = 'Alice' >>> zipped = list(zip(nums, tens)) >>> zipped [(1, 10), (2, 20), (3, 30)] >>> list(zip(*zipped)) # unzip [(1, 2, 3), (10, 20, 30)] >>> zipped2 = list(zip(nums, tens, list(firstname))) >>> zipped2 # zip, truncates on shortest [(1, 10, 'A'), (2, 20, 'l'), (3, 30, 'i')] >>> list(zip(*zipped2)) # unzip [(1, 2, 3), (10, 20, 30), ('A', 'l', 'i')] </syntaxhighlight> [[Haskell (programming language)|Haskell]] has a method of zipping sequences but requires a specific function for each [[arity]] ({{mono|zip}} for two sequences, {{mono|zip3}} for three etc.),<ref>[http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#v:zip zip :: <nowiki>[a] -> [b] -> [(a, b)]</nowiki>] from Prelude, Basic libraries</ref> similarly the functions {{mono|unzip}} and {{mono|unzip3}} are available for unzipping: <syntaxhighlight lang="haskell"> -- nums contains an infinite list of numbers [1, 2, 3, ...] nums = [1..] tens = [10, 20, 30] firstname = "Alice" zip nums tens -- β [(1,10), (2,20), (3,30)] β zip, truncates infinite list unzip $ zip nums tens -- β ([1,2,3], [10,20,30]) β unzip zip3 nums tens firstname -- β [(1,10,'A'), (2,20,'l'), (3,30,'i')] β zip, truncates unzip3 $ zip3 nums tens firstname -- β ([1,2,3], [10,20,30], "Ali") β unzip </syntaxhighlight> ==Language comparison== List of languages by support of zip: {| class="wikitable" |+ Zip in various languages |- ! scope="col" | Language ! scope="col" | Zip ! scope="col" | Zip 3 lists ! scope="col" | Zip ''n'' lists ! scope="col" | Notes |- ! scope="row" | [[Chapel (programming language)|Chapel]] | {{mono|zip (''iter1'' ''iter2'')}} | {{mono|zip (''iter1'' ''iter2'' ''iter3'')}} | {{mono|zip (''iter1'' ... ''itern'')}} | The shape of each iterator, the rank and the extents in each dimension, must be identical.<ref>{{cite web |url=https://chapel-lang.org/docs/language/spec/statements.html#zipper-iteration |title = Statements β Chapel Documentation 1.25}}</ref> |- ! scope="row" | [[Clojure]] | {{mono|(map list ''list1'' ''list2'')}}<br/>{{mono|(map vector ''list1'' ''list2'')}} | {{mono|(map list ''list1'' ''list2'' ''list3'')}}<br/>{{mono|(map vector ''list1'' ''list2'' ''list3'')}} | {{mono|(map list ''list1'' β¦ ''listn'')}}<br/>{{mono|(map vector ''list1'' β¦ ''listn'')}} | Stops after the length of the shortest list. |- ! scope="row" | [[Common Lisp]] | {{codett|2=lisp|(mapcar #'list list1 list2)}} | {{codett|2=lisp|(mapcar #'list list1 list2 list3)}} | {{codett|2=lisp|(mapcar #'list list1 ... listn)}} | Stops after the length of the shortest list. |- ! scope="row" | [[D (programming language)|D]] | {{mono|zip(''range1'', ''range2'')}}<br/>{{mono|''range1''.zip(''range2'')}} | {{mono|zip(''range1'', ''range2'',''range3'')}}<br/>{{mono|''range1''.zip(range2, range3)}} | {{mono|zip(''range1'', β¦, ''rangeN'')}}<br/>{{mono|''range1''.zip(β¦, rangeN)}} | The stopping policy defaults to shortest and can be optionally provided as shortest, longest, or requiring the same length.<ref>{{cite web |url=http://dlang.org/phobos/std_range.html#zip |title = std.range - D Programming Language}}</ref> The second form is an example of [[UFCS]]. |- ! scope="row" | [[F Sharp (programming language)|F#]] | {{mono|List.zip ''list1'' ''list2''}}<br/>{{mono|Seq.zip ''source1'' ''source2''}}<br/>{{mono|Array.zip ''array1'' ''array2''}} | {{mono|List.zip3 ''list1'' ''list2'' ''list3''}}<br/>{{mono|Seq.zip3 ''source1'' ''source2'' ''source3''}}<br/>{{mono|Array.zip3 ''array1'' ''array2'' ''array3''}} | | |- ! scope="row" | [[Haskell (programming language)|Haskell]] | {{mono|zip ''list1'' ''list2''}} | {{mono|zip3 ''list1'' ''list2'' ''list3''}} | {{mono|zip''n'' ''list1'' β¦ ''listn''}} | {{mono|zipn}} for ''n'' > 3 is available in the module ''Data.List''. Stops after the shortest list ends. |- ! scope="row" | [[Python (programming language)|Python]] | {{mono|zip(''list1'', ''list2'')}} | {{mono|zip(''list1'', ''list2'', ''list3'')}} | {{mono|zip(''list1'', β¦, ''listn'')}} | {{mono|''zip()''}} and {{mono|''map()''}} (3.x) stops after the shortest list ends, whereas {{mono|''map()''}} (2.x) and {{mono|''itertools.zip_longest()''}} (3.x) extends the shorter lists with {{mono|''None''}} items |- ! scope="row" | [[Ruby (programming language)|Ruby]] | {{mono|''list1''.zip(''list2'')}} | {{mono|''list1''.zip(''list2'', ''list3'')}} | {{mono|''list1''.zip(''list1'', .., ''listn'')}} | When the list being executed upon (list1) is shorter than the lists being zipped the resulting list is the length of list1. If list1 is longer nil values are used to fill the missing values<ref>{{Cite web|url=https://docs.ruby-lang.org/en/master/Array.html#method-i-zip|title = Class: Array}}</ref> |- ! scope="row" | [[Scala (programming language)|Scala]] | {{mono|''list1''.zip(''list2'')}} | | | If one of the two collections is longer than the other, its remaining elements are ignored.<ref>{{cite web |url=https://www.scala-lang.org/api/current/scala/collection/IterableOps.html#zip[B](that:scala.collection.IterableOnce[B]):CC[(A@scala.annotation.unchecked.uncheckedVariance,B) |title=IterableOps |date= |website=scala-lang.org}}</ref> |} {| class="wikitable" |+ Unzip in various languages |- ! scope="col" | Language ! scope="col" | Unzip ! scope="col" | Unzip 3 tuples ! scope="col" | Unzip ''n'' tuples ! scope="col" | Notes |- ! scope="row" | [[Clojure]] | {{mono|(apply map vector ''ziplist'')}} | {{mono|(apply map vector ''ziplist'')}} | {{mono|(apply map vector ''ziplist'')}} | |- ! scope="row" | [[Common Lisp]] | {{codett|2=lisp|(apply #'mapcar #'list ziplist)}} | {{codett|2=lisp|(apply #'mapcar #'list ziplist)}} | {{codett|2=lisp|(apply #'mapcar #'list ziplist)}} | |- ! scope="row" | [[F Sharp (programming language)|F#]] | {{mono|List.unzip ''list1'' ''list2''}}<br/>{{mono|Seq.unzip ''source1'' ''source2''}}<br/>{{mono|Array.unzip ''array1'' ''array2''}} | {{mono|List.unzip3 ''list1'' ''list2'' ''list3''}}<br/>{{mono|Seq.unzip3 ''source1'' ''source2'' ''source3''}}<br/>{{mono|Array.unzip3 ''array1'' ''array2'' ''array3''}} | | |- ! scope="row" | [[Haskell (programming language)|Haskell]] | {{mono|unzip ''ziplist''}} | {{mono|unzip3 ''ziplist''}} | {{mono|unzip''n'' ''ziplist''}} | {{mono|unzipn}} for ''n'' > 3 is available in the module {{mono|''Data.List''.}} |- ! scope="row" | [[Python (programming language)|Python]] | {{mono|zip(*''zipvlist'')}} | {{mono|zip(*''zipvlist'')}} | {{mono|zip(*''zipvlist'')}} | |} == See also == {{Portal|Computer programming}} * [[Map (higher-order function)]] ==References== <references/> [[Category:Articles with example Haskell code]] [[Category:Articles with example Lisp (programming language) code]] [[Category:Articles with example Clojure code]] [[Category:Articles with example Python (programming language) code]] [[Category:Data mapping]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:About
(
edit
)
Template:Cite web
(
edit
)
Template:Codett
(
edit
)
Template:Math
(
edit
)
Template:Mono
(
edit
)
Template:Portal
(
edit
)
Template:Short description
(
edit
)