473,406 Members | 2,217 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,406 software developers and data experts.

items in an array

Hi All -

I am working on a project in which I generate an array of values
(list_array). I need to use the values in this array to create list
similar to the one below:

list_array = []
list = item1,item2,itemN...

I am having difficulty in getting the values out of the original array.
I have tried enumerating over the array, but the results are not what
I need. When I attempt to simply print list, I get the following
output:

print list
['item1', 'item2', ..., 'itemN']

I can get list to be how I want it if I use the index value as follows:

list = ("%s" + "," + "%s", ...) % (list_array[0], list_array[1], ...

However, the list_array will never contain a constant number of items.
So my dilema is how to loop/iterate through list_array to create list
in the format I want.

Any suggestions are greatly appreciated.

-Shawn

Apr 19 '06 #1
6 1627
Ant
If you just want the items concatenated with a comma separator, the
following is what you need:
list_arr = ["one", "two", "three"]
list = ",".join(list_arr)
print(list)

one,two,three

Apr 19 '06 #2
> list_array = []
list = item1,item2,itemN...
My first recommendation would be that you not use "list" as
an identifier, as it's a builtin function. Odd bugs might
start happening if you redefine it.
I can get list to be how I want it if I use the index value as follows:

list = ("%s" + "," + "%s", ...) % (list_array[0], list_array[1], ...


If I understand correctly what you want, you're looking to
create a string that consists of commas separating each
element of your array. In such case, what you want is

result = ",".join(list_array)

or if you want spaces after your commas, the boringly
trivial modification:

result = ", ".join(list_array)

If instead you want the result as a tuple, you can just use
the tuple() function:

tuple_result = tuple(list_array)

If you want a tuple containing just the one string (which it
strangely seems like your example is doing), you can do

one_string_tuple = (",".join(list_array),)

(note the peculiar "trailing comma in parens creates a
one-element tuple" syntax...it often catches new Python
programmers off-guard)

HTH,

-tim

Apr 19 '06 #3
>>> list_array = ['aaa','bbb','ccc']
for item in list_array:

.... print item + ',',
....
aaa, bbb, ccc,
(notice the comma at the end of the print statement: this causes the
suppression of the automatic newline)

Is this what you need?

--
Renato Ramonda

Apr 19 '06 #4
> I am working on a project in which I generate an array of values
(list_array). I need to use the values in this array to create list
similar to the one below:

list_array = []
list = item1,item2,itemN...

I am having difficulty in getting the values out of the original array.
I have tried enumerating over the array, but the results are not what
I need. When I attempt to simply print list, I get the following
output:

print list
['item1', 'item2', ..., 'itemN']

I can get list to be how I want it if I use the index value as follows:

list = ("%s" + "," + "%s", ...) % (list_array[0], list_array[1], ...

However, the list_array will never contain a constant number of items.
So my dilema is how to loop/iterate through list_array to create list
in the format I want.

Any suggestions are greatly appreciated.


I'm not sure if I understand exactly what you want, but if all you
need is turning a list into a tuple then just use the function tuple:
mylist = [ 1, 2, 3 ]
mytuple = tuple( mylist )
print mylist [1, 2, 3] print mytuple (1, 2, 3)

Apr 19 '06 #5
Hi All -

Thanks to everyone for their input. The repsonses provided are exactly
what I was looking for!

Regards -
Shawn

Apr 19 '06 #6
sh******@gmail.com <sh******@gmail.com> wrote:
I can get list to be how I want it if I use the index value as follows:

list = ("%s" + "," + "%s", ...) % (list_array[0], list_array[1], ...

However, the list_array will never contain a constant number of items.
So my dilema is how to loop/iterate through list_array to create list
in the format I want.


I think what you want is:

list_string = ",".join(list_array)

(Don't use the name "list" as it shadows the builtin "list".)

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Apr 19 '06 #7

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

Similar topics

1
by: Colin Steadman | last post by:
I have a multi-dimensional array that I want to delete items from. To do this I display a form and the user clicks some tick boxes. Then on the delete page I check which tick boxes were...
0
by: tommazzo | last post by:
I'm currently writing a photo album sotware in C#. I'm using a ListView component to diaplay thumbnail previews of the photos and want to give the user the possibility to rearrange the photos via a...
4
by: mb | last post by:
what is the best way to do this: In a game I want to a class called "Items". This class will have the game items public class Items { public int Chair public int Table . . .and so on . . .
21
by: StriderBob | last post by:
Situation : FormX is mdi child form containing 2 ListViews ListView1 contains a list of table names and 4 sub items with data about each table. ListView2 contains a list of the columns on each...
0
by: Brian Henry | last post by:
Since no one else knew how to do this I sat here all morning experimenting with this and this is what I came up with... Its an example of how to get a list of items back from a virtual mode list...
9
by: Rob Meade | last post by:
Hi all, Ok - so I've got the array thing going on, and the session thing going on, and up until now its all been ok. I've got my view basket page which is displaying 3 rows (as an example) - I...
5
by: Phill W. | last post by:
(VB'2003) What's the correct way to remove multiple, selected items from a ListView control (say, from a ContextMenu)? I ask because I'm getting a very annoying ArgumentOutOfRangeException...
10
by: pamelafluente | last post by:
Hi I have a sorted list with several thousands items. In my case, but this is not important, objects are stored only in Keys, Values are all Nothing. Several of the stored objects (might be a...
1
by: dadevil | last post by:
Hi guys, this is my first post around here, and here it goes I need to create an array to store info about a number "n" of persons, so i create this structure ( each person have one name and one...
0
by: kasthurirangan.balaji | last post by:
Hello, I have some items in memory(character array) separarted with the newline character and the items are sorted inside the array. I already have a version of the code using a multimap object....
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.