473,574 Members | 3,173 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 #1
23 19724
On Sat, 15 Sep 2007 12:36:02 +0000, Summercool wrote:
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?
Convert them to strings before joining:

In [145]: foo = [1, 2, 3]

In [146]: ','.join(map(st r, foo))
Out[146]: '1,2,3'

Ciao,
Marc 'BlackJack' Rintsch
Sep 15 '07 #2
On Sep 15, 10:36 pm, 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?
>>foo = [1,2,3]
",".join(str( x) for x in foo)
'1,2,3'
>>",".join(map( str, foo))
'1,2,3'
>>>
If you are going to write several such results to a file, consider
using the csv module.

HTH,
John

Sep 15 '07 #3
js
print ''.join([str(i) for i in [1,2,3]])

On 9/15/07, Summercool <Su************ @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?

--
http://mail.python.org/mailman/listinfo/python-list
Sep 15 '07 #4
js escribió:
>On 9/15/07, Summercool <Su************ @gmail.comwrote :
>in Python... is the method to use ",".join() ? but then it must take
a list of strings... not integers...

any fast method?
print ''.join([str(i) for i in [1,2,3]])
It's better to use generator comprehension instead of LC:

",".join(st r(i) for i in [1, 2, 3])

Or, if you happen to like the itertools modules:

from itertools import imap
",".join(imap(s tr, [1, 2, 3]))
Sep 15 '07 #5
On 2007-09-15, Arnau Sanchez <ar***@ehas.org wrote:
>>in Python... is the method to use ",".join() ? but then it
must take a list of strings... not integers...

any fast method?
print ''.join([str(i) for i in [1,2,3]])

It's better to use generator comprehension instead of LC:

",".join(st r(i) for i in [1, 2, 3])

Or, if you happen to like the itertools modules:

from itertools import imap
",".join(imap(s tr, [1, 2, 3]))
It's nice people have invented so many ways to spell the
builting "map" ;)
>>",".join(map( str,[1,2,3]))
'1,2,3'

--
Grant Edwards grante Yow! Thousands of days of
at civilians... have produced
visi.com a... feeling for the
aesthetic modules --
Sep 15 '07 #6

On Sep 15, 2007, at 8:56 AM, Arnau Sanchez wrote:
js escribió:
>>On 9/15/07, Summercool <Su************ @gmail.comwrote :
>>in Python... is the method to use ",".join() ? but then it must
take
a list of strings... not integers...

any fast method?
>print ''.join([str(i) for i in [1,2,3]])

It's better to use generator comprehension instead of LC:

",".join(st r(i) for i in [1, 2, 3])
Why is that? That entire expression must be evaluated to obtain the
result, so what is the advantage of using a generator comprehension
v. a list comprehension?
Sep 15 '07 #7
On 2007-09-15, Erik Jones <er**@myemma.co mwrote:
>>print ''.join([str(i) for i in [1,2,3]])

It's better to use generator comprehension instead of LC:

",".join(str(i ) for i in [1, 2, 3])

Why is that? That entire expression must be evaluated to obtain the
result, so what is the advantage of using a generator comprehension
v. a list comprehension?
The generator avoids creating the intermediate list -- it
generates the intermediate values on the fly. For short
sequences it probably doesn't matter much. For a very long
list it's probably noticable.

--
Grant Edwards grante Yow! Mr and Mrs PED, can I
at borrow 26.7% of the RAYON
visi.com TEXTILE production of the
INDONESIAN archipelago?
Sep 15 '07 #8
On Sep 15, 2007, at 11:07 AM, Grant Edwards wrote:
On 2007-09-15, Arnau Sanchez <ar***@ehas.org wrote:
>>>in Python... is the method to use ",".join() ? but then it
must take a list of strings... not integers...

any fast method?
>>print ''.join([str(i) for i in [1,2,3]])

It's better to use generator comprehension instead of LC:

",".join(str(i ) for i in [1, 2, 3])

Or, if you happen to like the itertools modules:

from itertools import imap
",".join(imap( str, [1, 2, 3]))

It's nice people have invented so many ways to spell the
builting "map" ;)
>>>",".join(map (str,[1,2,3]))
'1,2,3'
IIRC, map's status as a builtin is going away.

Erik Jones

Software Developer | Emma®
er**@myemma.com
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com
Sep 15 '07 #9
It's nice people have invented so many ways to spell the
builting "map" ;)
>>",".join(map( str,[1,2,3]))
'1,2,3'

IIRC, map's status as a builtin is going away.
Actually, py3k built-in map == itertools.imap
>>map(str, [])
<itertools.im ap object at 0xb7c7c9ec>

--
http://www.advogato.org/person/eopadoan/
Bookmarks: http://del.icio.us/edcrypt
Sep 15 '07 #10

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

Similar topics

58
10090
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
4253
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...
20
4670
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
3214
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
1683
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
15014
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...
2
1322
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...
7
6417
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
1174
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
8100
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. ...
1
7856
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...
0
8137
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6509
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...
0
5335
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...
0
3773
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...
1
2270
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
1
1369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1099
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...

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.