473,890 Members | 1,345 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to join array of integers?

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?

Sep 15 '07
23 19766
On Sep 17, 10:16 pm, "timw.googl e" <tjand...@yahoo .comwrote:
On Sep 15, 8:36 am, Summercool <Summercooln... @gmail.comwrote :
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.

Sep 17 '07 #21
Steven D'Aprano <st***@REMOVE-THIS-cybersource.com .auwrites:
On Sun, 16 Sep 2007 19:25:22 +0100, Paul Rudin wrote:
>>The generator expression takes about twice as long to run, and in my
opinion it is no more readable. So what's the advantage?

If you do it with a decent size list they take more or less the same
time.

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.
>
>You'd presumably expect the generator to use less memory; which
might be an advantage if you have large lists.

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.
>
>Isn't it odd that the generator isn't faster, since the comprehension
presumably builds a list first and then iterates over it, whereas the
generator doesn't need to make a list?

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.
Sep 17 '07 #22
On Sep 17, 8:46 am, John Machin <sjmac...@lexic on.netwrote:
On Sep 17, 10:16 pm, "timw.googl e" <tjand...@yahoo .comwrote:


On Sep 15, 8:36 am, Summercool <Summercooln... @gmail.comwrote :
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.

Sep 17 '07 #23
Paul Rudin wrote:
Steven D'Aprano <st***@REMOVE-THIS-cybersource.com .auwrites:
>On Sun, 16 Sep 2007 19:25:22 +0100, Paul Rudin wrote:
>>>The generator expression takes about twice as long to run, and in my
opinion it is no more readable. So what's the advantage?
If you do it with a decent size list they take more or less the same
time.
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.
>>
>>You'd presumably expect the generator to use less memory; which
might be an advantage if you have large lists.
Unfortunatel y, 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.
>>
>>Isn't it odd that the generator isn't faster, since the comprehension
presumably builds a list first and then iterates over it, whereas the
generator doesn't need to make a list?
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.
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

Sep 17 '07 #24

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
10223
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
4
4276
by: William | last post by:
I would appreciate your help on the following programming questions: 1. Given an array of length N containing integers between 1 and N, determine if it contains any duplicates. HINT: The solution is: http://domino.research.ibm.com/Comm/wwwr_ponder.nsf/solutions/January2004.html but I don't understand what they meant by "Let f(x) be the location of the
20
4735
by: Pavel Stehule | last post by:
Hello, Is possible merge two arrays like array + array => array select array_append(array, array); ERROR: function array_append(integer, integer) does not exist
46
3273
by: RoSsIaCrIiLoIA | last post by:
Write a function that gets an array of unsigned int fill it with random values all differents, and sorts it. It should be faster than qsort too. Do you like my solution? _______________________ #include <stdio.h> #include <stdlib.h> #include <time.h>
8
1702
by: engaref | last post by:
Hello Every body, I am new with C programming.I have received the Problems from my advisor on Array but I did not find any Proper answer yet. If Possible,please make a solution for the Problems. Thanks The Problems are as follows: 1-Declare an array of length of 10 and read integers into the elements
14
15058
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in a comma-delimited string or string array. I don't want it to be a string because I want to overload this function, and it's sister already uses a string input parameter. Now if I define the function to take in a string array, it solves my...
2
1345
by: JP2006 | last post by:
I have a situation where I have 2 tables - NODES and TAGS. Nodes is the main table and contains various columns one of which is tagID. Tags contains a list of tag names and tagIDs. When a user adds an entry to the Nodes table they can specify one or more tags for the entry. Then when the form is posted if there were multiple tags in the post their tagIDs are comma seperated before being inserted in the tagID column of the Nodes table....
7
6446
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are now thown out of the array released properly by the CLI?
2
1185
by: André | last post by:
Hi, I have a two-dimension array with integers and i want to join it into a string. See my code: dim va(10,5) as integer dim mystring as string .... for j=1 to 10 for k=1 to 5 mystring = Join(";",va(j,k).ToString)
0
9980
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11236
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10925
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,...
0
9641
agi2029
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...
0
7172
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();...
0
5855
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4682
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
2
4276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3283
bsmnconsultancy
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...

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.