How to replace single quotes from a list in python

前端 未结 3 774
我寻月下人不归
我寻月下人不归 2021-01-25 10:28

I have a list:

my_list = [\'\"3\"\', \'\"45\"\',\'\"12\"\',\'\"6\"\']

This list has single and double quotes and the item value. How can I repl

3条回答
  •  青春惊慌失措
    2021-01-25 10:43

    As suggested by @MartijnPieters in comments, you can use replace on the strings to get the desired output.

    The change I like to suggest is that using .replace('"', '') instead of .replace('"', ' '). Otherwise the resultant strings will have a leading and trailing white space

    You can use list comprehension to deal with the list you have like this

    my_list = ['"3"', '"45"','"12"','"6"']
    
    new_list = [x.replace('"', '') for x in my_list]
    
    print(new_list) # ['3', '45', '12', '6']
    

提交回复
热议问题