data - MultiSet
multiset(λ€μ€μ§ν©)μ λμΌν μμκ° μ¬λ¬ λ² λνλ μ μλ μ§ν©μ μΌλ°νλ κ°λ μ λλ€. κ° μμκ° ν λ²λ§ λνλ μ μλ μΌλ° μ§ν©κ³Όλ λ¬λ¦¬, multisetμ κ° μμκ° μ‘΄μ¬νλ νμλ₯Ό μΆμ ν©λλ€.
π§ λ€μ€μ§ν©(multiset)μ μ£Όμ κ°λ
Elements: multisetμ μΆκ°ν μ μλ νλͺ©μ λλ€.
Multiplicity: multisetμ κ° μμκ° νμλλ νμμ λλ€
π§΅ νμ΄μ¬μμμ multiset ꡬν
νμ΄μ¬μλ set λΌλ μλ£νμ΄ μμ΅λλ€. setμ μ€λ³΅μ νμ©νμ§ μκ³ , set κ°μ²΄λ₯Ό μ§ν©μΌλ‘ μΈμνμ¬, μ§ν©μ κ΄λ ¨λ μ°μ°μ ν μ μμ΅λλ€. μλ₯Ό λ€μ΄, κ΅μ§ν©, ν©μ§ν© λ±μ μ°μ°μ ν μ μμ΅λλ€.
κ·Έλ¬λ multisetμ μ€λ³΅μ ν¬ν¨ν μ μκΈ° λλ¬Έμ set μλ£νμΌλ‘λ multisetμ ꡬνν μ μμ΅λλ€. λμ μ, ν€κ° μμμ΄κ³ κ°μ΄ κ°κ°μ κ°μμΈ dictionary μλ£νμ μ¬μ©νμ¬ κ΅¬νν μ μμΌλ©°, μ΄λ¬ν μ°μ°μ νΈνκ² ν΄μ€ μ μλ collections λͺ¨λμ Counter ν΄λμ€λ₯Ό μ¬μ©ν μ μμ΅λλ€.
π Counterλ dictionaryμ μλΈν΄λμ€μ λλ€.
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μ λν μ§ν© μ°μ°
from collections import Counter
multiset1 = Counter([1, 2, 2, 3, 3, 3])
multiset2 = Counter([2, 3, 3, 4, 4, 4, 4])
print("multiset1:", multiset1) # Counter({3: 3, 2: 2, 1: 1})
print("multiset2:", multiset2) # Counter({4: 4, 3: 2, 2: 1})
# Convert to list to see the elements with their counts
print("multiset1 as a list:", list(multiset1.elements())) # [1, 2, 2, 3, 3, 3]
print("multiset2 as a list:", list(multiset2.elements())) # [2, 3, 3, 4, 4, 4, 4]
# Calculate intersection
intersect_multiset = multiset1 & multiset2
print("Intersection:", intersect_multiset) # Counter({3: 2, 2: 1})
# Calculate union
union_multiset = multiset1 | multiset2
print("union:", union_multiset) # Counter({4: 4, 3: 3, 2: 2, 1: 1})
# Calculate difference
difference_multiset = multiset1 - multiset2
print("difference:", difference_multiset) # Counter({1: 1, 2: 1, 3: 1})multiset(also known as bags)λ μ, μ΄λμ μ¬μ©λλκ°
μμμ λΉλκ° μ€μν λ€μν μ¬λ리μ€μμ μ μ©ν©λλ€. κ³ μ ν μμλ§ μ μ₯νλ μ§ν©κ³Ό λ¬λ¦¬ λ€μ€ μ§ν©μ λμΌ μμκ° μ¬λ¬ λ² λ°μν μ μκ³ , κ·Έ μλ₯Ό μΆμ ν μ μμ΅λλ€. λ°λΌμ, μμμ λ°μ νμλ₯Ό μΆμ νλ μλ리μ€μ μ μ©ν©λλ€. μλ₯Ό λ€μ΄ ν μ€νΈ λΆμμμ λ¨μ΄ λΉλ μ, μ°½κ³ μμμ μ¬κ³ μ λλ μ΄λ²€νΈ λ°μ νμ μΆμ λ±μ΄ μμ΅λλ€.
Text Processing
λ¨μ΄ λΉλ μ: λ¬Έμμμ λ¨μ΄μ λΉλλ₯Ό κ³μ°νμ¬ ν μ€νΈλ₯Ό λΆμνκ±°λ μ€νΈμ κ°μ§νκ±°λ κ²μ μμ§μ© λ¬Έμ μμΈμ μμ±ν©λλ€.
μ λκ·Έλ¨(Anagram) νμ§: λ λ¬Έμμ΄μ λ¬Έμ μλ₯Ό λΉκ΅νμ¬ λ λ¬Έμμ΄μ΄ μλκ·Έλ¨μΈμ§ νμΈνλλ€.
π Anagram
μ΄λ ν λ¨μ΄μ λ¬Έμλ₯Ό μ¬λ°°μ΄νμ¬ λ€λ₯Έ λ»μ κ°μ§λ λ€λ₯Έ λ¨μ΄λ‘ λ°κΎΈλ κ²μ λ§νλ€.
Data Analysis
ν΅κ³ λΆμ: νμ€ν κ·Έλ¨μ΄λ λΉλ λΆν¬μ κ°μ ν΅κ³ λΆμμ μν΄ λ°μ΄ν° ν¬μΈνΈμ λ°μμ μΆμ ν©λλ€.
μ€λ¬Έ μ‘°μ¬ λΆμ: λμΌν μ§λ¬Έμ λν΄ μ¬λ¬ κ°μ μλ΅μ΄ κ°λ₯ν μ€λ¬Έμ‘°μ¬ λλ μ€λ¬Έμ§μμ μλ΅μ κ³μ°ν©λλ€.
Inventory Management
μ¬κ³ κ³μ°: κ° νλͺ©μ μλμ ν¬ν¨νμ¬ μμ μ μ¬κ³ νλͺ©μ μΆμ ν©λλ€
리μμ€ κ΄λ¦¬: μ¬μ© κ°λ₯ν 리μμ€μ μκ° μ€μν κ²μ κ°λ°κ³Ό κ°μ μ±μμ 리μμ€λ₯Ό κ΄λ¦¬ν©λλ€.
Event Counting
λ‘κΉ λ° λͺ¨λν°λ§: λͺ¨λν°λ§ λ° κ²½κ³ λͺ©μ μΌλ‘ μμ€ν λ‘κ·Έμμ μ΄λ²€νΈ λ°μ νμλ₯Ό κ³μ°ν©λλ€.
μ¬μ©μ νλ μΆμ : μ¬μ©μ νλμ μ΄ν΄νκ±°λ λΆμ λͺ©μ μΌλ‘ μ ν리μΌμ΄μ μμ μ¬μ©μ μμ λλ μ΄λ²€νΈλ₯Ό μΆμ ν©λλ€.
Last updated