1

I have a list of strings:

a = ['[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]',
     '[2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]',
     '[3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0]',
     '[4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0]',
     '[5.0, 5.0, 5.0]', 
     '[1.0]'
    ]

When I use the code as below, the format for every element in the list is string, not the int.

a= tuple(a.reshape(1,-1)[0])

What I need to do is change the format for all elements. Such as

a[1][2] = 2

However, in my code:

a[1][2] = '.'
1
  • a is a list of strings. Commented Jun 19, 2014 at 19:56

4 Answers 4

4

a is a list of strings. I don't know where you got it from, but as far as I understand your question you want it to be a list of lists which contain numbers. This might be a use case for the eval function.

>>> a
['[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]', '[2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]', '[3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0]', '[4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0]', '[5.0, 5.0, 5.0]', '[1.0]']
>>> a = [eval(x) for x in a]
>>> a
[[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0], [3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0], [4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0], [5.0, 5.0, 5.0], [1.0]]
>>> a[1][2]
2.0

If you do not want to use eval, I would suggest

a = [[float(x) for x in liststring[1:-1].split(',')] for liststring in a]
Sign up to request clarification or add additional context in comments.

1 Comment

haha, almost at the exact same time. Yeah, I feel the same, eval makes me feel uneasy, but I don't think this is going to be used to execute code dynamically
1

How about this:

>>> a =['[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]', '[2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]','[3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0]','[4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0]','[5.0, 5.0, 5.0]','[1.0]']
>>> a
['[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]', '[2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]', '[3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0]', '[4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0]', '[5.0, 5.0, 5.0]', '[1.0]']
>>> a2 = [eval(l) for l in a]
>>> a2[1][2]
2.0

Comments

1

Just use the JSON module; it is made specifically for these kinds of jobs. I made an example below. It converts all the items of list a (which are strings originally) into lists of floats. If you want to convert them to ints, just iterate through the nested lists and make blahblah = int(blahblah) (just be careful that you don't put something into the list that can't be converted into an integer, such as "s").

from json import loads

a = ['[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]',
 '[2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]',
 '[3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0]',
 '[4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0]',
 '[5.0, 5.0, 5.0]', 
 '[1.0]'
]

for nestlist in range(len(a)):
    a[nestlist] = loads(a[nestlist])

print a

Hope this helps!
Palmer :)

Comments

1

Your a is a list of strings, not a list of lists. It should be:

a = [[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
[2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0],
[3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0],
[4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0],
[5.0, 5.0, 5.0],
[1.0]]

EDIT -- AN ACTUAL ANSWER:

Okay I realize this is an old problem for you now, but I just ran across another solution that does not use eval() (and thus avoids the security issues). Your list of strings a can be decoded using the json module and then turned into a new list of lists:

import json

def decodeList(list_of_strings):
    newList = list()
    for i in list_of_strings:
         newList.append(json.loads(i))
    return newList

a = decodeList(a)

print a[1][2]

# outputs 2.0

EDIT AGAIN:

Haha I just read through the rest of the answers and noticed someone already offered this one! Sigh. Nevermind.

2 Comments

Bro, I know what you mean. However, my case is a list of list. I want to find any element as an int. in my list, not a string
Ah, sorry misunderstood the issue. @timgeb looks like a working answer then!

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.