470,591 Members | 2,174 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,591 developers. It's quick & easy.

A Faster Way...

Hello NG,

it is probably a beginner question, but I didn't solve it without
for-loops, and I am unable to determine if there is a faster way (probably
using some built-in function) to do this task. I have to speed up a
wxPython code that uses a lot of string concatenation (and uses these
strings to build some Fancy StaticText Controls). I found a way, but I need
a little bit of help from you, NG.

If I simplify the problem, suppose I have 2 lists like:

a = range(10)
b = range(20,30)

What I would like to have, is a "union" of the 2 list in a single tuple. In
other words (Python words...):

c = (0, 20, 1, 21, 2, 22, 3, 23, 4, 24, 5, 25, .....

and so on.

Sorry if it seems an homework assignment.

Thanks to you all.

Andrea
------------------------------------------------------------------------------------------------------------------------------------------
Message for the recipient only, if received in error, please notify the
sender and read http://www.eni.it/disclaimer/
Jul 19 '05 #1
7 1151
andrea.gavana wrote:
If I simplify the problem, suppose I have 2 lists like:

a = range(10)
b = range(20,30)

What I would like to have, is a "union" of the 2 list in a single tuple. In
other words (Python words...):

c = (0, 20, 1, 21, 2, 22, 3, 23, 4, 24, 5, 25, .....


The 'yield' statement is very useful for this sort of thing as
well as the itertools module. I thought the latter had something
for this already but I don't see it. Here's an implementation
import itertools
def round_robin(*iterables): .... iterables = map(iter, iterables)
.... for element in itertools.cycle(iterables):
.... yield element.next()
.... tuple(round_robin(range(10), range(20, 30))) (0, 20, 1, 21, 2, 22, 3, 23, 4, 24, 5, 25, 6, 26, 7, 27, 8, 28, 9, 29)


Don't know about the speed though. Didn't have anything to
compare it took. You mentioned you do a lot of string concatenation
Double checking; do you know that in Python it's faster to append
the new string elements to a list and only then do a single
string concatenation of the list elements?

That is, do

terms = []
for x in data:
s = process_the_element(x)
terms.append(s)

s = "".join(data)

rather than

# this is slow if there are many string concatenations
s = ""
for x in data:
s = s + process_the_element(x)

Andrew
da***@dalkescientific.com

Jul 19 '05 #2
an***********@agip.it wrote:
If I simplify the problem, suppose I have 2 lists like:

a = range(10)
b = range(20,30)

What I would like to have, is a "union" of the 2 list in a single tuple. In
other words (Python words...):

c = (0, 20, 1, 21, 2, 22, 3, 23, 4, 24, 5, 25, .....


py> a = range(10)
py> b = range(20,30)
py> [x for tup in zip(a, b) for x in tup]
[0, 20, 1, 21, 2, 22, 3, 23, 4, 24, 5, 25, 6, 26, 7, 27, 8, 28, 9, 29]

HTH,

STeVe
Jul 19 '05 #3
an***********@agip.it wrote:
If I simplify the problem, suppose I have 2 lists like:

a = range(10)
b = range(20,30)

What I would like to have, is a "union" of the 2 list in a
single tuple. In other words (Python words...):

c = (0, 20, 1, 21, 2, 22, 3, 23, 4, 24, 5, 25, .....


If the order is unimportant you could use:

#v+
tuple(set(range(10)) | set(range(20,30))) (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29)


#v-

Cheers,

--
Klaus Alexander Seistrup
Magnetic Ink, Copenhagen, Denmark
http://magnetic-ink.dk/
Jul 19 '05 #4
hmm, there's lots of ways, huh? you can use itertools.zip instead of
builtin zip, or do:

map(None, list1, list2)

, which will pad the shorter one to match the longer one.

Jul 19 '05 #5
For efficient string concatenation in python look at:
http://www.skymind.com/~ocrow/python_string

Jul 19 '05 #6
On Tue, 10 May 2005 18:11:27 -0700, gene.tani wrote:
hmm, there's lots of ways, huh? you can use itertools.zip instead of
builtin zip, or do:

map(None, list1, list2)

Not!
One should try a possible solution first,
l1 = range(10)
l2 = range(10,20)
l1 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] l2 [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] map(None,l1,l2) [(0, 10), (1, 11), (2, 12), (3, 13), (4, 14), (5, 15), (6, 16), (7, 17), (8, 18), (9, 19)]


Stas

Jul 19 '05 #7
"stasz" wrote:
hmm, there's lots of ways, huh? you can use itertools.zip instead of
builtin zip, or do:

map(None, list1, list2)
Not!


huh?
One should try a possible solution first,
l1 = range(10)
l2 = range(10,20)
l1 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] l2 [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] map(None,l1,l2) [(0, 10), (1, 11), (2, 12), (3, 13), (4, 14), (5, 15), (6, 16), (7, 17), (8, 18), (9, 19)]


and that's different from zip in exactly what way?
zip(l1,l2)

[(0, 10), (1, 11), (2, 12), (3, 13), (4, 14), (5, 15), (6, 16), (7, 17), (8, 18), (9, 19)]

(as Gene pointed out, the only difference between map(None, ...) and
zip(...) is that map() pads the shorter sequence, while zip() truncates
the long sequence).

</F>

Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

36 posts views Thread by Armin Rigo | last post: by
23 posts views Thread by YinTat | last post: by
1 post views Thread by James dean | last post: by
12 posts views Thread by karthikbalaguru | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.