在Python中查找 Set 的大小
Set 是一种无序的集合数据类型,它是可迭代的、可变的并且没有重复元素。 Python 的集合类表示集合的数学概念。集合的大小是指集合对象占用的内存量(以字节为单位)。在本文中,我们将学习各种获取Python集大小的方法。
1.使用getsizeof()
函数:
getsizeof()
函数属于 python 的 sys 模块。它已在以下示例中实现。
示例 1:
import sys
# sample Sets
Set1 = {"A", 1, "B", 2, "C", 3}
Set2 = {"Geek1", "Raju", "Geek2", "Nikhil", "Geek3", "Deepanshu"}
Set3 = {(1, "Lion"), ( 2, "Tiger"), (3, "Fox")}
# print the sizes of sample Sets
print("Size of Set1: " + str(sys.getsizeof(Set1)) + "bytes")
print("Size of Set2: " + str(sys.getsizeof(Set2)) + "bytes")
print("Size of Set3: " + str(sys.getsizeof(Set3)) + "bytes")
输出:
Size of Set1: 736bytes
Size of Set2: 736bytes
Size of Set3: 224bytes
注意: sys.getsizeof()
函数包括边际空间使用量,其中包括对象的垃圾收集开销。这意味着除了正在使用的空间的垃圾收集开销之外,它还返回对象占用的总空间。
1.使用内置__sizeof__()
方法:
Python还有一个内置的 __sizeof__() 方法来确定对象的空间分配,而无需任何额外的垃圾值。它已在以下示例中实现。
示例 2:
import sys
# sample Sets
Set1 = {"A", 1, "B", 2, "C", 3}
Set2 = {"Geek1", "Raju", "Geek2", "Nikhil", "Geek3", "Deepanshu"}
Set3 = {(1, "Lion"), ( 2, "Tiger"), (3, "Fox")}
# print the sizes of sample Sets
print("Size of Set1: " + str(Set1.__sizeof__()) + "bytes")
print("Size of Set2: " + str(Set2.__sizeof__()) + "bytes")
print("Size of Set3: " + str(Set3.__sizeof__()) + "bytes")
输出:
Size of Set1: 712bytes
Size of Set2: 712bytes
Size of Set3: 200bytes