0

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!

1
  • 1
    Might want to just keep the original list instead of converting and reconverting. Commented Apr 17, 2014 at 15:12

3 Answers 3

4

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]
Sign up to request clarification or add additional context in comments.

3 Comments

@AlexThornton Thanks for the lighting fast reply. I'm sure this works but im getting a "zero length field name in format" error. This software runs on python 2.6 I believe, would that have anything to do with it?
Ah, I see. It's easy to fix, see the edit. Notice the 0. :)
@Motion4D You're very welcome, come back any time. :)
3

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]

Comments

0
a = [8, 9, 10, 11]
b = '.' + '.'.join(str(x) for x in a)
v = [int(x) for x in b.split('.')[1:]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.