fInd能不能使用数组 python
时间: 2023-05-14 18:06:55 浏览: 56
可以使用数组来实现 fInd 函数,以下是一个示例代码:
def fInd(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
其中,arr 是一个数组,target 是要查找的目标元素。这个函数会遍历整个数组,如果找到目标元素,就返回它的下标,否则返回 -1。在 Python 中,数组可以使用列表来表示。
相关问题
python find 数组
在 Python 中,可以使用 `index()` 方法来查找一个元素在列表中的位置,例如:
```python
my_list = [1, 2, 3, 4, 5]
index = my_list.index(3)
print(index) # 输出 2
```
如果要查找多个元素在列表中的位置,可以使用列表推导式来实现,例如:
```python
my_list = [1, 2, 3, 4, 5]
indexes = [i for i in range(len(my_list)) if my_list[i] in [3, 5]]
print(indexes) # 输出 [2, 4]
```
另外,如果要判断一个元素是否在列表中,可以使用 `in` 操作符来实现,例如:
```python
my_list = [1, 2, 3, 4, 5]
if 3 in my_list:
print("3 在列表中")
else:
print("3 不在列表中")
```
python 中 find 数组
在 Python 中,可以使用 `list` 类型的变量来存储数组,然后使用 `index()` 方法来查找特定元素在数组中的位置。例如:
```python
my_array = [1, 2, 3, 4, 5]
print(my_array.index(3)) # 输出 2,因为 3 在数组中的索引位置为 2
```
另外,也可以使用 `in` 关键字来检查数组中是否包含特定元素。例如:
```python
my_array = [1, 2, 3, 4, 5]
if 3 in my_array:
print("3 存在于数组中")
else:
print("3 不存在于数组中")
```
输出结果为:“3 存在于数组中”。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)