remove single quotes in list, split string avoiding the quotes

2024/7/6 22:32:44

Is it possible to split a string and to avoid the quotes(single)?

I would like to remove the single quotes from a list(keep the list, strings and floats inside:

l=['1','2','3','4.5']

desired output:

l=[1, 2, 3, 4.5]

next line works neither with int nor float

l=[float(value) for value in ll]
Answer

To get int or float based on what each value looks like, so '1' becomes 1 and "1.2" becomes 1.2, you can use ast.literal_eval to convert the same way Python's literal parser does, so you have an actual list of ints and floats, rather than a list of str (that would include the quotes when echoed):

>>> import ast
>>> [ast.literal_eval(x) for x in l]
[1, 2, 3, 4.5]

Unlike plain eval, this doesn't open security holes since it can't execute arbitrary code.

You could use map for a mild performance boost here (since ast.literal_eval is a built-in implemented in C; normally, map gains little or loses out to list comprehensions), in Py 2, map(ast.literal_eval, l) or in Py3 (where map returns a generator, not a list), list(map(ast.literal_eval, l))

If the goal is purely to display the strings without quotes, you'd just format manually and avoid type conversions entirely:

>>> print('[{}]'.format(', '.join(l)))
[1, 2, 3, 4.5]
https://en.xdnf.cn/q/119581.html

Related Q&A

Image Segmentation to Map Cracks

I have this problem that I have been working on recently and I have kind of reached a dead end as I am not sure why the image saved when re opened it is loaded as black image. I have (original) as my b…

operations on column length

Firstly, sorry if I have used the wrong language to explain what Im operating on, Im pretty new to python and still far from being knowledgeable about it.Im currently trying to do operations on the len…

Python: Parse one string into multiple variables?

I am pretty sure that there is a function for this, but I been searching for a while, so decided to simply ask SO instead.I am writing a Python script that parses and analyzes text messages from an inp…

How do I pull multiple values from html page using python?

Im performing some data analysis for my own knowledge from nhl spread/betting odds information. Im able to pull some information, but Not the entire data set. I want to pull the list of games and the a…

Creating h5 file for storing a dataset to train super resolution GAN

I am trying to create a h5 file for storing a dataset for training a super resolution GAN. Where each training pair would be a Low resolution and a High resolution image. The dataset will contain the d…

How to resolve wide_to_long error in pandas

I have following dataframeAnd I want to convert it into the following format:-To do so I have used the following code snippet:-df = pd.wide_to_long(df, stubnames=[manufacturing_unit_,outlet_,inventory,…

Odoo 10: enter value in Many2one field dynamically

I added in my models.py :commercial_group = fields.Many2one("simcard.simcard")and in my views.xml :<field name="commercial_group" widget="selection"/>And then i am t…

How to erode this thresholded image using OpenCV

I am trying to first remove the captcha numbers by thresholding and then eroding it ,to get slim continuous lines to get better output. Problem:the eroded image is not continuous as u can see Original …

Searching for only the first value in an array in a csv file

So i am creating a account login system which searches a database for a username (and its relevant password) and, if found, will log the user on.This is what the csv file currently looks like[dom, ente…

how to write a single row cell by cell and fill it in csv file

I have a CSV file that only has column headers:cat mycsv.csvcol_1@@@col_2@@@col_3@@@col_3I have to fill a single row with None values in each cell of the CSV file. Can someone suggest me the best-optim…