What is the purpose of the zip function (as in Python or C # 4.0)?

Someone asked How to make Pythons zip in C #? ...

... which makes me ask what is good in zip code? In which scenarios do I need this? Is it really so fundamental that I need it in a base class library?

+7
python c # zip
source share
7 answers

Someone really asked a question quite recently what I answered using the Zip extension method, so it is obviously important to some people.;)

In fact, this is a rather important operation for mathematical algorithms - matrices, curve fitting, interpolation, pattern recognition, such things. It is also very important in technical applications such as digital signal processing, where most of what you do combines several signals or applies linear transformations to them - both of them are based on the index of the sample, hence zip. Replacing two sequences is far, much faster than sorting and combining them based on some key, especially if you know in advance that the sequences have the same number of elements and are in the same order.

I cannot go into details in connection with my current work, but, generally speaking, it is also valuable for telemetry data - industrial, scientific, of this kind. Often you will have time sequences of data coming in from hundreds or thousands of points - parallel sources - and you need to collect, but horizontally, through devices, and not after some time. In the end, you need a different time sequence, but with the sum or average or some other aggregate of all the individual points.

This may seem like a simple sort / group / join in SQL Server (for example), but in fact it is really very difficult to do this way. Firstly, the timestamps may not match exactly, but you do not need a few millisecond differences, so you have to generate a surrogate key / line number and a group on this - and, of course, the surrogate line number is nothing more than a time index that you already had. Zipping is simple, fast and infinitely parallelizable.

I do not know if I will call it fundamental, but it is important. I donโ€™t use the Reverse method very often, but Iโ€™m glad that I donโ€™t need to write it myself on those rare occasions when I find it in need.

One reason you might find it inappropriate is that .NET / C # 3.5 does not have tuples. C # 4 has tuples , and when you work with tuples, zipping is really a fundamental operation because ordering is strictly enforced.

+8
source share

zip is useful if you want to iterate over multiple iterations at the same time, which is a fairly common script in Python.

One real scenario when zip comes in handy for me is if you have an array M by N and you want to look at columns instead of rows. For example:

 >>> five_by_two = ((0, 1), (1, 2), (2, 3), (3, 4), (4, 5)) >>> two_by_five = tuple(zip(*five_by_two)) >>> two_by_five ((0, 1, 2, 3, 4), (1, 2, 3, 4, 5)) 
+11
source share

Use case:

 >>> fields = ["id", "name", "location"] >>> values = ["13", "bill", "redmond"] >>> dict(zip(fields, values)) {'location': 'redmond', 'id': '13', 'name': 'bill'} 

Try to do it without zip ...

+9
source share

It allows you to process sequences in parallel, rather than sequentially or nested. There ... uses so much for this that they are currently avoiding me.

+7
source share

Here is a common usage example for zip:

 x = [1,2,3,4,5] y = [6,7,8,9,0] for a,b in zip(x,y): print a, b 

What will be output:

 1 6 2 7 3 8 4 9 5 0 
+7
source share

It is convenient in different places. My favorite, from http://norvig.com/python-iaq.html , transports the matrix:

 >>> x = [ [1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]] >>> zip(*x) [(1, 6, 11), (2, 7, 12), (3, 8, 13), (4, 9, 14), (5, 10, 15)] 
+3
source share

Here is the case where I used zip() for a useful effect, in a Python class to compare version numbers:

 class Version(object): # ... snip ... def get_tuple(self): return (self.major, self.minor, self.revision) def compare(self, other): def comp(a, b): if a == '*' or b == '*': return 0 elif a == b: return 0 elif a < b: return -1 else: return 1 return tuple(comp(a, b) for a, b in zip(self.get_tuple(), Version(other).get_tuple())) def is_compatible(self, other): tup = self.compare(other) return (tup[0] == 0 and tup[1] == 0) def __eq__(self, other): return all(x == 0 for x in self.compare(other)) def __ne__(self, other): return any(x != 0 for x in self.compare(other)) def __lt__(self, other): for x in self.compare(other): if x < 0: return True elif x > 0: return False return False def __gt__(self, other): for x in self.compare(other): if x > 0: return True elif x < 0: return False return False 

I think zip() , combined with all() and any() , makes the implementation of the comparison operator particularly clear and elegant. Of course, this could have been done without zip() , but then the same could be said of almost any language function.

+2
source share

All Articles