๐๊ณต๋ถ/Python9 [Python] collections.Counter 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.p.. 2020. 12. 29. [Python] in range ์ enumerate ๋ค์ด๊ฐ๊ธฐ ์์... ๐ iterable iterable ๊ฐ์ฒด = ๋ฐ๋ณต ๊ฐ๋ฅํ ๊ฐ์ฒด (list, dict, set, str, bytes, tuple, range) ๐ iterator iterator ๊ฐ์ฒด = ๊ฐ์ ์ฐจ๋ก๋๋ก ๊บผ๋ผ ์ ์๋ ๊ฐ์ฒด next() ๋ฉ์๋๋ก ๋ฐ์ดํฐ๋ฅผ ์์ฐจ์ ์ผ๋ก ํธ์ถ ๊ฐ๋ฅํ ๊ฐ์ฒด์ด๋ค iterable๊ฐ์ฒด != iterator ๊ฐ์ฒด (๋ฐ๋์ ๊ฐ์๊ฑด ์๋๋ค!!) list๋ iterable์ด์ง๋ง next()๊ฐ ๋จน์ง์๋๋ค. iter()๋ผ๋ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ list๋ฅผ list iterator ํ์ ์ผ๋ก ๋ณ๊ฒฝํ ๋ค์์๋ next()๋ฅผ ์ธ ์ ์๋ค. ํ์ง๋ง.. ๊ตณ์ด iter()๋ฅผ ์ฐ์ง ์์๋ for๋ฌธ์ผ๋ก ์์ฐจ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ ์ด์ ๋! ํ์ด์ฌ ๋ด๋ถ์์ ์์๋ก list๋ฅผ iterator๋ก ์๋๋ณํ ํ๊ธฐ ๋.. 2020. 11. 19. [Python] for-else, while-else๋ฌธ ์ค๋ ๋ฌธ์ ์๊ธฐ๋ฅผํ๋ค๊ฐ for-else์๊ธฐ๊ฐ ๋์์.... ๊น๋จน๊ธฐ์ ์ ์ ๋ฆฌ๋ฅผ ํ๋ค ๋ฐ๋ณต๋ฌธ(for, while)์ ๋ค์ else๋ฅผ ๋ถ์ด๋ฉด ๋ฐ๋ณต๋ฌธ์ด breakํ๊ฒ ์๋๊ณ ์ฒ์๋ถํฐ ์กฐ๊ฑด ๋๊น์ง ๋ค ๋๊ณ ๋ ํ์! (์ ์์ข ๋ฃ) else๋ฅผ ํ์ ์คํ์ ํจ for _ in range(n): r = random.randint(10, 99) print(r, end=' ') if r == 13: print('\n ํ๋ก๊ทธ๋จ์ ์ข ๋ฃํจ') break else: print('\n ๋์ ์์ฑ์ ์ข ๋ฃํจ') ๋ง์ฝ 13์ด ๋์ค๋ฉด breakํ๊ธฐ ๋๋ฌธ์ '๋์์์ฑ์ ์ข ๋ฃํจ' ์ ๋์ค์ง ์๋๋ค. 13์ด ์๋์ค๋ฉด n๋งํผ ๋๊ณ for๊ฐ ๋๋ ํ, else๋ฅผ ํ์ '๋์ ์์ฑ์ ์ข ๋ฃํจ'์ด ์ถ๋ ฅ๋๋ค........๊ณ ํ๋ค. 2020. 11. 18. [Python] ํด๋์ค์ ์์ฑ๊ณผ ์์ ์ด ์์์ฝ๋๋ฅผ ๊ฐ์ง๊ณ ์ ๋ฆฌ๋ฅผ ํ ๊ฒ์ด๋ค. class FourCal: # def setData(self, input1, input2): # self.num1 = input1 # self.num2 = input2 # print(self.num1, self.num2) def __init__(self, input1, input2): self.num1 = input1 self.num2 = input2 print(self.num1, self.num2) def add(self): print('ํฉ :', self.num1+self.num2) def sub(self): print('์ฐจ :', self.num1-self.num2) def prod(self): print('๊ณฑ :', self.num1*self.num2) d.. 2020. 11. 16. [Python] ์ ๋ ฅ๊ฐ์ ๊ฐฏ์๊ฐ ์ ํด์ง์ง ์์ ํจ์๋ฅผ ๋ง๋ค๊ธฐ ๋ฐฉ๊ธ ์ ํํฌํ์ด์ฌ ์ฐ์ต๋ฌธ์ ํ๋ค๊ฐ ํท๊ฐ๋ฆฌ๋๊ฒ ์์ด์ ์๊ธฐ์ ์ ์ ๋ฆฌํ๊ธฐ๋ก๐ข ๋ฌธ์ ) ์ ๋ ฅ์ผ๋ก ๋ค์ด์ค๋ ๋ชจ๋ ์์ ํ๊ท ๊ฐ์ ๊ณ์ฐํ๋ ํจ์ ์์ฑ. (๋จ! ์ ๋ ฅ์ผ๋ก ๋ค์ด์ค๋ ์์ ๊ฐ์๋ ์ ํด์ ธ์์ง ์์) def avg_all(*args): result = 0 sum = 0 for num in args: sum += num result = sum / len(args) print(result) ์ด๋ฐ ๊ฒฝ์ฐ์๋ ์๋ฐ์์ผ๋ก args์์ * ๋ฅผ ๋ถ์ฌ์ฃผ๋ฉด ๋๋ค! * ๋ฅผ ๋ถ์ฌ์ฃผ๋ฉด ์ ๋ ฅ๊ฐ์ ๋ชจ๋ ๋ชจ์์ ํํ๋ก ๋ง๋ค์ด์ค. ๊ทธ๋ฆฌ๊ณ args ๋์ ์ nums, numbers, potato, ๋ฑ๋ฑ ์๋ฌด๊ฑฐ๋ ์จ๋ ๋จ๐ฅ (์ด๋ฆ์ ๋ด๊ฐ ์ง์ด์ฃผ๊ธฐ ๋๋ฆ!) ์ด๋ ๊ฒ ์คํํ์ฌ 4.5๋ผ๋ ๊ฒฐ๊ณผ๋ฅผ ์ป์๋ค๐ค 2020. 11. 14. ์ด์ 1 2 ๋ค์