How do I convert this to an array?

user3043594

I'm trying to convert a string that looks like this into an array of tuples

"[(1,2), (2,3), (4,5)]" -> [(1,2), (2,3), (4,5)]

Here is the body of code I would like to populate:

def convert_to_polygon(polygon_string):

    return polygon_array

Should I be using a python library? Does one exist for this job?

Ashwini Chaudhary

You can use ast.literal_eval:

>>> import ast
>>> s = '{{1,2},{2,3},{4,5}}'
>>> polygon_array = ast.literal_eval(s.replace('{', '(').replace('}', ')'))
>>> polygon_array
((1, 2), (2, 3), (4, 5))
>>> polygon_array[1][0]
2

Use list(polygon_array) if you want a list of tuples.

Collected from the Internet

Please contact javaer1[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert an array of integers to binary?

How do I convert a pandas Series or index to a Numpy array?

How do I convert an array of strings to a array of unique values?

How do I convert a Java byte array into a Scala byte array?

How do I convert a Go array of strings to a C array of strings?

How do I convert a Swift Array to a String?

How do I convert an object to an array?

How do I convert an array of integers to an array of objects?

How do I convert an array of strings to an object property in javascript?

How do i convert a character array to a string?

Dart: How do I convert an array of objects to an array of hashmaps?

How do I convert an array `UnsafePointer<DSPComplex>` to an array of Floats?

How do I convert an array of UnsafeMutablePointer<Double> to an array of Double?

How do I convert an array of floats into an array of unique integers?

How do I convert a RAID 4 array to RAID 0?

How do I convert this php array into a JavaScript array of objects?

How do I use GSON to convert an object array to an array of their IDs

How do I convert an array into an object within array - Angular and Javascript

How do I convert JSON to Array

How do I convert array (decimal[]) to dynamic[] array?

How do I convert a JSON array to an ArrayList?

How do I convert a data set to an array

How do I convert a string to an array?

How do I convert a promise into the array it holds?

In Typescript, how do I convert a string of strings as an array of strings?

How do I convert an array or a list into a hashref?

How do I convert an array object into a two-dimensional array

How do I convert a nested array to a 'keyed' array in JavaScript?

In Powershell, how do I convert an array of PSObjects to an array of Strings?