Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I evaluate a list of strings as a list of tuples in Python?

I have a list of thousands of elements of a form like the following:

pixels = ['(112, 37, 137, 255)', '(129, 39, 145, 255)', '(125, 036, 138, 255)' ...]

I am trying to convert these string elements to tuples using ast.literal_eval, but it is breaking on encountering things like leading zeros (e.g. in the third tuple string shown) with the error SyntaxError: invalid token.

pixels = [ast.literal_eval(pixel) for pixel in pixels]

What would be a good way to deal with things like this and get this list of strings evaluated as a list of tuples?

like image 501
d3pd Avatar asked Jun 23 '15 17:06

d3pd


People also ask

How do you make a list of strings into a list of tuples?

Method #1 : Using map() + split() + tuple() This task can be achieved using the combination of these functions. The map function can be used to link the logic to each string, split function is used to split the inner contents of list to different tuple attributes and tuple function performs the task of forming a tuple.

How do you check if a string is present in a list of tuples Python?

On each iteration, we check if the string a is contained in the current tuple and return the result. The in operator tests for membership. For example, x in t evaluates to True if x is a member of t , otherwise it evaluates to False . x not in t returns the negation of x in t .

How do you convert a string to a tuple in Python?

Method #1 : Using map() + int + split() + tuple() This method can be used to solve this particular task. In this, we just split each element of string and convert to list and then we convert the list to resultant tuple.

How do I get a list of tuples in Python?

In the majority of programming languages when you need to access a nested data type (such as arrays, lists, or tuples), you append the brackets to get to the innermost item. The first bracket gives you the location of the tuple in your list. The second bracket gives you the location of the item in the tuple.


1 Answers

Use re module.

>>> import re
>>> import ast
>>> pixels = ['(112, 37, 137, 255)', '(129, 39, 145, 255)', '(125, 036, 138, 255)']
>>> [ast.literal_eval(re.sub(r'\b0+', '', pixel)) for pixel in pixels]
[(112, 37, 137, 255), (129, 39, 145, 255), (125, 36, 138, 255)]

re.sub(r'\b0+', '', pixel) helps to remove the leading zeros. \b matches between a word character and a non-word character or vice-versa, so here there must be an word boundary exists before zero and after the space or ( symbol.

Update:

>>> pixels = ['(0, 0, 0, 255)', '(129, 39, 145, 255)', '(125, 036, 138, 255)']
>>> [ast.literal_eval(re.sub(r'\b0+\B', '', pixel)) for pixel in pixels]
[(0, 0, 0, 255), (129, 39, 145, 255), (125, 36, 138, 255)]
like image 61
Avinash Raj Avatar answered Sep 28 '22 07:09

Avinash Raj