๊ด€๋ฆฌ ๋ฉ”๋‰ด

๋ชฉ๋กํŒŒ์ด์ฌ ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜ (1)

Hey Tech

[ํŒŒ์ด์ฌ] Counter ํ•จ์ˆ˜: ๋ฆฌ์ŠคํŠธ ๋‚ด ์›์†Œ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ!(feat. collections ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ)

์•ˆ๋…•ํ•˜์„ธ์š”, ์˜ค๋Š˜์€ ํŒŒ์ด์ฌ Counter ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ ๋‚ด ์›์†Œ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์†Œ๊ฐœํ•ด ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ์†Œ์Šค์ฝ”๋“œ from collections import Counter # ๊ณผ์ผ ์ •๋ณด๋ฅผ ์ €์žฅํ•œ ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ arr = ['Apple', 'Banana', 'Orange', 'Apple', 'Grape', 'Orange', 'Water Melon'] cnt = Counter(arr) print(cnt['Apple']) # ์‚ฌ๊ณผ ๊ฐœ์ˆ˜ print(cnt['Orange']) # ์˜ค๋ Œ์ง€ ๊ฐœ์ˆ˜ print(dict(cnt)) # ๋”•์…”๋„ˆ๋ฆฌ ์ž๋ฃŒํ˜•์œผ๋กœ ์ถœ๋ ฅ ๊ฐ€์žฅ ๋จผ์ €, ๋ฆฌ์ŠคํŠธ ๋‚ด ์›์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๊ธฐ ์œ„ํ•ด์„œ๋Š” collections ํŒŒ์ด์ฌ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ Counter ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ ธ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ํ•จ์ˆ˜์— ๋ฆฌ..

SW ๊ฐœ๋ฐœ/Python 2021. 4. 23. 11:29