how to join array of integers?

J

John Machin

i think in Ruby, if you have an array (or list) of integers
foo = [1, 2, 3]
you can use foo.join(",") to join them into a string "1,2,3"
in Python... is the method to use ",".join() ? but then it must take
a list of strings... not integers...
any fast method?

Isn't the OP just looking for something like:


foo=[1,2,3]
bar=[4,5,6]
foo+bar
[1, 2, 3, 4, 5, 6]

No. Read what he wrote. He has a SINGLE list whose elements are (e.g.)
integers [1, 2, 3], *NOT* two lists. He wants to create a STRING
"1,2,3", not a list.
 
P

Paul Rudin

Steven D'Aprano said:
Did you try it,
Yes.

or are you guessing?

Well - I guessed first, otherwise it wouldn't have been worth trying :)
What do you call a decent size?

I used 10,000.
Unfortunately, Python doesn't make it as easy to measure memory use as it
does to time snippets of code, so that's just a hypothetical.

Well - as it turns out the list gets made anyway by the join method,
so in this case there's probably little difference. However there
(obviously) are siturations where a generator is going to save you a
significant memory over the similar list comprehension.
Who says it doesn't need to make a list? string.join() needs a sequence.

The generator doesn't make a list. The implementation of str.join does
apparently needs a sequence and so makes a list from the generator
passed to it, which is presumably why you get essentially the same
performance once you factor out setup noise for small lists.

Although it's not clear to me why the join method needs a sequence
rather than just an iterator.
 
T

timw.google

i think in Ruby, if you have an array (or list) of integers
foo = [1, 2, 3]
you can use foo.join(",") to join them into a string "1,2,3"
in Python... is the method to use ",".join() ? but then it must take
a list of strings... not integers...
any fast method?
Isn't the OP just looking for something like:
foo=[1,2,3]
bar=[4,5,6]
foo+bar
[1, 2, 3, 4, 5, 6]

No. Read what he wrote. He has a SINGLE list whose elements are (e.g.)
integers [1, 2, 3], *NOT* two lists. He wants to create a STRING
"1,2,3", not a list.- Hide quoted text -

- Show quoted text -

You're right. I read it wrong. Sorry.
 
A

Arnaud Delobelle

Although it's not clear to me why the join method needs a sequence
rather than just an iterator.

Pure guess (I haven't looked at the code): the join method needs to
know the length of the string it builds in order to allocate the
correct amount of memory, therefore needs to traverse the iterable
object it is joining *twice* (find the length, allocate, fill in)?
 
S

Steve Holden

Paul said:
Well - I guessed first, otherwise it wouldn't have been worth trying :)


I used 10,000.


Well - as it turns out the list gets made anyway by the join method,
so in this case there's probably little difference. However there
(obviously) are siturations where a generator is going to save you a
significant memory over the similar list comprehension.


The generator doesn't make a list. The implementation of str.join does
apparently needs a sequence and so makes a list from the generator
passed to it, which is presumably why you get essentially the same
performance once you factor out setup noise for small lists.

Although it's not clear to me why the join method needs a sequence
rather than just an iterator.

I suspect it's to do with making memory allocation easier, though I
didn't read the whole gory detail in stringobject.c. Without a
pre-existing set of joinable items you have to build the string up using
some sort of allocate-and-reallocate strategy, whereas if you have the
joinables already in a sequence you can pre-compute how much memory the
result will need.

Ah, right, it's in the comment:

* Do a pre-pass to figure out the total amount of space we'll
* need (sz), see whether any argument is absurd, and defer to
* the Unicode join if appropriate.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top