python查找特定元素_Python程序查找数组中特定数字的出现

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


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


翻译自: https://www.includehelp.com/python/program-to-find-the-occurrence-of-a-particular-number-in-an-array.aspx

python查找特定元素

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值