473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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 1233
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: Armin Rigo | last post by:
Hi! This is a rant against the optimization trend of the Python interpreter. Sorting a list of 100000 integers in random order takes: * 0.75 seconds in Python 2.1 * 0.51 seconds in Python...
23
by: YinTat | last post by:
Hi, I learned C++ recently and I made a string class. A code example is this: class CString { public: inline CString(const char *rhs) { m_size = strlen(rhs);
98
by: jrefactors | last post by:
I heard people saying prefix increment is faster than postfix incerement, but I don't know what's the difference. They both are i = i+1. i++ ++i Please advise. thanks!!
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
1
by: James dean | last post by:
I done a test and i really do not know the reason why a jagged array who has the same number of elements as a multidimensional array is faster here is my test. I assign a value and do a small...
9
by: VenuGopal | last post by:
Hi, why n++ executes faster than n+1..... or does it realli execute faster? thanks Venugopal.B
11
by: ctman770 | last post by:
Hi Everyone, Is it faster to save the precise location of an html dom node into a variable in js, or to use getElementById everytime you need to access the node? I want to make my application...
12
by: karthikbalaguru | last post by:
Hi, How is 'Int' Faster than 'Char' ? I think , 'Char' is small and so it should be easily & efficiently . Can someone here provide some info regarding this. Thanks and Regards, Karthik...
23
by: Python Maniac | last post by:
I am new to Python however I would like some feedback from those who know more about Python than I do at this time. def scrambleLine(line): s = '' for c in line: s += chr(ord(c) | 0x80)...
41
by: c | last post by:
Hi every one, Me and my Cousin were talking about C and C#, I love C and he loves C#..and were talking C is ...blah blah...C# is Blah Blah ...etc and then we decided to write a program that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
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...
0
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...

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.