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

๋ชฉ๋กํŒŒ์ด์ฌ Counter (2)

Hey Tech

๋ฌธ์ž์—ด์ฒ˜๋ฆฌ #๋ฐฑ์ค€1157 #๋‹จ์–ด ๊ณต๋ถ€ | ํŒŒ์ด์ฌ ํ’€์ด

๐Ÿ“š ๋ฌธ์ œ ๋งํฌ: https://www.acmicpc.net/problem/1157 1157๋ฒˆ: ๋‹จ์–ด ๊ณต๋ถ€ ์•ŒํŒŒ๋ฒณ ๋Œ€์†Œ๋ฌธ์ž๋กœ ๋œ ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์ง€๋ฉด, ์ด ๋‹จ์–ด์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋œ ์•ŒํŒŒ๋ฒณ์ด ๋ฌด์—‡์ธ์ง€ ์•Œ์•„๋‚ด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋‹จ, ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋Š”๋‹ค. www.acmicpc.net ๐Ÿ’ก ์ ‘๊ทผ๋ฒ• ๋ฌธ์ž์—ด ๋‚ด ์•ŒํŒŒ๋ฒณ๋ณ„ ์ถœํ˜„ ํšŸ์ˆ˜๋ฅผ ์นด์šดํŠธํ•˜๊ธฐ ์œ„ํ•ด ํŒŒ์ด์ฌ collections ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‚ด Counter ๋ชจ๋“ˆ์„ ํ™œ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ตœ๋‹ค ๋นˆ์ถœ ์•ŒํŒŒ๋ฒณ์„ ์ถ”์ถœํ•˜๊ธฐ ์œ„ํ•ด Counter ๋ชจ๋“ˆ์—์„œ most_commons ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ถœํ˜„ ํšŸ์ˆ˜๊ฐ€ ๊ณต๋™ 1์œ„์ธ ์•ŒํŒŒ๋ฒณ์ด ์žˆ์„ ๊ฒฝ์šฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์ถœํ˜„ ๋นˆ๋„ 1์œ„์™€ 2์œ„๋ฅผ ๋น„๊ตํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๐Ÿ’ป ์ฝ”๋“œ # https://www.acmicpc.net/problem/1..

[ํŒŒ์ด์ฌ] 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