python去掉列表中所有元素的前后单引号
时间: 2023-12-02 18:03:29 浏览: 30
可以使用列表推导式和字符串的`strip()`方法来去掉列表中所有元素的前后单引号。以下是示例代码:
```python
original_list = ["'apple'", "'banana'", "'cherry'"]
stripped_list = [elem.strip("'") for elem in original_list]
print(stripped_list)
```
输出结果将会是:
```
['apple', 'banana', 'cherry']
```
这样就去掉了列表中所有元素的前后单引号。
相关问题
python去掉列表中所有元素的单引号
你可以使用列表解析 (list comprehension) 来去掉列表中所有元素的单引号。以下是一个示例代码:
```python
my_list = ["'apple'", "'banana'", "'cherry'"]
new_list = [item.strip("'") for item in my_list]
print(new_list)
```
输出结果将是:
```
['apple', 'banana', 'cherry']
```
在上述代码中,`strip("'")` 方法用于去掉字符串中的单引号。通过列表解析,我们对列表中的每个元素都应用了这个方法,从而得到了一个新的列表 `new_list`,其中所有元素都不包含单引号。
python如何去掉列表中元素的单引号
您好!谢谢您的提问。针对您的问题,请使用以下代码:
```python
my_list = ['apple', 'banana', 'orange']
new_list = [i.strip("'") for i in my_list]
print(new_list)
```
这里使用列表推导式去除了每个元素的单引号,输出结果为 `['apple', 'banana', 'orange']`。希望能帮助您解决问题!