docs.python.org/ko/3/library/collections.html?highlight=collection#collections.Counter
collections — Container datatypes — Python 3.9.1 ๋ฌธ์
collections — Container datatypes Source code: Lib/collections/__init__.py This module implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple. namedtuple() factory f
docs.python.org
์ด๊ฑธ ์๊ฒ๋ ๊ฑด ๋ฐฑ์ค ์ ๋ ฌ > ํต๊ณํ ๋ฌธ์ ๋ฅผ ํ๋ค๊ฐ....ใ ใ
์ต๋น๊ฐ์ ๊ตฌํ๋ ค๊ณ count๋ฅผ ํ๋๋ ์๊ฐ์ด๊ณผ๊ฐ ๋์ ๋ค๋ฅธ์ฌ๋๋ค์ ์ด๋ป๊ฒ ํ์๋์ง ๊ถ๊ธํด์ ๊ฒ์ํ๋ค๊ฐ ์๊ฒ๋๋ค!
๊น๋จน๊ธฐ์ ์ ์ ๋ฆฌํด๋ฌ์ผ์ง..ใ ใ
Counter๋ฅผ ์ฐ๊ธฐ ์ํด์๋ from collections import Counter ๋ผ๊ณ ์ํฌํธ๋ฅผ ํด์ค์ผ ํ๋ค
๊ทธ๋ฆฌ๊ณ most_common์ ํด์ฃผ๋ฉด ๋ง์ด ๋ฑ์ฅํ ๊ฐ๋ถํฐ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํด์ค๋ค.
๋ฑ์ฅ ํ์๊ฐ ๊ฐ์ ์ ์์ ๊ฐ์ผ๋ก ์ ๋ ฌํ๋ค.
freqs = Counter(nums).most_common() #nums = [3,3,2,1,4,4,5]
print(freqs) #[(3, 2), (4, 2), (1, 1), (2, 1), (5, 1)]
'๐๊ณต๋ถ > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] in range ์ enumerate (0) | 2020.11.19 |
---|---|
[Python] for-else, while-else๋ฌธ (0) | 2020.11.18 |
[Python] ํด๋์ค์ ์์ฑ๊ณผ ์์ (0) | 2020.11.16 |
[Python] ์ ๋ ฅ๊ฐ์ ๊ฐฏ์๊ฐ ์ ํด์ง์ง ์์ ํจ์๋ฅผ ๋ง๋ค๊ธฐ (0) | 2020.11.14 |
[Python] ํ์ด์ฌ์์ ์์์ ๋ํ๋ด๊ธฐ (ํฌ๋งทํ ) (0) | 2020.11.13 |
๋๊ธ