튜플의 크기를 구해야 하는 경우 'sizeof' 방법을 사용할 수 있습니다.
아래는 동일한 데모입니다 -
예
import sys
tuple_1 = ("A", 1, "B", 2, "C", 3)
tuple_2 = ("Java", "Lee", "Code", "Mark", "John")
tuple_3 = ((1, "Bill"), ( 2, "Ant"), (3, "Fox"), (4, "Cheetah"))
print("The first tuple is :")
print(tuple_1)
print("The second tuple is :")
print(tuple_2)
print("The third tuple is :")
print(tuple_3)
print("Size of first tuple is : " + str(sys.getsizeof(tuple_1)) + " bytes")
print("Size of second tuple is : " + str(sys.getsizeof(tuple_2)) + " bytes")
print("Size of third tuple is: " + str(sys.getsizeof(tuple_3)) + " bytes") 출력
The first tuple is :
('A', 1, 'B', 2, 'C', 3)
The second tuple is :
('Java', 'Lee', 'Code', 'Mark', 'John')
The third tuple is :
((1, 'Bill'), (2, 'Ant'), (3, 'Fox'), (4, 'Cheetah'))
Size of first tuple is : 96 bytes
Size of second tuple is : 88 bytes
Size of third tuple is : 80 bytes 설명
-
필요한 패키지를 가져옵니다.
-
튜플이 정의되고 콘솔에 표시됩니다.
-
'sizeof' 메소드는 모든 튜플에서 호출되고 길이는 콘솔에 출력으로 표시됩니다.