๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป IT·๊ธฐ์ˆ ·ํ†ต๊ณ„

[Snippet] ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” List, Dictionary ๊ธฐ๋Šฅ

by nowgeun 2023. 2. 9.
728x90

์ฝ”๋“œ ์ž‘์„ฑ์„ ํ•˜๋‹ค ๋ณด๋ฉด ํ•ญ์ƒ ์žŠ์–ด๋ฒ„๋ฆฌ๊ฑฐ๋‚˜ ๊ธฐ์–ต์ด ์ž˜ ๋‚˜์ง€ ์•Š๋Š” ๊ธฐ๋Šฅ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋‚˜๋ผ ๋ง์„ ํ•  ๋•Œ๋„ ๊ฐ€๋” ๋‹จ์–ด๊ฐ€ ์ƒ๊ฐ์ด ์•ˆ ๋‚  ๋•Œ์ฒ˜๋Ÿผ ๋ง์ด์ฃ  ใ…Žใ…Ž ๊ดœํžˆ ์ฝ”๋”ฉํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์ด ๊ตฌ๊ธ€์„ "์‹ "์ด๋ผ๊ณ  ํ•˜๋Š” ๊ฒŒ ์•„๋‹™๋‹ˆ๋‹ค. ChatGPT๊ฐ€ ๋งˆ์ดํฌ๋กœ์†Œํ”„ํŠธ์˜ ๊ฒ€์ƒ‰์—”์ง„ Bing์— ์ž˜ ํƒ‘์žฌ๋งŒ ๋œ๋‹ค๋ฉด ์•ž์œผ๋กœ ๊ฐœ๋ฐœ ์ƒ์‚ฐ์„ฑ์€ ๋”๋”์šฑ ํ–ฅ์ƒ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

 

์–˜๊ธฐ๊ฐ€ ์กฐ๊ธˆ ์ƒœ๋Š”๋ฐ, ํšŒ์‚ฌ์—์„œ๋‚˜ ์ทจ๋ฏธ๋กœ๋‚˜ ์ฝ”๋”ฉ์„ ํ•  ๋•Œ ์ž์ฃผ ๊นŒ๋จน๋Š” ๊ธฐ๋Šฅ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. List์•ˆ์— ์žˆ๋Š” List๋ฅผ ํ•˜๋‚˜์˜ List๋กœ ๋ณ‘ํ•ฉํ•œ๋‹ค๋˜์ง€, Dictionary ์•ˆ์— ์žˆ๋Š” ํ‚ค ๊ฐ’๋“ค์„ ์–ด๋–ป๊ฒŒ ์ •๋ ฌํ•˜๋Š”์ง€ ๋“ฑ ์ž์ฃผ ์‚ฌ์šฉํ•˜์ง€๋งŒ ๋งค๋ฒˆ ์™ธ์šฐ๋Š” ๋ฐ๋Š” ์‹คํŒจํ•˜๋Š” ์ฝ”๋“œ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋ฒˆ ํฌ์ŠคํŠธ์—์„œ๋Š” ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” List์™€ Dictionary ๊ธฐ๋Šฅ๋“ค์— ๋Œ€ํ•ด ์ •๋ฆฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ ์Šค๋‹ˆํŽซ!

1. ๋ฆฌ์ŠคํŠธ ์•ˆ์— ๋ฆฌ์ŠคํŠธ๋“ค์„ ํ•˜๋‚˜์˜ ๋ฆฌ์ŠคํŠธ๋กœ ํ•ฉ์น˜๊ธฐ

ํฌ๊ฒŒ ๋„ค ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. 

`For`๋ฌธ์„ ํ•œ ๋ฒˆ ํ˜น์€ ๋‘ ๋ฒˆ ์‚ฌ์šฉํ•˜์—ฌ list์˜ ๋ฉ”์„œ๋“œ์ธ. append() ์™€. extend()๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๊ณ , List comprehension์„ ์‚ฌ์šฉํ•ด์„œ ํ•œ ์ค„๋กœ ์ •๋ฆฌํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๋‚ด์žฅํ•จ์ˆ˜์ธ itertools์˜ chain์„ ์ด์šฉํ•ด์„œ ํ•ด๊ฒฐํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์–ด๋Š ๊ฒƒ์ด ๋” ๋‚ซ๋‹ค๊ณ  ๋งํ•˜๊ธฐ์—๋Š” ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ์–ด๋–ค ๋ฐฉ๋ฒ•์€ ์ฝ”๋“œ๊ฐ€ ๋ฌด์—‡์„ ํ•˜๋Š”์ง€ ๋” ์‰ฝ๊ฒŒ ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๊ณ , ์–ด๋–ค ๋ฐฉ๋ฒ•์€ ์ฝ”๋“œ๊ฐ€ ์ฐจ์ง€ํ•˜๋Š” ๊ณต๊ฐ„์ด ์ ์–ด์ ธ์„œ ๊ด€๋ฆฌํ•˜๊ณ  ๋ณด๊ธฐ์— ๋” ํŽธํ•ฉ๋‹ˆ๋‹ค. 

# ์„œ๋ธŒ๋ฆฌ์ŠคํŠธ
sublist_1 = ['์นด์ง€๋…ธ','์˜ค์ง•์–ด๊ฒŒ์ž„','๋” ๊ธ€๋กœ๋ฆฌ']
sublist_2 = ['์žฌ๋ฒŒ์ง‘ ๋ง‰๋‚ด์•„๋“ค', '์นด์ง€๋…ธ', '์†Œ๋…„์‹ฌํŒ']
sublist_3 = ['๋‚ฎ๊ณผ๋ฐค', '์›ฌ์ฆˆ๋ฐ์ด', '์ˆ˜๋ฆฌ๋‚จ', '๋ฏธ์ƒ']
sublist_4 = ['์ข…์ด์˜์ง‘','์šฐ๋ฆฌ๋“ค์˜ ๋ธ”๋ฃจ์Šค', '๋ฉœ๋กœ๊ฐ€์ฒด์งˆ']

# ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์„œ๋ธŒ๋ฆฌ์ŠคํŠธ๋กœ ๋œ ๋ฆฌ์ŠคํŠธ
lst = [sublist_1, sublist_2, sublist_3, sublist_4]



# ๋ชจ๋“  ์š”์†Œ๋ฅผ ํ•˜๋‚˜์˜ ๋ฆฌ์ŠคํŠธ๋กœ ํ•ฉ์น˜๊ธฐ
## ๋ฐฉ๋ฒ• 1: List Comprehension
def method1(lst):
    return [item for sublist in lst for item in sublist]


## ๋ฐฉ๋ฒ• 2: Looping (์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์„œ ์ง€์ €๋ถ„ํ•ด์ง€์ง€๋งŒ, readability๋Š” ๋” ์ข‹์Œ)
def method2(lst):
    result = []
    for sublist in lst:
        for item in sublist:
            result.append(item)
    return result


# ๋ฐฉ๋ฒ• 3: extend ์‚ฌ์šฉ
def method3(lst):
    result = []
    for sublist in lst:
        result.extend(sublist)
    return result


## ๋ฐฉ๋ฒ• 4: ๋‚ด์žฅํ•จ์ˆ˜ itertools.chain() ์‚ฌ์šฉ --> sublist๊ฐ€ ๋งŽ์•„์ง€๋ฉด ๊ฐ€์žฅ ๋น ๋ฅธ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค
from itertools import chain

def method4_a(lst):    
    return list(chain(*lst))  

def method4_b(lst):
    return list(chain.from_iterable(lst)) # ํ˜น์€ ์ด๋ ‡๊ฒŒ๋„ ๊ฐ€๋Šฅ

 

2. ๋ฌธ์ž์—ด๋กœ ํ‘œํ˜„๋œ ๋ฆฌ์ŠคํŠธ, ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ฝ๋Š” ๋ฐฉ๋ฒ•

๋ฆฌ์ŠคํŠธ๋‚˜ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ pandas.DataFrame์— ์ €์žฅํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”๋ฐ, ๊ผญ ๋ถˆ๋Ÿฌ์˜ค๋ฉด ์ฃ„๋‹ค ๋ฌธ์ž์—ด ํ˜•ํƒœ๋กœ ๋˜์–ด ์žˆ์–ด ๋‚œ๊ฐํ•  ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿด ๋•Œ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

# Built-in ๋‚ด์žฅํ•จ์ˆ˜ ์‚ฌ์šฉ
from ast import literal_eval

string = '["์ด","๋ฐ์ดํ„ฐ๋Š”","๋ฆฌ์ŠคํŠธ","์ผ๊นŒ์š”?","๋ฌธ์ž์—ด","์ผ๊นŒ์š”?"]'
lst = literal_eval(string)

print(lst)
['์ด', '๋ฐ์ดํ„ฐ๋Š”', '๋ฆฌ์ŠคํŠธ', '์ผ๊นŒ์š”?', '๋ฌธ์ž์—ด', '์ผ๊นŒ์š”?']

 

 

3. ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ Key ๊ฐ’ ํ˜น์€ Value ๊ฐ’์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ

๋”•์…”๋„ˆ๋ฆฌ์— ์ €์žฅ๋œ ๊ฐ’์„ ์ •๋ ฌํ•˜๊ณ  ์‹ถ์„ ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ๋”•์…”๋„ˆ๋ฆฌ์˜ ๊ฒฝ์šฐ์—๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ˆœ์„œ๋ผ๋Š” ๊ฐœ๋…์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋” ์ฝ”๋“œ๋ฅผ ์งœ๋‹ค๊ฐ€ ๋ง‰ํžˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด OrderedDict๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํŽธํ•ฉ๋‹ˆ๋‹ค.

from collections import OrderedDict
from operator import itemgetter

apt = {'Mapo': 12, 'Gangnam': 22,
        'Songpa': 16, 'Songdo': 8, 'Hannam': 20}

# Key ๊ฐ’์œผ๋กœ ์ •๋ ฌ (์˜ค๋ฆ„์ฐจ์ˆœ)
result1 = OrderedDict(sorted(apt.items()))   

# Value ๊ฐ’์œผ๋กœ ์ •๋ ฌ (์˜ค๋ฆ„์ฐจ์ˆœ)
result2 = OrderedDict(sorted(apt.items(), key=itemgetter(1)))


# Key ๊ฐ’์œผ๋กœ ์ •๋ ฌ (๋‚ด๋ฆผ์ฐจ์ˆœ)
# result3 = OrderedDict(sorted(apt.items(), reverse=True))   

# Value ๊ฐ’์œผ๋กœ ์ •๋ ฌ (๋‚ด๋ฆผ์ฐจ์ˆœ)
# result4 = OrderedDict(sorted(apt.items(), key=itemgetter(1), reverse=True))

result1 

key ๊ฐ’์˜ ์˜ค๋ฆ„์ฐจ์ˆœ์ธ a, b, c, d.. ์ˆœ์„œ๋กœ ์ •๋ ฌ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

OrderedDict([('Gangnam', 22), ('Hannam', 20), ('Mapo', 12), ('Songdo', 8), ('Songpa', 16)])

result2

value ๊ฐ’์˜ ์˜ค๋ฆ„์ฐจ์ˆœ์ธ 1, 2, 3, 4.. ์ˆœ์„œ๋กœ ์ •๋ ฌ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

OrderedDict([('Songdo', 8), ('Mapo', 12), ('Songpa', 16), ('Hannam', 20), ('Gangnam', 22)])

 

๋ง‰์ƒ ์“ฐ๋ ค๋‹ˆ๊นŒ ์ƒ๊ฐ์ด ๋งŽ์ด ๋‚˜์ง€๋Š” ์•Š๋„ค์š”...

์ด ํฌ์ŠคํŠธ์— ์กฐ๊ธˆ์”ฉ ์ถ”๊ฐ€ํ•ด ๋‚˜๊ฐ€๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€