Operator.countOf()
se utiliza para contar el número de ocurrencias de b
en a
. Cuenta el número de ocurrencias de valor.
Sintaxis: Operator.countOf(freq = a, value = b)
Parámetros:
freq: puede ser una lista o cualquier otro tipo de datos que almacene valor
value: es un valor para el que tenemos que contar el número de ocurrenciasDevuelve: Recuento del número de ocurrencias de valor.
Ejemplo #1: Úselo Operator.countOf()
para contar el número de ocurrencias de un valor dado en una lista.
# Python program showing # use of countOf() function # in a list from operator import * arr = [ 1, 2, 3, 3, 3 ] arr1 = [ 'a', 'b', 'c', 'b', 'd' ] print("Number of occurrence of b in arr1 =", countOf(arr1, "b")) print("Number of occurrence of e in arr1 =", countOf(arr1, "e")) print("Number of occurrence of 3 in arr =", countOf(arr, 3))
Producción:
Number of occurrence of b in arr1 = 2 Number of occurrence of e in arr1 = 0 Number of occurrence of 3 in arr = 3
Ejemplo #2: Usar Operator.countOf()
para contar el número de ocurrencias de un valor dado en un diccionario.
# Python program showing # use of countOf() function # in a dictionary from operator import * d = {"score": 3, "score1": 1, "score2": 3, "score3": 1, "score4": 3} print("Number of occurrence of three in dict =",countOf(d.values(),3)) print("Number of occurrence of one in dict =",countOf(d.values(),1)) print("Number of occurrence of four in dict =",countOf(d.values(),4))
Producción :
Number of occurrence of three in dict = 3 Number of occurrence of one in dict = 2 Number of occurrence of four in dict = 0
Ejemplo #3: Usar Operator.countOf()
para contar el número de ocurrencias de un valor dado en una tupla.
# Python program showing # use of countOf() function # in a tuples from operator import * tup = ( 1, 2, 3, 3, 3 ) tup1 = ( 'a', 'b', 'c', 'b', 'd' ) print("Number of occurrence of b in tuple1 =",countOf(tup1,"b")) print("Number of occurrence of e in tuple1 =",countOf(tup1,"e")) print("Number of occurrence of 3 in tuple =",countOf(tup,3))
Producción :
Number of occurrence of b in tuple1 = 2 Number of occurrence of e in tuple1 = 0 Number of occurrence of 3 in tuple = 3
Publicación traducida automáticamente
Artículo escrito por garg_ak0109 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA