[ Code Kata ] ๐Ÿคฏ Python # 9 ๋ฆฌ์ŠคํŠธ ๋‚ด ๊ฐ€์žฅ ์ž์ฃผ ๋“ฑ์žฅํ•œ ์ˆซ์ž ๋ฐ˜ํ™˜ํ•˜๊ธฐ / List Comprehension

Haileeยท2020๋…„ 12์›” 10์ผ
0

[ Code Kata ]

๋ชฉ๋ก ๋ณด๊ธฐ
15/28
post-thumbnail

๋ฌธ์ œ

nums๋Š” ์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
๊ฐ€์žฅ ์ž์ฃผ ๋“ฑ์žฅํ•œ ์ˆซ์ž๋ฅผ k ๊ฐœ์ˆ˜๋งŒํผ return ํ•ด์ฃผ์„ธ์š”.

nums = [1,1,1,2,2,3],
k = 2
- 
return [1,2]
- 
nums = [1]
k = 1
- 
return [1]

๊ทธ์ €๊ป˜ ํ’€์—ˆ๋˜ #7 ๋ฆฌ์ŠคํŠธ ๋‚ด ๊ณผ๋ฐ˜์ˆ˜ ๋„˜๋Š” ์ˆซ์ž ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋ฌธ์ œ๋ž‘ ๋น„์Šทํ•˜๋‹ค.
๊ทธ ๋ฌธ์ œ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ˆซ์ž๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ›์•„ ๊ณผ๋ฐ˜์ˆ˜๊ฐ€ ๋„˜๋Š” ์ˆซ์ž๋ฅผ ๋ฐ”๋กœ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋˜์—ˆ์ง€๋งŒ,
์ด๋ฒˆ ๋ฌธ์ œ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ˆซ์ž๋ฆฌ์ŠคํŠธ, k๋ผ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜ ๋‘๊ฐœ๋ฅผ ๋ฐ›์•„์„œ k์˜ ๊ฐฏ์ˆ˜๋งŒํผ์˜ ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ–ˆ๋‹ค.

#7๋ฒˆ ๋ฌธ์ œ๋ž‘ ๋น„์Šทํ•˜๊ฒŒ ํ’€๊ณ  ๋Œ€์‹  k์˜ ๊ฐœ์ˆ˜๋งŒํผ์„ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ•˜๋ ค ํ–ˆ๋‹ค!

๋‚ด๊ฐ€ ํ‘ผ ์ฝ”๋“œ

def top_k(nums, k):
  count       = set(nums)
  check_nums  = {}
  final_nums  = []

  for i in count:
    check_nums[i] = nums.count(i)
  
  for j in range(k):
    max_key = max(check_nums, key=check_nums.get)
    final_nums.append(max_key)
    del(check_nums[max_key])

  return final_nums

์šฐ์„  ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋“ค์–ด์˜จ ๋ฆฌ์ŠคํŠธ๋ฅผ set ์ž๋ฃŒ๊ตฌ์กฐํ˜•์œผ๋กœ ๋ฐ”๊ฟ”์„œ ์œ ์ผํ•œ ์ˆซ์ž๋“ค์„ ์ถ”์ถœํ•œ ๋’ค
ํ•ด๋‹น ์ˆซ์ž๋งŒํผ for loop์„ ๋Œ๋ฆฌ๋ฉด์„œ check_nums๋ผ๋Š” dictionary ๋‚ด๋ถ€์—
๊ฐ ์ˆซ์ž, ๊ทธ ์ˆซ์ž์˜ ๊ฐœ์ˆ˜๋ฅผ key, value๊ตฌ์กฐ๋กœ ๋„ฃ์–ด์ค€๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ k ๋งŒํผ์˜ ์ˆซ์ž๋“ค์„ ๋ฆฌํ„ดํ•ด์•ผ ํ•˜๊ธฐ๋•Œ๋ฌธ์—

  • ํ˜„์žฌ check_nums๋ผ๋Š” dictionary ๋‚ด์— ์กด์žฌํ•˜๋Š” value๋“ค ์ค‘ ๊ฐ€์žฅ ํฐ key๊ฐ’์„ ๊ตฌํ•œ๋’ค,
    max_key๊ฐ’์„ final_nums๋ผ๋Š” list๋‚ด์— ๋„ฃ๊ณ  check_nums๋ผ๋Š” dictionary๋‚ด์—์„œ ํ•ด๋‹น key๋ฅผ ์‚ญ์ œํ•œ๋‹ค.

๋Š” ์กฐ๊ฑด์œผ๋กœ k๋งŒํผ ๋ฐ˜๋ณตํ•˜๋Š” for loop์„ ๋Œ๋ฆฐ๋‹ค.
์ดํ›„ ๋ฆฌํ„ดํ•ด์ฃผ๊ธฐ!

์ด๊ฒƒ๋ณด๋‹ค ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์ด ๋ถ„๋ช…ํžˆ ์žˆ์„ํ…๋ฐ....
๋‹ค ํ’€์–ด๋‚ธ ๋’ค ๋ชจ๋ธ ์†”๋ฃจ์…˜์„ ๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.


๋ชจ๋ธ ์†”๋ฃจ์…˜

def top_k(nums, k):
    count = {}
    for n in nums:
        count[n] = count.get(n, 0) + 1
    bucket = [[] for _ in range(len(nums)+1)]		<- ์—ฌ๊ธฐ!!
    for n, freq in count.items():
        bucket[freq].append(n)
    ret = []
    for n_list in bucket[::-1]:
        if n_list:
            ret.extend(n_list)
            if len(ret) == k:
                return ret

์—ญ์‹œ๋‚˜ ์˜ค๋Š˜์˜ ๋ชจ๋ธ ์†”๋ฃจ์…˜๋„ ์ •๋ง์ด์ง€ ์‰ฝ์ง€ ์•Š๋‹ค.. ํ•˜ํ•˜
๊ทธ๋ž˜๋„ ๋”์šฑ ๊ฐ„๊ฒฐํ•œ, ์ˆ˜์ค€๋†’์€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ดํ•ด๋„๋ฅผ ์˜ฌ๋ฆฌ๊ณ  ์‹ถ์œผ๋‹ˆ๊นŒ..! ๋ถ„์„ํ•ด๋ณด๊ธฐ๐Ÿคฏ ๐Ÿคฏ

but ๋ถ„์„ํ•˜๋ ค๊ณ  ํ–ˆ๋”๋‹ˆ ๋‹น์žฅ 5๋ฒˆ์งธ ์ฝ”๋“œ๋ถ€ํ„ฐ ๋ง‰ํžˆ๋Š” ๊ฒƒ์ด ์•„๋‹Œ๊ฐ€ใ… ใ… 
๋‹น์žฅ ๊ฒ€์ƒ‰์„ ๋ญ๋ผ๊ณ  ํ•ด์•ผํ•  ์ง€.. ๊ฒ€์ƒ‰ ํ‚ค์›Œ๋“œ๋„ ๋ชจ๋ฅด๊ฒ ๊ณ ..!(์ด๋Ÿด๋•Œ๋Š” ์งˆ๋ฌธ.. )

list comprehension์ด๋ผ๋Š” ์„ ์–ธ ๋ฐฉ์‹์ด๋ผ๊ณ  ํ•œ๋‹ค.
Advanced Python์€ ์˜์˜ ์•ˆ๋“ค์–ด๊ฐ€๋ณผ์ค„ ์•Œ์•˜๋”๋‹ˆ, ์ด๋ ‡๊ฒŒ ๋ณด๊ฒŒ ๋˜๋„ค..
๋‚˜ Python์ด๋ž‘ ์ธ์—ฐ์ด์—ˆ๋„ค..

์•”ํŠผ ๊ทธ๋ž˜์„œ list Comprehension์ด ๋ฌด์—‡์ธ๊ฐ€?


List Comprehension

  • ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ„๋‹จํ•œ ํ‘œํ˜„์‹
  • ๋ฆฌ์ŠคํŠธ์™€ ๊ฐ™์ด [ ] ๋Œ€๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์„ฑ
  • ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“ค๋ ค๋Š” ์›์†Œ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ํ‘œํ˜„์‹์œผ๋กœ ์‹œ์ž‘ํ•˜์—ฌ for loop์ด ๋’ค๋”ฐ๋ผ์˜ค๋Š” ํ˜•์‹
  • for loop ๋’ค์— if๋ฌธ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์กฐ๊ฑด๋ฌธ์„ ํฌํ•จํ•œ ํ˜•์‹๋„ ๊ฐ€๋Šฅ!
[ ํ‘œํ˜„์‹ for ์›์†Œ in ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด ]
[ ํ‘œํ˜„์‹ for ์›์†Œ in ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด if๋ฌธ ]

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿฆฐ (๊ณผ๊ฑฐ์˜ ํ•˜๋žŒ) : ๊ตณ์ด list comprehension์ด๋ผ๋Š” ๊ฐœ๋…์„ ์•Œ์•„์•ผํ•ด? ๊ทธ๋ƒฅ for loop๋Œ๋ ค์„œ ๋„ฃ์œผ๋ฉด ์•ˆ๋ผ?
๐Ÿ‘ฉโ€๐Ÿ’ป (ํ˜„์žฌ์˜ ๋‚˜) : ์‘ ์•ˆ๋ผ ํ›จ์”ฌ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์งค ์ˆ˜ ์žˆ์œผ๋‹ˆ๊นŒ ๊ผญ ์•Œ์•„๋‘ฌ์•ผํ•ด

1) for list comprehension

1~10๊นŒ์ง€์˜ ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑํ•˜๊ณ  ์‹ถ์„ ๋•Œ, ์ง€๊ธˆ๊นŒ์ง€๋Š” ๋นˆ ๋ฐฐ์—ด์„ ์„ ์–ธํ•˜๊ณ , for loop์„ ์„ ์–ธํ•ด์„œ, ๋นˆ ๋ฐฐ์—ด ์•ˆ์— ์š”์†Œ๋“ค์ด ์ถ”๊ฐ€๋˜๋„๋ก ํ•˜๊ณ .. ๊ทธ๊ฑธ 10๋ฒˆ ๋ฐ˜๋ณต์„ ํ•˜๊ณ ..
์•„๋ฌด๋ฆฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ƒ๊ฐํ•ด๋ด๋„ 3?4์ค„ ์ •๋„์˜ ์ฝ”๋“œ๊ฐ€ ๋‚˜์˜ค๋Š”๋ฐ list comprehension์„ ์‚ฌ์šฉํ•˜๋ฉด?

new_list = [ x for x in range(1, 11) ]
print(new_list)

์š”๋กœ์ผ€ ํ•œ ์ค„๋กœ ๋๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ๐Ÿคฉ

2) if๋ฌธ์„ ํฌํ•จํ•˜๋Š” list comprehension

list๋‚ด ์ˆซ์ž๋“ค ์ค‘ ํ™€์ˆ˜๋งŒ ์ถ”์ถœํ•ด์„œ for loop ๋ฐ”๊นฅ์˜ odd_numbers๋ผ๋Š” ๋ฆฌ์ŠคํŠธ์— appendํ•˜๋Š” ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ,

odd_numbers = [ ]
for element in range(1,11):
    if (element % 2) == 1:
        odd_numbers.append(element)

์ง€๊ธˆ๊นŒ์ง€๋Š” ์ด๋Ÿฐ ์‹์œผ๋กœ ์ž‘์„ฑํ–ˆ์—ˆ๋‹ค!

if๋ฅผ ํฌํ•จํ•˜๋Š” list comprehension์œผ๋กœ ์ˆ˜์ •ํ•ด์ฃผ๋ฉด?

list_comprehension = [ element for element in range(1,11) if (element % 2) == 1 ]
print(list_comprehension)

ํ•œ์ค„๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ, ํ›จ์”ฌ ๊ฐ„๊ฒฐํ•ด์ง„๋‹ค!


list comprehension์˜ ์žฅ์ ์€?

  • ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•˜๋‹ค & ์ž‘์—… ์ˆ˜ํ–‰ ์†๋„๊ฐ€ ํ›จ์”ฌ ๋น ๋ฅด๋‹ค
  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์กฐํšŒํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค ๋•Œ ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค
    : ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค๋•Œ ์œ ์šฉ
    ๐Ÿ‘‰๐Ÿป DB์—์„œ ๊ฒŒ์‹œ๋ฌผ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ ์†๋„๋ฅผ ํ›จ์”ฌ ๋น ๋ฅด๊ฒŒ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค!

  • ๋˜ํ•œ ์„ฑ๋Šฅ๊ฐœ์„ ์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์„ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•˜์ง€๋งŒ ๋ชจ๋“  ์ƒํ™ฉ์—์„œ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์ด ๋งŒ๋Šฅ์€ ์•„๋‹™๋‹ˆ๋‹ค.
    ์˜ˆ๋ฅผ ๋“ค์–ด ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด์„œ๋Š” ์—ฌ๋Ÿฌ ์ค„์˜ ํ‘œํ˜„์‹๊ณผ ์กฐ๊ฑด๋ฌธ์œผ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์ด ์ด์ค‘ for๋ฌธ์˜ ๋ณต์žกํ•œ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜ ํ•œ์ค„๋กœ ์ฝ”๋”ฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋‚˜์€ ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ‘‰๐Ÿป ๊ตฌ๊ธ€์˜ ํŒŒ์ด์ฌ ์Šคํƒ€์ผ ๊ฐ€์ด๋“œ์—์„œ list comprehension์˜ ์žฅ์ ๊ณผ ๋‹จ์ ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค!

profile
์›น ๊ฐœ๋ฐœ ๐Ÿท๐Ÿ˜Ž๐Ÿ‘Š๐Ÿป๐Ÿ”ฅ

0๊ฐœ์˜ ๋Œ“๊ธ€