data - MultiSet
π§ λ€μ€μ§ν©(multiset)μ μ£Όμ κ°λ
π§΅ νμ΄μ¬μμμ multiset ꡬν
Counter ν΄λμ€λ₯Ό νμ©νμ¬ muliset ꡬν λ° κ³μ°
multiset ꡬν
from collections import Counter
# Creating a multiset
multiset = Counter()
# Adding elements to the multiset
multiset.update([1, 2, 2, 3, 3, 3, 3])
print("multiset:", multiset) # Counter({3: 4, 2: 2, 1: 1})
# Adding more elements
multiset.update([2, 2, 4])
print("multiset:", multiset) # Counter({2: 4, 3: 4, 1: 1, 4: 1})
# Accessing the count of a specific element
print("Count of element 3:", multiset[3]) # 4
# # Removing elements
multiset.subtract([2, 2])
print("Multiset after removal:", multiset) # Counter({3: 4, 2: 2, 1: 1, 4: 1})
# Converting to a list (elements repeated according to their counts)
multiset_as_list = list(multiset.elements())
print("Multiset as a list:", multiset_as_list) # [1, 2, 2, 3, 3, 3, 3, 4]
# Checking if an element is in the multiset
print("Is 4 in the multiset?", 4 in multiset) # True
print("Is 5 in the multiset?", 4 in multiset) # False
# Removing an element completely
del multiset[4]
print("Multiset after deleting element 4:", multiset) # Counter({3: 4, 2: 2, 1: 1})multisetμ λν μ§ν© μ°μ°
multiset(also known as bags)λ μ, μ΄λμ μ¬μ©λλκ°
Text Processing
Data Analysis
Inventory Management
Event Counting
Last updated