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 11 9052
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. 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.appe nd, a, (1,2))
[None, None]
>>a
[[1], [2]]
>>map(list.appe nd, a, (3,4))
[None, None]
>>a
[[1, 3], [2, 4]]
>>map(list.appe nd, a, (30,40))
[None, None]
>>a
[[1, 3, 30], [2, 4, 40]]
Gary Herron
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(array s):
.... alist.append(tu pl[i])
....
>>arrays
[[1, 2, 3, 4, 'a'], [101, 102, 103, 104, 'b']]
--
Steven
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.
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. 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.
On Dec 15, 4:45 am, Gary Herron <gher...@island training.comwro te:
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 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 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(zippe d)
((1, 2, 3), (4, 5, 6))
"""
if len(zipped) < 1:
raise ValueError, 'At least one item is required for unzip.'
indices = range(len(zippe d[0]))
return tuple(tuple(pai r[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(de sts, 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(zippe d[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(rang e(1024),
range(1024))' 'unzip.unzip_ex tend([[], []], 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(rang e(1024),
range(1024))' 'unzip.unzip(zi pped)'
1000 loops, best of 3: 504 usec per loop
Rich This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 am trying to flip an array around so that the "subscripts" happen
in the opposite order and reading the docs I thought that zip() did this.
So I tried it like this:
|
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
|
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 = cgi.FieldStorage()
zipdata = fStorage.value
|
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 Settings\Frostillicus\Local Settings\Temporary
Internet Files\Content.IE5\U7GXENGF\file.zip
The URL to open the zip file is like this: doc_view.asp?id=1&ver=2
....where id and ver represent the zip file's version in the database. The
code I've pieced...
|
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 CANADIAN POSTAL
codes, and merged them into one function that I called validCode. The
<form> tag has an onSubmit call to a general form-checker that works fine to
make sure all fields are filled. But within the form is a ZIP/POSTAL CODE
field,...
| |
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
|
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 subdirectory, move the zip
file into the subdirectory, but unzips the contents into the
root (current) directory. I want the contents of the zip file
unloaded into the newly created subdirectory where the zip file is.
Any ideas?
Thanks.
|
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. This code works fine by
taking file(s) from
disk and creating a zip file to disk. I need to modify this code to read a
file from a byte array and then output the zip file to a byte array (all done
in memory instead of disk). The reason for...
|
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 reference vsjlib Library in .net component.
.NET Classes used :
Imports System.IO
Imports java.io
Imports java.util
Imports java.util.zip
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |