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 [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do i convert a character array to a string?

How do I convert a Swift Array to a String?

How do I convert an array of integers to binary?

How do I convert an object to an array?

How do I convert a string to an array?

How do I convert JSON to Array

How do I convert a data set to an array

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

How do I convert a JSON array to an ArrayList?

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

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

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 strings to a array of unique values?

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

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 array (decimal[]) to dynamic[] array?

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

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

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

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

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

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

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

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

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

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

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

TOP Ranking

  1. 1

    How to return a single asset OhMyBrew Laravel Shopify

  2. 2

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    pump.io port in URL

  6. 6

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  7. 7

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  8. 8

    Make a B+ Tree concurrent thread safe

  9. 9

    Group boxplot data while keeping their individual X axis labels in ggplot2 in R

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  12. 12

    BigQuery - concatenate ignoring NULL

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  15. 15

    Razor Page - How to redirect to another folder's page using asp-for tag helper

  16. 16

    "django.db.utils.ProgrammingError: relation "auth_user" does not exist" Django V2.0

  17. 17

    How to exclude a specific git submodule from update?

  18. 18

    how to fix CMake Error in CMakeLists.txt: Generator NMake Makefiles does not support platform specification, but platform x64 was specified

  19. 19

    Is there a way to ask the user before loading the website

  20. 20

    How to remove the extra space from right in a webview?

  21. 21

    Optimising manipulation of large vectors

HotTag

Archive