python查找特定元素
In this article, we would learn about arrays and how to code for an array question asked? So starting off we would first learn about arrays i.e. what an array is?
在本文中,我们将学习数组以及如何为所问的数组问题编码? 因此,我们首先要了解数组,即数组是什么?
ARRAY: An array can be defined as a container object that is used to hold a fixed number of values of a single type of data. The main purpose of an array is to store multiple items of the same type together.
ARRAY:数组可以定义为容器对象,用于容纳固定数量的单一类型数据的值。 数组的主要目的是将相同类型的多个项目存储在一起。
![arrays in python](https://i-blog.csdnimg.cn/blog_migrate/378a7ddb28f1e968e0d475cab84031cb.png)
Image source: https://cdncontribute.geeksforgeeks.org/wp-content/uploads/array-2.png
图片来源:https://cdncontribute.geeksforgeeks.org/wp-content/uploads/array-2.png
This is the simple idea that you need to understand about the array, now starting with the simple question.
这是您需要了解的关于数组的简单概念,现在从简单问题开始。
Question:
题:
Suppose you are given with an array that contains ints. Your task is to return the number of 3 in the array.
假设给定一个包含整数的数组。 您的任务是返回数组中的3。
Example:
例:
Count3([1, 2, 3]) = 1
Count3([1, 3, 3]) = 2
Count9([1, 3, 9, 3, 3]) = 3
Solution:
解:
Here we have to again consider a variable which is initially equal to zero to keep the count of a number of 3 and also our function is only defined for nums as our array would only contain integers as mentioned earlier.
在这里,我们必须再次考虑一个初始为零的变量,以保持3的计数,并且我们的函数仅针对nums定义,因为我们的数组将仅包含整数(如前所述)。
Code:
码:
def Count3(nums):
count = 0
for num in nums:
if num == 3:
count = count + 1
return count
print (Count3([1,2,3,4,3,3,3,]))
Output
输出量
4
python查找特定元素