A Faster Way...

A

andrea.gavana

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
 
A

Andrew Dalke

andrea.gavana said:
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
.... iterables = map(iter, iterables)
.... for element in itertools.cycle(iterables):
.... yield element.next()
....
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
(e-mail address removed)
 
S

Steven Bethard

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
 
K

Klaus Alexander Seistrup

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+

#v-

Cheers,
 
G

gene.tani

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.
 
S

stasz

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
 
F

Fredrik Lundh

stasz said:
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?
[(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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top