怎么去掉python数组中的双引号
时间: 2023-05-30 19:06:04 浏览: 278
在Python中,如果数组中的元素都是字符串类型,并且每个字符串都被双引号包围,可以使用以下方法去掉双引号:
1. 使用列表解析式
```python
original_list = ["\"apple\"", "\"banana\"", "\"orange\""]
new_list = [element.replace("\"", "") for element in original_list]
print(new_list)
```
输出:
```
['apple', 'banana', 'orange']
```
2. 使用map()函数和lambda表达式
```python
original_list = ["\"apple\"", "\"banana\"", "\"orange\""]
new_list = list(map(lambda x: x.replace("\"", ""), original_list))
print(new_list)
```
输出:
```
['apple', 'banana', 'orange']
```
相关问题
python去掉数组中的数值双引号
在Python中,可以使用`strip()`方法去掉字符串两侧的引号。如果要去掉数组中每个元素值的双引号,可以使用列表推导式来实现。以下是一个示例代码:
```python
# 带有双引号的字符串数组
array = ['"value1"', '"value2"', '"value3"']
# 去掉数组中每个元素值的双引号
array_without_quotes = [value.strip('"') for value in array]
# 打印结果
print(array_without_quotes)
```
在上面的代码中,我们定义了一个带有双引号的字符串数组`array`。然后,使用列表推导式`[value.strip('"') for value in array]`,遍历数组中的每个元素,使用`strip('"')`方法去掉每个元素值的双引号。最后,将处理后的结果存储在`array_without_quotes`列表中。
运行上述代码后,将得到一个不带双引号的数组`array_without_quotes`。输出结果为:`['value1', 'value2', 'value3']`。
这样,你就可以在数组中去掉每个元素值的双引号了。
如何用数组去除隐藏的双引号
你可以使用循环遍历数组中的每一个元素,然后使用字符串的replace方法去除其中的双引号。具体实现如下:
```python
arr = ['"hello"', 'world', '"foo"', '"bar"']
for i in range(len(arr)):
arr[i] = arr[i].replace('"', '')
print(arr)
```
运行结果为:
```
['hello', 'world', 'foo', 'bar']
```
这样就可以去除数组中所有元素中的双引号了。