Isn't there a way to split items in a list into text separated by spaces? Like turn
[4, 6, 9]
INTO
4 6 9
Isn't there a way to split items in a list into text separated by spaces? Like turn
[4, 6, 9]
INTO
4 6 9
I'm confident that there's a much more efficient way, but for now (while I research it):
After some research, it turns out that it's quite easy to just convert the list into a string (as python's print does), then remove extraneous characters. E.g.Code:#any list here some_list = [1, 2, 3] #blank storage string some_list_string = '' #iterate over every item in the list for num in some_list: #append the number and a space some_list_string += str(num) + ' ' print some_list_string
EDIT 2: Python's join is even better, see the people below.Code:#Any list here. some_list = [1, 2, 3] #Convert to string, remove '[' and ']', and remove commas. some_list_string = str(some_list).strip('[]').replace(',', '') print some_list_string
Last edited by MG&TL; March 6th, 2013 at 03:42 PM.
I think the Python way would be:
EDIT: Or, if it's a list of integers:Code:' '.join(some_list)
Code:' '.join([str(n) for n in some_list])
Last edited by r-senior; March 6th, 2013 at 03:47 PM. Reason: Ah, Vaphell beat me too it
Please create new threads for new questions.
Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].
integers need to be converted to strings with str()
Code:$ python Python 2.7.2+ (default, Jul 20 2012, 22:15:08) [GCC 4.6.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> l=[4,6,9] >>> ' '.join(str(x) for x in l) '4 6 9'
Last edited by Vaphell; March 6th, 2013 at 03:44 PM.
if your question is answered, mark the thread as [SOLVED]. Thx.
To post code or command output, use [code] tags.
Check your bash script here // BashFAQ // BashPitfalls
Thanks guys for the help
should work, too.Code:' '.join(map(str, list))
Bookmarks