Question

I need to pass a list of numbers in a 3D program I'm using but it can only pass a single value. I want to pass the list as a single string separated by decimal points and then later convert that string back to a list. Here's the outcome I'm looking for:

a=[8,9,10,11] # will always contain integers

list elements converted to decimal then join as a string

a=".8.9.10.11"

then later on convert that string back to a list

a=[8,9,10,11]

I'm thinking it's a combo of the map() and join() function but I'm not exactly sure. Thanks for any help!

Was it helpful?

Solution

Starting with a as [8,9,10,11]:

First one would be (to string):

a = ''.join('.{0}'.format(d) for d in a)
# ".8.9.10.11"

Second would be (back to list of ints again):

a = [int(i) for i in a.split('.')[1:]]
# [8, 9, 10, 11]

OTHER TIPS

To convert that list to a string, just do:

a=[8,9,10,11]
b = '.'+'.'.join(a)

>>> print b
.8.9.10.11

To get the list back from the string, you do:

b = '.8.9.10.11'
a = b[1:].split('.')

>>> print a
[8,9,10,11]
a = [8, 9, 10, 11]
b = '.' + '.'.join(str(x) for x in a)
v = [int(x) for x in b.split('.')[1:]]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top