ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Python] ๋ฐฑ์ค€ 16165 ๊ฑธ๊ทธ๋ฃน ๋งˆ์Šคํ„ฐ ์ค€์„์ด (Dictionary)
    ๐Ÿ’ป Algorithm/Python 2022. 7. 2. 15:11

     

    ๐Ÿ“Œ ๋ฌธ์ œ

    ์ •์šฐ๋Š” ์†Œ๋ฌธ๋‚œ ๊ฑธ๊ทธ๋ฃน ๋•ํ›„์ด๋‹ค. ์ •์šฐ์˜ ์นœ๊ตฌ ์ค€์„์ด๋„ ๊ฑธ๊ทธ๋ฃน์„ ์ข‹์•„ํ•˜์ง€๋งŒ ์ด๋ฆ„์„ ์ž˜ ์™ธ์šฐ์ง€ ๋ชปํ•œ๋‹ค๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋‹ค. ์ •์šฐ๋Š” ์นœ๊ตฌ๋ฅผ ์œ„ํ•ด ๊ฑธ๊ทธ๋ฃน ๊ฐœ์ธ๊ณผ ํŒ€์˜ ์ด๋ฆ„์„ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์™ธ์šฐ๊ฒŒ ํ•˜๋Š” ํ€ด์ฆˆ ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค๊ณ ์ž ํ•œ๋‹ค.

    ์ž…๋ ฅ

    ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ์ด ์ž…๋ ฅ ๋ฐ›์„ ๊ฑธ๊ทธ๋ฃน์˜ ์ˆ˜ N(0 < N < 100)๊ณผ ๋งžํ˜€์•ผ ํ•  ๋ฌธ์ œ์˜ ์ˆ˜ M(0 < M < 100)์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.

     

    ๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ๋Š” ๊ฐ ๊ฑธ๊ทธ๋ฃน๋งˆ๋‹ค ํŒ€์˜ ์ด๋ฆ„, ๊ฑธ๊ทธ๋ฃน์˜ ์ธ์› ์ˆ˜, ๋ฉค๋ฒ„์˜ ์ด๋ฆ„์„ ํ•œ ์ค„์”ฉ ์ฐจ๋ก€๋Œ€๋กœ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ํŒ€๊ณผ ๋ฉค๋ฒ„์˜ ์ด๋ฆ„์€ ์ตœ๋Œ€ 100๊ธ€์ž์ด๋ฉฐ, ๋ชจ๋“  ๊ธ€์ž๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž์ด๋‹ค. ํ•˜๋‚˜์˜ ๊ฑธ๊ทธ๋ฃน์ด๋‚˜ ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ๊ฑธ๊ทธ๋ฃน์— ์ด๋ฆ„์ด ๊ฐ™์€ ๋‘ ๋ฉค๋ฒ„๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.

     

    ๊ทธ ๋‹ค์Œ ์ค„๋ถ€ํ„ฐ๋Š” M๊ฐœ์˜ ํ€ด์ฆˆ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ๊ฐ๊ฐ์˜ ํ€ด์ฆˆ๋Š” ๋‘ ์ค„๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ํŒ€์˜ ์ด๋ฆ„์ด๋‚˜ ๋ฉค๋ฒ„์˜ ์ด๋ฆ„์ด ์ฒซ ์ค„์— ์ฃผ์–ด์ง€๊ณ  ํ€ด์ฆˆ์˜ ์ข…๋ฅ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” 0 ๋˜๋Š” 1์ด ๋‘ ๋ฒˆ์งธ ์ค„์— ์ฃผ์–ด์ง„๋‹ค. ํ€ด์ฆˆ์˜ ์ข…๋ฅ˜๊ฐ€ 0์ผ ๊ฒฝ์šฐ ํŒ€์˜ ์ด๋ฆ„์ด ์ฃผ์–ด์ง€๋ฉฐ, 1์ผ ๊ฒฝ์šฐ ๋ฉค๋ฒ„์˜ ์ด๋ฆ„์ด ์ฃผ์–ด์ง„๋‹ค.

    ์ถœ๋ ฅ

    ์ฒซ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ํ€ด์ฆˆ์— ๋Œ€ํ•œ ๋‹ต์„ ์ถœ๋ ฅํ•œ๋‹ค. ํ€ด์ฆˆ์˜ ์ข…๋ฅ˜๊ฐ€ 0์ผ ๊ฒฝ์šฐ ํ•ด๋‹น ํŒ€์— ์†ํ•œ ๋ฉค๋ฒ„์˜ ์ด๋ฆ„์„ ์‚ฌ์ „์ˆœ์œผ๋กœ ํ•œ ์ค„์— ํ•œ ๋ช…์”ฉ ์ถœ๋ ฅํ•œ๋‹ค. ํ€ด์ฆˆ์˜ ์ข…๋ฅ˜๊ฐ€ 1์ผ ๊ฒฝ์šฐ ํ•ด๋‹น ๋ฉค๋ฒ„๊ฐ€ ์†ํ•œ ํŒ€์˜ ์ด๋ฆ„์„ ์ถœ๋ ฅํ•œ๋‹ค.

    ์˜ˆ์ œ ์ž…๋ ฅ 1

    3 4
    twice
    9
    jihyo
    dahyeon
    mina
    momo
    chaeyoung
    jeongyeon
    tzuyu
    sana
    nayeon
    blackpink
    4
    jisu
    lisa
    rose
    jenny
    redvelvet
    5
    wendy
    irene
    seulgi
    yeri
    joy
    sana
    1
    wendy
    1
    twice
    0
    rose
    1

    ์˜ˆ์ œ ์ถœ๋ ฅ 1

    twice
    redvelvet
    chaeyoung
    dahyeon
    jeongyeon
    jihyo
    mina
    momo
    nayeon
    sana
    tzuyu
    blackpink

     


     

    ๐Ÿ“Œ ํ’€์ด

    import sys
    input = sys.stdin.readline
    
    n, m = map(int, input().split()) # n์€ ๊ฑธ๊ทธ๋ฃน ์ˆ˜, m์€ ํ€ด์ฆˆ ์ˆ˜
    team = {}
    
    for _ in range(n):
        name = input().rstrip() # ํŒ€ ์ด๋ฆ„ ์ €์žฅ
        team[name] = []
        for _ in range(int(input())): # ๋ฉค๋ฒ„ ์ด๋ฆ„ ์ €์žฅ
            team[name].append(input().rstrip())
        team[name].sort()
    
    for _ in range(m): # 1: ํŒ€ ์ถœ๋ ฅ, #0: ๋ฉค๋ฒ„ ์ถœ๋ ฅ
        quiz = input().rstrip()
        if int(input()) == 1:
            print(''.join([k for k, v in team.items() if quiz in v]))
        else:
            print('\n'.join(*[v for k, v in team.items() if quiz in k]))

    rstrip()

    string์˜ ๊ฒฝ์šฐ input()์œผ๋กœ ๋ฐ›์œผ๋ฉด ๊ฐœํ–‰๋ฌธ์ž '\n'๊นŒ์ง€ ๊ฐ™์ด ์ €์žฅ๋˜๊ธฐ ๋•Œ๋ฌธ์— input().rstrip()์œผ๋กœ ๋์˜ ๊ฐœํ–‰๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    dictionary

    ํŒ€ ์ด๋ฆ„์œผ๋กœ๋„ ๋ฉค๋ฒ„๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๊ณ , ๋ฉค๋ฒ„ ์ด๋ฆ„์œผ๋กœ๋„ ํŒ€์„ ์ฐพ์„ ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๋ฏ€๋กœ key(ํŒ€๋ช…): value(๋ฉค๋ฒ„) ์Œ์œผ๋กœ ์ด๋ค„์ง€๋Š” dictionary๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ข‹์€ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

    blackpink: [jenny, jisu, lisa, rose]
    redvelvet: [irene, joy, seulgi, wendy, yeri]

    ↑ ์ด๋Ÿฐ ์‹์œผ๋กœ ์ €์žฅ๋˜๋„๋ก!

    • team = {} # ํŒ€ ๋”•์…”๋„ˆ๋ฆฌ
    • team[name] = [] # value๋ฅผ list๋กœ
    • team[name].append(input().rstrip()) # ๋ฉค๋ฒ„ ์ด๋ฆ„ ์ €์žฅ

    list comprehension

    • ๋ฉค๋ฒ„ ์ด๋ฆ„์œผ๋กœ ํŒ€ ์ฐพ๊ธฐ
    # list comprehension ์ ์šฉ X
    for k, v in team.items():
        if quiz in v:
            print(k)
            
    # list comprehension ์ ์šฉ O
    print(''.join([k for k, v in team.items() if quiz in v]))
    • ํŒ€ ์ด๋ฆ„์œผ๋กœ ๋ฉค๋ฒ„ ์ฐพ๊ธฐ
    # list comprehension ์ ์šฉ X
    for k, v in team.items():
        if quiz in k:
            print(*v, sep='\n')
            
    # list comprehension ์ ์šฉ O
    print('\n'.join(*[v for k, v in team.items() if quiz in k]))

    unpaking

    'ํŒ€ ์ด๋ฆ„์œผ๋กœ ๋ฉค๋ฒ„ ์ฐพ๊ธฐ' ๊ณผ์ •์—์„œ list ์•ˆ์— ๋ฌถ์—ฌ ์žˆ๋Š” ๋ฉค๋ฒ„ ์ด๋ฆ„๋“ค์„ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ถœ๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    for k, v in team.items():
        if quiz in k:
            print(v, sep='\n')
    ['irene', 'joy', 'seulgi', 'wendy', 'yeri']

    print๋ฌธ์— sep='\n'์„ ์ถ”๊ฐ€ํ•ด๋„ ์œ„์™€ ๊ฐ™์ด list๊ฐ€ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅ๋˜๋Š”๋ฐ, list ์•ž์— unpaking ์—ฐ์‚ฐ์ž์ธ *์„ ๋ถ™์ด๋ฉด ๋ฌถ์—ฌ ์žˆ๋Š” ๊ฐ’๋“ค์„ ํ’€์–ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    for k, v in team.items():
        if quiz in k:
            print(*v, sep='\n')
    irene
    joy
    seulgi
    wendy
    yeri

    ๋Œ“๊ธ€

Designed by Tistory.