Stagg
Stagg

Reputation: 309

Convert a list of string into tuples

I have this list of strings :

['(39.2947500000, -76.6565600000)', '(39.3423900000, -76.5698300000)', '(39.3199500000, -76.6222000000)', '(39.2533200000, -76.6263600000)', '(39.3068100000, -76.6549700000)', '(39.2937500000, -76.6233700000)', '(39.3146700000, -76.6425300000)', '(39.3073300000, -76.6015900000)', '(39.2451900000, -76.6336400000)', '(39.3283000000, -76.5893200000)', '(39.3215400000, -76.6736800000)', '(39.3010000000, -76.5977400000)', '(39.3122600000, -76.6194200000)', '(39.3161400000, -76.5663900000)', '(39.3573500000, -76.6005300000)', '(39.3311200000, -76.6315100000)', '(39.3311200000, -76.6315100000)', '(39.2832900000, -76.5996300000)', '(39.2868200000, -76.6063900000)', '(39.3031200000, -76.6461100000)']

I need to convert this strings to tuples, so that the output is:

[(39.2947500000, -76.6565600000),(39.3423900000, -76.5698300000)......]

I tried to use float method but it gives the this error:

ValueError: could not convert string to float: (39.2947500000, -76.6565600000)

Thanks in advance

Upvotes: 1

Views: 327

Answers (4)

boardrider
boardrider

Reputation: 6185

If you wish to avoid imports and you feel
this may be the place to avoid list comprehension in favour of clarity*:

lst = ['(39.2947500000, -76.6565600000)', '(39.3423900000, -76.5698300000)', '(39.3199500000, -76.6222000000)', '(39.2533200000, -76.6263600000)', '(39.3068100000, -76.6549700000)', '(39.2937500000, -76.6233700000)', '(39.3146700000, -76.6425300000)', '(39.3073300000, -76.6015900000)', '(39.2451900000, -76.6336400000)', '(39.3283000000, -76.5893200000)', '(39.3215400000, -76.6736800000)', '(39.3010000000, -76.5977400000)', '(39.3122600000, -76.6194200000)', '(39.3161400000, -76.5663900000)', '(39.3573500000, -76.6005300000)', '(39.3311200000, -76.6315100000)', '(39.3311200000, -76.6315100000)', '(39.2832900000, -76.5996300000)', '(39.2868200000, -76.6063900000)', '(39.3031200000, -76.6461100000)']

out = list()
for i in lst:
    j,k = i.split(",")
    out.append(tuple((float(j[1:]), float(k[:-1]))))
print(out)



* See https://www.python.org/dev/peps/pep-0020/

Upvotes: 0

riteshtch
riteshtch

Reputation: 8769

>>> L=['(39.2947500000, -76.6565600000)', '(39.3423900000, -76.5698300000)', '(39.3199500000, -76.6222000000)', '(39.2533200000, -76.6263600000)', '(39.3068100000, -76.6549700000)', '(39.2937500000, -76.6233700000)', '(39.3146700000, -76.6425300000)', '(39.3073300000, -76.6015900000)', '(39.2451900000, -76.6336400000)', '(39.3283000000, -76.5893200000)', '(39.3215400000, -76.6736800000)', '(39.3010000000, -76.5977400000)', '(39.3122600000, -76.6194200000)', '(39.3161400000, -76.5663900000)', '(39.3573500000, -76.6005300000)', '(39.3311200000, -76.6315100000)', '(39.3311200000, -76.6315100000)', '(39.2832900000, -76.5996300000)', '(39.2868200000, -76.6063900000)', '(39.3031200000, -76.6461100000)']
>>> import ast
>>> list(map(lambda x:ast.literal_eval(x), L))
[(39.29475, -76.65656), (39.34239, -76.56983), (39.31995, -76.6222), (39.25332, -76.62636), (39.30681, -76.65497), (39.29375, -76.62337), (39.31467, -76.64253), (39.30733, -76.60159), (39.24519, -76.63364), (39.3283, -76.58932), (39.32154, -76.67368), (39.301, -76.59774), (39.31226, -76.61942), (39.31614, -76.56639), (39.35735, -76.60053), (39.33112, -76.63151), (39.33112, -76.63151), (39.28329, -76.59963), (39.28682, -76.60639), (39.30312, -76.64611)]

For python 2.x: map(lambda x:ast.literal_eval(x), L)

Edit: some explanation:

ast stands for Abstract Syntax Tree. literal_eval() is much more safe than eval().

Quoting from official doc:

ast.literal_eval(node_or_string) Safely evaluate an expression node or a Unicode or Latin-1 encoded string containing a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None.

This can be used for safely evaluating strings containing Python values from untrusted sources without the need to parse the values oneself. It is not capable of evaluating arbitrarily complex expressions, for example involving operators or indexing.

Upvotes: 6

Jan van der Vegt
Jan van der Vegt

Reputation: 1511

my_list = ['(39.2947500000, -76.6565600000)', '(39.3423900000, -76.5698300000)']
print [(float(x.split(',')[0][1:]), float(x.split(',')[1][1:-1])) for x in my_list]

This solution does not use eval (which I dislike using), it iterates over the strings, splits them on the ',', substrings them to remove the '(', ' ' and ')' and then casts them to a float

Upvotes: 1

Zafi
Zafi

Reputation: 629

You could use eval:

a = ['(39.2947500000, -76.6565600000)', '(39.3423900000, -76.5698300000)', '(39.3199500000, -76.6222000000)', '(39.2533200000, -76.6263600000)', '(39.3068100000, -76.6549700000)', '(39.2937500000, -76.6233700000)', '(39.3146700000, -76.6425300000)', '(39.3073300000, -76.6015900000)', '(39.2451900000, -76.6336400000)', '(39.3283000000, -76.5893200000)', '(39.3215400000, -76.6736800000)', '(39.3010000000, -76.5977400000)', '(39.3122600000, -76.6194200000)', '(39.3161400000, -76.5663900000)', '(39.3573500000, -76.6005300000)', '(39.3311200000, -76.6315100000)', '(39.3311200000, -76.6315100000)', '(39.2832900000, -76.5996300000)', '(39.2868200000, -76.6063900000)', '(39.3031200000, -76.6461100000)'] 
b = [ eval(x) for x in a ]

Upvotes: 2

Related Questions