Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从不需要的字符中去除数组_Python_Python 2.7 - Fatal编程技术网

Python 从不需要的字符中去除数组

Python 从不需要的字符中去除数组,python,python-2.7,Python,Python 2.7,我有一个数组,如下所示: >>>tags = ['frankie', "franki's car", 'car'] 这里我只想将双引号替换为单引号,并从数组索引中删除撇号 因此,我希望有如下内容: >>> tags ['frankie', 'frankis car', 'car'] 有什么帮助吗?谢谢。您可以使用列表理解删除单引号: [t.replace("'", '') for t in tags] 双引号是Python如何表示字符串文字的人工制品;除

我有一个数组,如下所示:

>>>tags = ['frankie', "franki's car", 'car']
这里我只想将双引号替换为单引号,并从数组索引中删除
撇号

因此,我希望有如下内容:

>>> tags
['frankie', 'frankis car', 'car']

有什么帮助吗?谢谢。

您可以使用列表理解删除单引号:

[t.replace("'", '') for t in tags]
双引号是Python如何表示字符串文字的人工制品;除非字符串中有单引号,否则它将使用单引号,此时它将使用双引号来避免使用反斜杠转义该字符。如果您的字符串同时具有这两种类型,Python将再次使用单引号并转义值中的任何双引号:

>>> "No single quotes"
'No single quotes'
>>> "A single quote: '"
"A single quote: '"
>>> "Both types: \" and '"
'Both types: " and \''
演示:


如果有多个撇号怎么办?还有,你为什么想要这个?
>>> tags = ['frankie', "franki's car", 'car']
>>> [t.replace("'", '') for t in tags]
['frankie', 'frankis car', 'car']