python去掉数组中的数值双引号
时间: 2023-08-11 08:04:28 浏览: 374
在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']`。
这样,你就可以在数组中去掉每个元素值的双引号了。
相关问题
怎么去掉python数组中的双引号
在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
arr = [1, 2, 3, 4, 5]
values = [x for x in arr]
print(values)
```
这将输出:
```
[1, 2, 3, 4, 5]
```
如果数组中有重复的数值,也可以使用集合来去除重复的元素,例如:
```python
arr = [1, 2, 3, 2, 4, 5, 4]
values = list(set(arr))
print(values)
```
这将输出:
```
[1, 2, 3, 4, 5]
```