📜  Python|计数器对象 |元素()

📅  最后修改于: 2022-05-13 01:54:43.483000             🧑  作者: Mango

Python|计数器对象 |元素()

计数器类是 Python3 中的集合模块提供的一种特殊类型的对象数据集。 Collections 模块为用户提供了专门的容器数据类型,因此,提供了 Python 通用内置函数(如字典、列表和元组)的替代方案。

Counter是一个子类,用于对可散列对象进行计数。它在调用时隐式创建一个可迭代的哈希表。

elements()是 Counter 类的函数之一,在 Counter 对象上调用时将返回 Counter 对象中所有已知元素的 itertool。

代码 #1: elements() 在简单数据容器上的工作

Python3
# import counter class from collections module
from collections import Counter
 
# Creation of a Counter Class object using
# string as an iterable data container
x = Counter("geeksforgeeks")
 
# printing the elements of counter object
for i in x.elements():
    print ( i, end = " ")


Python3
# import counter class from collections module
from collections import Counter
 
# Creation of a Counter Class object using
# a string as an iterable data container
# Example - 1
a = Counter("geeksforgeeks")
 
# Elements of counter object
for i in a.elements():
    print ( i, end = " ")
print()
     
# Example - 2
b = Counter({'geeks' : 4, 'for' : 1,
            'gfg' : 2, 'python' : 3})
 
for i in b.elements():
    print ( i, end = " ")
print()
 
# Example - 3
c = Counter([1, 2, 21, 12, 2, 44, 5,
              13, 15, 5, 19, 21, 5])
 
for i in c.elements():
    print ( i, end = " ")
print()             
               
# Example - 4
d = Counter( a = 2, b = 3, c = 6, d = 1, e = 5)
 
for i in d.elements():
    print ( i, end = " ")


Python3
# import Counter from collections
from collections import Counter
 
# creating a raw data-set
x = Counter ("geeksforgeeks")
 
# will return a itertools chain object
# which is basically a pseudo iterable container whose
# elements can be used when called with a iterable tool
print(x.elements())


Python3
# import Counter from collections
from collections import Counter
 
# creating a raw data-set using keyword arguments
x = Counter (a = 2, x = 3, b = 3, z = 1, y = 5, c = 0, d = -3)
 
# printing out the elements
for i in x.elements():
    print( "% s : % s" % (i, x[i]), end ="\n")


输出:

g g e e e e k k s s f o r 

代码 #2:具有不同数据容器的各种计数器对象上的元素

Python3

# import counter class from collections module
from collections import Counter
 
# Creation of a Counter Class object using
# a string as an iterable data container
# Example - 1
a = Counter("geeksforgeeks")
 
# Elements of counter object
for i in a.elements():
    print ( i, end = " ")
print()
     
# Example - 2
b = Counter({'geeks' : 4, 'for' : 1,
            'gfg' : 2, 'python' : 3})
 
for i in b.elements():
    print ( i, end = " ")
print()
 
# Example - 3
c = Counter([1, 2, 21, 12, 2, 44, 5,
              13, 15, 5, 19, 21, 5])
 
for i in c.elements():
    print ( i, end = " ")
print()             
               
# Example - 4
d = Counter( a = 2, b = 3, c = 6, d = 1, e = 5)
 
for i in d.elements():
    print ( i, end = " ")

输出:

g g e e e e k k s s f o r 
geeks geeks geeks geeks for gfg gfg python python python 
1 2 2 21 21 12 44 5 5 5 13 15 19 
a a b b b c c c c c c d e e e e e 

代码#3:演示elements()在直接打印时返回什么

Python3

# import Counter from collections
from collections import Counter
 
# creating a raw data-set
x = Counter ("geeksforgeeks")
 
# will return a itertools chain object
# which is basically a pseudo iterable container whose
# elements can be used when called with a iterable tool
print(x.elements())

输出:

itertools.chain object at 0x037209F0

代码 #4:当 Counter 中项目的计数被初始化为负值或零时。

Python3

# import Counter from collections
from collections import Counter
 
# creating a raw data-set using keyword arguments
x = Counter (a = 2, x = 3, b = 3, z = 1, y = 5, c = 0, d = -3)
 
# printing out the elements
for i in x.elements():
    print( "% s : % s" % (i, x[i]), end ="\n")

输出:

a : 2
a : 2
x : 3
x : 3
x : 3
b : 3
b : 3
b : 3
z : 1
y : 5
y : 5
y : 5
y : 5
y : 5

注意:我们可以从输出中推断出值小于 1 的项会被 elements() 省略。

应用:
计数器对象及其功能共同用于处理大量数据。我们可以看到 Counter() 为使用它调用的数据容器创建了一个哈希映射,这比手动处理元素非常有用。它是一种非常强大的处理和功能工具之一,甚至函数处理广泛的数据。