2차전지주 TOP 10 핵심정리
https://chosunstock.com/2%ec%b0%a8%ec%a0%84%ec%a7%80%ec%a3%bc-top-10-%ed%95%b5%ec%8b%ac%ec%a0%95%eb%a6%ac?feed_id=871&_unique_id=65d37dbf8927f
Counter(list)
다음 목록이 있다고 가정합니다.
list1 = ['x','y','z','x','x','x','y', 'z']
목록에는 요소 x, y 및 z가 있습니다. 이 목록에서 Counter를 사용하면 x, y 및 z가 몇 번 있는지 계산합니다. list1에서 counter를 사용하는 경우 출력은 다음과 같아야 합니다.
Counter({'x': 4, 'y': 2, 'z': 2})
따라서 x의 개수는 4, y는 2, z는 2입니다. Counter를 사용하려면 아래 주어진 예제와 같이 먼저 가져와야 합니다.
from collections import Counter
다음은 Counter 모듈의 작동을 보여주는 간단한 예입니다.
from collections import Counter
list1 = ['x','y','z','x','x','x','y', 'z']
print(Counter(list1))
산출:
Counter({'x': 4, 'y': 2, 'z': 2})
from collections import Counter
my_str = "Welcome to Guru99 Tutorials!"
print(Counter(my_str))
산출:
Counter({'o': 3, ' ': 3, 'u': 3, 'e': 2, 'l': 2, 't': 2, 'r': 2, '9': 2, 'W': 1,
'c': 1, 'm': 1, 'G': 1, 'T': 1, 'i': 1, 'a': 1, 's': 1, '!': 1})
from collections import Counter
list1 = ['x','y','z','x','x','x','y','z']
print(Counter(list1))
산출:
Counter({'x': 4, 'y': 2, 'z': 2})
from collections import Counter
dict1 = {'x': 4, 'y': 2, 'z': 2, 'z': 2}
print(Counter(dict1))
산출:
Counter({'x': 4, 'y': 2, 'z': 2})
from collections import Counter
tuple1 = ('x','y','z','x','x','x','y','z')
print(Counter(tuple1))
산출:
Counter({'x': 4, 'y': 2, 'z': 2})
from collections import Counter
print(Counter("Welcome to Guru99 Tutorials!")) #using string
print(Counter(['x','y','z','x','x','x','y', 'z'])) #using list
print(Counter({'x': 4, 'y': 2, 'z': 2})) #using dictionary
print(Counter(('x','y','z','x','x','x','y', 'z'))) #using tuple
아래와 같이 빈 카운터를 초기화할 수도 있습니다.
from collections import Counter
_count = Counter()
_count.update('Welcome to Guru99 Tutorials!')
최종 코드는 다음과 같습니다.
from collections import Counter
_count = Counter()
_count.update('Welcome to Guru99 Tutorials!')
print(_count)
출력은 다음과 같습니다.
Counter({'o': 3, ' ': 3, 'u': 3, 'e': 2, 'l': 2, 't': 2, 'r': 2, '9': 2, 'W': 1,
'c': 1, 'm': 1, 'G': 1, 'T': 1, 'i': 1, 'a': 1, 's': 1, '!': 1})
from collections import Counter
_count = Counter()
_count.update('Welcome to Guru99 Tutorials!')
print('%s : %d' % ('u', _count['u']))
print('\n')
for char in 'Guru':
print('%s : %d' % (char, _count[char]))
산출:
u : 3
G : 1
u : 3
r : 2
u : 3
from collections import Counter
dict1 = {'x': 4, 'y': 2, 'z': 2}
del dict1["x"]
print(Counter(dict1))
산출:
Counter({'y': 2, 'z': 2})
from collections import Counter
counter1 = Counter({'x': 4, 'y': 2, 'z': -2})
counter2 = Counter({'x1': -12, 'y': 5, 'z':4 })
#Addition
counter3 = counter1 + counter2 # only the values that are positive will be returned.
print(counter3)
#Subtraction
counter4 = counter1 - counter2 # all -ve numbers are excluded.For example z will be z = -2-4=-6, since it is -ve value it is not shown in the output
print(counter4)
#Intersection
counter5 = counter1 & counter2 # it will give all common positive minimum values from counter1 and counter2
print(counter5)
#Union
counter6 = counter1 | counter2 # it will give positive max values from counter1 and counter2
print(counter6)
산출:
Counter({'y': 7, 'x': 4, 'z': 2})
Counter({'x1': 12, 'x': 4})
Counter({'y': 2})
Counter({'y': 5, 'x': 4, 'z': 4})
from collections import Counter
counter1 = Counter({'x': 5, 'y': 2, 'z': -2, 'x1':0})
_elements = counter1.elements() # will give you all elements with positive value and count>0
for a in _elements:
print(a)
산출:
x
x
x
x
x
y
y
from collections import Counter
counter1 = Counter({'x': 5, 'y': 12, 'z': -2, 'x1':0})
common_element = counter1.most_common(2) # The dictionary will be sorted as per the most common element first followed by next.
print(common_element)
common_element1 = counter1.most_common() # if the value is not given to most_common , it will sort the dictionary and give the most common elements from the start.The last element will be the least common element.
print(common_element1)
산출:
[('y', 12), ('x', 5)]
[('y', 12), ('x', 5), ('x1', 0), ('z', -2)]
from collections import Counter
counter1 = Counter({'x': 5, 'y': 12, 'z': -2, 'x1':0})
counter2 = Counter({'x': 2, 'y':5})
counter1.subtract(counter2)
print(counter1)
산출:
Counter({'y': 7, 'x': 3, 'x1': 0, 'z': -2})
from collections import Counter
counter1 = Counter({'x': 5, 'y': 12, 'z': -2, 'x1':0})
counter2 = Counter({'x': 2, 'y':5})
counter1.update(counter2)
print(counter1)
산출:
Counter({'y': 17, 'x': 7, 'x1': 0, 'z': -2})
from collections import Counter
counter1 = Counter({'x': 5, 'y': 12, 'z': -2, 'x1':0})
counter1['y'] = 20
print(counter1)
출력: 실행 후 y 개수가 12에서 20으로 변경된 것을 볼 수 있습니다.
Counter({'y': 20, 'x': 5, 'x1': 0, 'z': -2})
from collections import Counter
counter1 = Counter({'x': 5, 'y': 12, 'z': -2, 'x1':0})
print(counter1['y']) # this will give you the count of element 'y'
산출:
12
요소 수를 설정하려면 다음과 같이 할 수 있습니다.
from collections import Counter
counter1 = Counter({'x': 5, 'y': 12, 'z': -2, 'x1':0})
print(counter1['y'])
counter1['y'] = 20
counter1['y1'] = 10
print(counter1)
산출:
12
Counter({'y': 20, 'y1': 10, 'x': 5, 'x1': 0, 'z': -2})