473,473 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

opposite of zip()?

Given a bunch of arrays, if I want to create tuples, there is
zip(arrays). What if I want to do the opposite: break a tuple up and
append the values to given arrays:
map(append, arrays, tupl)
except there is no unbound append() (List.append() does not exist,
right?).

Without append(), I am forced to write a (slow) explicit loop:
for (a, v) in zip(arrays, tupl):
a.append(v)

I assume using an index variable instead wouldn't be much faster.

Is there a better solution?

Thanks,
igor
Dec 15 '07 #1
11 9014
On Dec 15, 5:47 am, igor.tatari...@gmail.com wrote:
Given a bunch of arrays, if I want to create tuples, there is
zip(arrays). What if I want to do the opposite: break a tuple up and
append the values to given arrays:
map(append, arrays, tupl)
except there is no unbound append() (List.append() does not exist,
right?).

Without append(), I am forced to write a (slow) explicit loop:
for (a, v) in zip(arrays, tupl):
a.append(v)

I assume using an index variable instead wouldn't be much faster.

Is there a better solution?

Thanks,
igor
I can't quite get what you require from your explanation. Do you have
sample input & output?

Maybe this:
http://paddy3118.blogspot.com/2007/0...in-python.html
Will help.

- Paddy.
Dec 15 '07 #2
ig************@gmail.com wrote:
Given a bunch of arrays, if I want to create tuples, there is
zip(arrays). What if I want to do the opposite: break a tuple up and
append the values to given arrays:
map(append, arrays, tupl)
except there is no unbound append() (List.append() does not exist,
right?).

Without append(), I am forced to write a (slow) explicit loop:
for (a, v) in zip(arrays, tupl):
a.append(v)

I assume using an index variable instead wouldn't be much faster.

Is there a better solution?

Thanks,
igor

But it *does* exist, and its named list.append, and it works as you wanted.
>>list.append
<method 'append' of 'list' objects>
>>a = [[],[]]
map(list.append, a, (1,2))
[None, None]
>>a
[[1], [2]]
>>map(list.append, a, (3,4))
[None, None]
>>a
[[1, 3], [2, 4]]
>>map(list.append, a, (30,40))
[None, None]
>>a
[[1, 3, 30], [2, 4, 40]]
Gary Herron
Dec 15 '07 #3
On Fri, 14 Dec 2007 21:47:06 -0800, igor.tatarinov wrote:
Given a bunch of arrays, if I want to create tuples, there is
zip(arrays). What if I want to do the opposite: break a tuple up and
append the values to given arrays:
map(append, arrays, tupl)
except there is no unbound append() (List.append() does not exist,
right?).

Don't guess, test.
>>list.append # Does this exist?
<method 'append' of 'list' objects>
Apparently it does. Here's how *not* to use it to do what you want:
>>arrays = [[1, 2, 3, 4], [101, 102, 103, 104]]
tupl = tuple("ab")
map(lambda alist, x: alist.append(x), arrays, tupl)
[None, None]
>>arrays
[[1, 2, 3, 4, 'a'], [101, 102, 103, 104, 'b']]

It works, but is confusing and hard to understand, and the lambda
probably makes it slow. Don't do it that way.
Without append(), I am forced to write a (slow) explicit loop:
for (a, v) in zip(arrays, tupl):
a.append(v)
Are you sure it's slow? Compared to what?
For the record, here's the explicit loop:
>>arrays = [[1, 2, 3, 4], [101, 102, 103, 104]]
tupl = tuple("ab")
zip(arrays, tupl)
[([1, 2, 3, 4], 'a'), ([101, 102, 103, 104], 'b')]
>>for (a, v) in zip(arrays, tupl):
.... a.append(v)
....
>>arrays
[[1, 2, 3, 4, 'a'], [101, 102, 103, 104, 'b']]
I think you're making it too complicated. Why use zip()?

>>arrays = [[1, 2, 3, 4], [101, 102, 103, 104]]
tupl = tuple("ab")
for i, alist in enumerate(arrays):
.... alist.append(tupl[i])
....
>>arrays
[[1, 2, 3, 4, 'a'], [101, 102, 103, 104, 'b']]


--
Steven
Dec 15 '07 #4
On Sat, 15 Dec 2007 06:46:44 +0000, Steven D'Aprano wrote:
Here's how *not* to use it to do what you want:
>>>arrays = [[1, 2, 3, 4], [101, 102, 103, 104]] tupl = tuple("ab")
map(lambda alist, x: alist.append(x), arrays, tupl)
[None, None]
>>>arrays
[[1, 2, 3, 4, 'a'], [101, 102, 103, 104, 'b']]

It works, but is confusing and hard to understand, and the lambda
probably makes it slow. Don't do it that way.
As Gary Herron points out, you don't need to use lambda:

map(list.append, arrays, tupl)

will work. I still maintain that this is the wrong way to to it: taking
the lambda out makes the map() based solution marginally faster than the
explicit loop, but I don't believe that the gain in speed is worth the
loss in readability.

(e.g. on my PC, for an array of 900000 sub-lists, the map() version takes
0.4 second versus 0.5 second for the explicit loop. For smaller arrays,
the results are similar.)

--
Steven.
Dec 15 '07 #5
Hi folks,

Thanks, for all the help. I tried running the various options, and
here is what I found:
from array import array
from time import time

def f1(recs, cols):
for r in recs:
for i,v in enumerate(r):
cols[i].append(v)

def f2(recs, cols):
for r in recs:
for v,c in zip(r, cols):
c.append(v)

def f3(recs, cols):
for r in recs:
map(list.append, cols, r)

def f4(recs):
return zip(*recs)

records = [ tuple(range(10)) for i in xrange(1000000) ]

columns = tuple([] for i in xrange(10))
t = time()
f1(records, columns)
print 'f1: ', time()-t

columns = tuple([] for i in xrange(10))
t = time()
f2(records, columns)
print 'f2: ', time()-t

columns = tuple([] for i in xrange(10))
t = time()
f3(records, columns)
print 'f3: ', time()-t

t = time()
columns = f4(records)
print 'f4: ', time()-t

f1: 5.10132408142
f2: 5.06787180901
f3: 4.04700708389
f4: 19.13633203506

So there is some benefit in using map(list.append). f4 is very clever
and cool but it doesn't seem to scale.

Incidentally, it took me a while to figure out why the following
initialization doesn't work:
columns = ([],)*10
apparently you end up with 10 copies of the same list.

Finally, in my case the output columns are integer arrays (to save
memory). I can still use array.append but it's a little slower so the
difference between f1-f3 gets even smaller. f4 is not an option with
arrays.
Dec 15 '07 #6
ig************@gmail.com wrote:
Hi folks,

Thanks, for all the help. I tried running the various options, and
here is what I found:
from array import array
from time import time

def f1(recs, cols):
for r in recs:
for i,v in enumerate(r):
cols[i].append(v)

def f2(recs, cols):
for r in recs:
for v,c in zip(r, cols):
c.append(v)

def f3(recs, cols):
for r in recs:
map(list.append, cols, r)

def f4(recs):
return zip(*recs)

records = [ tuple(range(10)) for i in xrange(1000000) ]

columns = tuple([] for i in xrange(10))
t = time()
f1(records, columns)
print 'f1: ', time()-t

columns = tuple([] for i in xrange(10))
t = time()
f2(records, columns)
print 'f2: ', time()-t

columns = tuple([] for i in xrange(10))
t = time()
f3(records, columns)
print 'f3: ', time()-t

t = time()
columns = f4(records)
print 'f4: ', time()-t

f1: 5.10132408142
f2: 5.06787180901
f3: 4.04700708389
f4: 19.13633203506

So there is some benefit in using map(list.append). f4 is very clever
and cool but it doesn't seem to scale.

Incidentally, it took me a while to figure out why the following
initialization doesn't work:
columns = ([],)*10
apparently you end up with 10 copies of the same list.
Yes. A well known gotcha in Python and a FAQ.
Finally, in my case the output columns are integer arrays (to save
memory). I can still use array.append but it's a little slower so the
difference between f1-f3 gets even smaller. f4 is not an option with
arrays.
Dec 15 '07 #7
On Dec 15, 4:45 am, Gary Herron <gher...@islandtraining.comwrote:
igor.tatari...@gmail.com wrote:
Hi folks,
Thanks, for all the help. I tried running the various options, and
here is what I found:
from array import array
from time import time
def f1(recs, cols):
for r in recs:
for i,v in enumerate(r):
cols[i].append(v)
def f2(recs, cols):
for r in recs:
for v,c in zip(r, cols):
c.append(v)
def f3(recs, cols):
for r in recs:
map(list.append, cols, r)
def f4(recs):
return zip(*recs)
records = [ tuple(range(10)) for i in xrange(1000000) ]
columns = tuple([] for i in xrange(10))
t = time()
f1(records, columns)
print 'f1: ', time()-t
columns = tuple([] for i in xrange(10))
t = time()
f2(records, columns)
print 'f2: ', time()-t
columns = tuple([] for i in xrange(10))
t = time()
f3(records, columns)
print 'f3: ', time()-t
t = time()
columns = f4(records)
print 'f4: ', time()-t
f1: 5.10132408142
f2: 5.06787180901
f3: 4.04700708389
f4: 19.13633203506
So there is some benefit in using map(list.append). f4 is very clever
and cool but it doesn't seem to scale.
Incidentally, it took me a while to figure out why the following
initialization doesn't work:
columns = ([],)*10
apparently you end up with 10 copies of the same list.

Yes. A well known gotcha in Python and a FAQ.
Finally, in my case the output columns are integer arrays (to save
memory). I can still use array.append but it's a little slower so the
difference between f1-f3 gets even smaller. f4 is not an option with
arrays.
If you want another answer. The opposite of zip(lists) is zip(*
list_of_tuples)

That is:
lists == zip(zip(* lists))

I don't know about its speed though compared to the other suggestions.

Matt
Dec 15 '07 #8
ig************@gmail.com wrote:
map(append, arrays, tupl)
except there is no unbound append() (List.append() does not exist,
right?).
Er, no, but list.append does:
>>list.append
<method 'append' of 'list' objects>

so you should be able to do

map(list.append, arrays, tupl)

provided you know that all the elements of 'arrays' are
actual lists.

--
Greg
Dec 15 '07 #9
ig************@gmail.com wrote:
Given a bunch of arrays, if I want to create tuples, there is
zip(arrays). What if I want to do the opposite: break a tuple up and
append the values to given arrays:
map(append, arrays, tupl)
except there is no unbound append() (List.append() does not exist,
right?).
list.append does exist (try the lower-case flavor).
Without append(), I am forced to write a (slow) explicit loop:
for (a, v) in zip(arrays, tupl):
a.append(v)
Except that isn't technically the opposite of zip. The opposite would
be a tuple of single-dimensional tuples:

def unzip(zipped):
"""
Given a sequence of size-sized sequences, produce a tuple of tuples
that represent each index within the zipped object.

Example:
>>zipped = zip((1, 2, 3), (4, 5, 6))
zipped
[(1, 4), (2, 5), (3, 6)]
>>unzip(zipped)
((1, 2, 3), (4, 5, 6))
"""
if len(zipped) < 1:
raise ValueError, 'At least one item is required for unzip.'
indices = range(len(zipped[0]))
return tuple(tuple(pair[index] for pair in zipped)
for index in indices)

This is probably not the most efficient hunk of code for this but this
would seem to be the correct behavior for the opposite of zip and it
should scale well.

Modifying the above with list.extend would produce a variant closer to
what I think you're asking for:

def unzip_extend(dests, zipped):
"""
Appends the unzip versions of zipped into dests. This avoids an
unnecessary allocation.

Example:
>>zipped = zip((1, 2, 3), (4, 5, 6))
zipped
[(1, 4), (2, 5), (3, 6)]
>>dests = [[], []]
unzip_extend(dests, zipped)
dests
[[1, 2, 3], [4, 5, 6]]
"""
if len(zipped) < 1:
raise ValueError, 'At least one item is required for unzip.'
for index in range(len(zipped[0])):
dests[index].extend(pair[index] for pair in zipped)

This should perform pretty well, as extend with a comprehension is
pretty fast. Not that it's truly meaningful, here's timeit on my 2GHz
laptop:

bash-3.1$ python -m timeit -s 'import unzip; zipped=zip(range(1024),
range(1024))' 'unzip.unzip_extend([[], []], zipped)'
1000 loops, best of 3: 510 usec per loop

By comparison, here's the unzip() version above:

bash-3.1$ python -m timeit -s 'import unzip; zipped=zip(range(1024),
range(1024))' 'unzip.unzip(zipped)'
1000 loops, best of 3: 504 usec per loop

Rich

Dec 17 '07 #10
Rich Harkins wrote:
ig************@gmail.com wrote:
>Given a bunch of arrays, if I want to create tuples, there is
zip(arrays). What if I want to do the opposite: break a tuple up and
append the values to given arrays:
map(append, arrays, tupl)
except there is no unbound append() (List.append() does not exist,
right?).

list.append does exist (try the lower-case flavor).
>Without append(), I am forced to write a (slow) explicit loop:
for (a, v) in zip(arrays, tupl):
a.append(v)

Except that isn't technically the opposite of zip. The opposite would
be a tuple of single-dimensional tuples:

def unzip(zipped):
"""
Given a sequence of size-sized sequences, produce a tuple of tuples
that represent each index within the zipped object.

Example:
>>zipped = zip((1, 2, 3), (4, 5, 6))
>>zipped
[(1, 4), (2, 5), (3, 6)]
>>unzip(zipped)
((1, 2, 3), (4, 5, 6))
"""
if len(zipped) < 1:
raise ValueError, 'At least one item is required for unzip.'
indices = range(len(zipped[0]))
return tuple(tuple(pair[index] for pair in zipped)
for index in indices)

This is probably not the most efficient hunk of code for this but this
would seem to be the correct behavior for the opposite of zip and it
should scale well.

Modifying the above with list.extend would produce a variant closer to
what I think you're asking for:

def unzip_extend(dests, zipped):
"""
Appends the unzip versions of zipped into dests. This avoids an
unnecessary allocation.

Example:
>>zipped = zip((1, 2, 3), (4, 5, 6))
>>zipped
[(1, 4), (2, 5), (3, 6)]
>>dests = [[], []]
>>unzip_extend(dests, zipped)
>>dests
[[1, 2, 3], [4, 5, 6]]
"""
if len(zipped) < 1:
raise ValueError, 'At least one item is required for unzip.'
for index in range(len(zipped[0])):
dests[index].extend(pair[index] for pair in zipped)

This should perform pretty well, as extend with a comprehension is
pretty fast. Not that it's truly meaningful, here's timeit on my 2GHz
laptop:

bash-3.1$ python -m timeit -s 'import unzip; zipped=zip(range(1024),
range(1024))' 'unzip.unzip_extend([[], []], zipped)'
1000 loops, best of 3: 510 usec per loop

By comparison, here's the unzip() version above:

bash-3.1$ python -m timeit -s 'import unzip; zipped=zip(range(1024),
range(1024))' 'unzip.unzip(zipped)'
1000 loops, best of 3: 504 usec per loop

Rich
As Paddy wrote, zip is its own unzip:
>>zipped = zip((1, 2, 3), (4, 5, 6))
zipped
[(1, 4), (2, 5), (3, 6)]
>>unzipped = zip(*zipped)
unzipped
[(1, 2, 3), (4, 5, 6)]

Neat and completely confusing, huh? :-)

<http://paddy3118.blogspot.com/2007/02/unzip-un-needed-in-python.html>
--
Dec 17 '07 #11
Matt Nordhoff wrote:
[snip]
>
As Paddy wrote, zip is its own unzip:
>>>zipped = zip((1, 2, 3), (4, 5, 6))
zipped
[(1, 4), (2, 5), (3, 6)]
>>>unzipped = zip(*zipped)
unzipped
[(1, 2, 3), (4, 5, 6)]

Neat and completely confusing, huh? :-)

<http://paddy3118.blogspot.com/2007/02/unzip-un-needed-in-python.html>
I hadn't thought about zip() being symmetrical like that. Very cool...

Rich
Dec 17 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ray Tomes | last post by:
Hi all Many thanks to those that answered my questions about whitespace and ord() being reverse of chr(). As well as the 2 things I asked about I learned about 5 other useful things. This I...
6
by: Tertius | last post by:
Is there a method to create a dict from a list of keys and a list of values ? TIA Tertius
6
by: Will Stuyvesant | last post by:
I am uploading a .zip file to a Python CGI program, using a form on a HTML page with <input name="yourfile" type="file">... In the Python CGI program I do: import cgi fStorage =...
2
by: Frostillicus | last post by:
I'm trying to get an ASP to return a zip file to the remote browser from an Image (BLOB) field in SQL Server 2000 but Internet Explorer keeps saying: Cannot open C:\Documents and...
2
by: Axel Foley | last post by:
I used some of the excellent resources from DITHERING.COM for help in my groveling newbie attempts to cough up working form validation.... I cut and pasted bits of code to check USA ZIP codes and...
38
by: Steven Bethard | last post by:
> >>> aList = > >>> it = iter(aList) > >>> zip(it, it) > > That behavior is currently an accident. >http://sourceforge.net/tracker/?group_id=5470&atid=105470&func=detail&aid=1121416
4
by: DataSmash | last post by:
I need to unzip all zip file(s) in the current directory into their own subdirectories. The zip file name(s) always start with the string "usa" and end with ".zip". The code below will make the...
8
by: =?Utf-8?B?Q2hyaXMgRmluaw==?= | last post by:
I am trying to make a minor modification to the code below and need some assistance. Currently this code is using the java.util, java.util.zip, and java.io assemblies from the vjslib.dll assembly....
1
by: sandhyabhavani | last post by:
This article is used to zip a file or directory using vb.net. The classes and method to zip a file is availale in java.io, java.util, java.util.zip class library.To import these you have to add a...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.