๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Counter2

[Python] collections ๋ชจ๋“ˆ Python์˜ ๋ชจ๋“ˆ ์ค‘ collections์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜๋‹ค. ์ด๋ฅผ ์‚ฌ์šฉํ•ด์„œ List, Tuple, Dict๋ฅผ ํŽธ๋ฆฌํ•˜๊ฒŒ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋‹ค. ๊ฐ์ฒด ์ข…๋ฅ˜ deque, Counter, OrderedDcit, defaultdict, namedtuple deque - stack๊ณผ queue๋ฅผ ์ง€์› - List์— ๋น„ํ•ด์„œ ํšจ์œจ์ ์ธ(๋น ๋ฅธ) ์ž๋ฃŒ ์ €์žฅ ๋ฐฉ์‹ ์ง€์›! from collections import deque appendleft() : ๋ฆฌ์ŠคํŠธ์˜ ์•ž์— ์š”์†Œ ์ถ”๊ฐ€ deque_list = deque() # ๊ฐ์ฒด ์„ ์–ธ for i in range(5): deque_list.append(i)# [0,1,2,3,4] deque_list.appendleft(10)# ์•ž์— ์ถ”๊ฐ€ deque_list >>> deque([10, 0,.. 2023. 1. 5.
Python_๋‘ ๊ฐœ์˜ ๋”•์…”๋„ˆ๋ฆฌ ํ•ฉ์น˜๊ธฐ! ํŒŒ์ด์ฌ์˜ dictionary๋ฅผ ๋‹ค๋ฃจ๋Š” ๋„์ค‘ 2๊ฐœ์˜ dictionary๋ฅผ ํ•ฉ์น˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ํ•ด๋ณด์•˜๋‹ค! 1. key๊ฐ’์ด ๊ฒน์น˜์ง€ ์•Š์„ ๋•Œ dict_1 = {'A':1, 'B':2, 'C':3} dict_2 = {'D':1, 'E':2, 'F':3} - update() result = dict_1 result.update(dict_2) >>> {'A': 1, 'B': 2, 'C': 3, 'D': 1, 'E': 2, 'F': 3} dictnary์˜ ๋ฉ”์†Œ๋“œ ์ค‘ ํ•˜๋‚˜์ธ update๋ฅผ ์ด์šฉํ•˜๋ฉด ์œ„์™€ ๊ฐ™์ด ๋‘ ๊ฐœ๋ฅผ ํ•ฉ์น  ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ, key๊ฐ’์ด ๋ชจ๋‘ ๊ฒน์น˜์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค๋Š” ์ „์ œ์กฐ๊ฑด์ด ์žˆ๋‹ค. ๋งŒ์•ฝ ๊ฒน์นœ๋‹ค๋ฉด, ์ค‘๋ณต๋œ ๊ธฐ์กด key๊ฐ’์€ update์˜ ์ธ์ˆ˜๋กœ ๋„ฃ์–ด์ค€ ๊ฐ’์œผ๋กœ ๋Œ€์ฒด๋œ๋‹ค. 2. key๊ฐ’์ด ๊ฒน์น  ๋•Œ ๋ฐ์ดํ„ฐ๋ฅผ ๋”•.. 2022. 1. 19.
๋ฐ˜์‘ํ˜•