๐Ÿง  ์„œ์šธ์—์„œ ๊น€์„œ๋ฐฉ ์ฐพ๊ธฐ

๋ฌธ์ œ

Stringํ˜• ๋ฐฐ์—ด seoul์˜ element์ค‘ Kim์˜ ์œ„์น˜ x๋ฅผ ์ฐพ์•„, ๊น€์„œ๋ฐฉ์€ x์— ์žˆ๋‹ค๋Š” String์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜๋ผ. seoul์— Kim์€ ์˜ค์ง ํ•œ ๋ฒˆ๋งŒ ๋‚˜ํƒ€๋‚˜๋ฉฐ ์ž˜๋ชป๋œ ๊ฐ’์ด ์ž…๋ ฅ๋˜๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.

  • seoul์€ ๊ธธ์ด 1 ์ด์ƒ, 1000 ์ดํ•˜์ธ ๋ฐฐ์—ด์ด๋‹ค.
  • seoul์˜ ์›์†Œ๋Š” ๊ธธ์ด 1 ์ด์ƒ, 20 ์ดํ•˜์ธ ๋ฌธ์ž์—ด์ด๋‹ค.
  • Kim์€ ๋ฐ˜๋“œ์‹œ seoul ์•ˆ์— ํฌํ•จ๋˜์–ด ์žˆ๋‹ค.

๋‚˜์˜ ํ’€์ด

def findKim(seoul):
    idx = 0
    for s in seoul:
        if s == 'Kim':
            return '๊น€์„œ๋ฐฉ์€ {}์— ์žˆ๋‹ค'.format(idx)
        else:
            idx += 1

ํŒŒ์ด์ฌ์œผ๋กœ ์ž๋ฐ”์ฒ˜๋Ÿผ ํ’€๊ธฐโ€ฆใ…Ž; ํ’€๋ฉด์„œ๋„ idx ๋ณ€์ˆ˜๋ฅผ ๊ตณ์ด ์„ ์–ธํ•ด์„œ ์นด์šดํŒ…์„ ํ•ด์•ผํ•˜๋‚˜? ์‹ถ์—ˆ๋Š”๋ฐ (๊ทธ๋Ÿผ ์™œ ์•ˆ์ฐพ์•„๋ดค์–ด ์ž„๋งˆ) ์˜ˆ์ƒ๋Œ€๋กœ ํŒŒ์ด์ฌ์—๋Š” ์ž…๋ ฅ๊ฐ’์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ–ˆ๋‹ค.

๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด

def findKim(seoul):
    return '๊น€์„œ๋ฐฉ์€ {}์— ์žˆ๋‹ค'.format(seoul.index('Kim'))

index() ํ•จ์ˆ˜๋Š” ์ž…๋ ฅ๊ฐ’์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค. seoul = ['Jane', 'Kim'] ์ผ ๋•Œ, seoul.index('Kim')์€ seoul ๋ฆฌ์ŠคํŠธ์—์„œ 'Kim'์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ผ๋Š” ๋œป์ด๋ฏ€๋กœ 1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

References


Written by@ugaemi
Record things I want to remember

๐Ÿฑ GitHub๐Ÿ“š Reading Space