๐Ÿ—‚ List Comprehension

Min-Ho Leeยท2020๋…„ 6์›” 30์ผ
0

Learn Python

๋ชฉ๋ก ๋ณด๊ธฐ
17/22
post-thumbnail

Tuesday_June_30
.
.
.

๐Ÿ—‚ List Comprehension

List Comprehension์ด๋ž€, ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ„๋‹จํ•œ ํ‘œํ˜„์‹์œผ๋กœ ๋ฆฌ์ŠคํŠธ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋Œ€๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์„ฑํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“ค๋ ค๊ณ  ํ•˜๋Š” ์›์†Œ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ํ‘œํ˜„์‹์œผ๋กœ ์‹œ์ž‘ํ•˜์—ฌ for ๋ฃจํ”„๊ฐ€ ๋’ค์— ๋”ฐ๋ผ์˜ค๋Š” ํ˜•์‹์„ ๊ฐ€์ง„๋‹ค. For ๋ฌธ ๋’ค์— if๋ฌธ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์กฐ๊ฑด๋ฌธ์„ ํฌํ•จํ•œ ํ˜•์‹๋„ ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

ํ‘œํ˜„ ๋ฐฉ๋ฒ•

comprehension_list = [x for x in something]

๋ณ€์ˆ˜ ์ด๋ฆ„ = [๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•œ ๊ฐ’ for ์‚ฌ์šฉํ•  ๋ณ€์ˆ˜ ์ด๋ฆ„ in ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ’]

comprehension_list = [x for x in something if ~]

๋ณ€์ˆ˜ ์ด๋ฆ„ = [๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•œ ๊ฐ’ for ์‚ฌ์šฉํ•  ๋ณ€์ˆ˜ ์ด๋ฆ„ in ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ’ if๋ฌธ]

๊ทธ๋Ÿผ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์˜ ์žฅ์ ์„ ์‚ดํŽด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์œ„์˜ ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜ ํ˜•์‹์œผ๋กœ 1๋ถ€ํ„ฐ 10๊นŒ์ง€๋ฅผ ๊ฐ€์ง€๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜์œผ๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

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

1๏ธโƒฃ ๋ฌธ์ œ

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

cities = [Tokyo, Shanghai, Jakarta, Seoul, Guangzhou, Beijing, Karachi, Shenzhen, Delhi ]

ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ, parameter๋กœ ์›๋ž˜์˜ cities๋ผ๋Š” list๋ฅผ ์ฃผ๊ณ  for๋ฌธ์œผ๋กœ ์›์†Œ๋“ค์„ ๋ฐ›์•„์˜ค๋Š”๋ฐ, 'S'๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋„์‹œ์˜ ์ด๋ฆ„์„ ๋”ฐ์™€์•ผ ํ•˜๋‹ˆ String์—์„œ ์ฒซ ๊ธ€์ž๋ฅผ ๋”ฐ์˜ค๋ ค๋ฉด ์Šฌ๋ผ์ด์‹ฑ์„ ํ•˜๋ฉด ๋œ๋‹ค.

cities = ["Tokyo", "Shanhai", "Jakarta", "Seoul",
          "Guangzhou", "Beijing", "Karachi", "Shenzhen", "Delhi"]

print("This is Original List = ")
print(cities)

def pick_s_cities(list):
    list_comprehension = [element for element in list if (element[0]) == "S"]
    return list_comprehension

print("This is Comprehension List = ")
print(pick_s_cities(cities))

์ถœ๋ ฅ ๊ฒฐ๊ณผ

This is Original List = 
['Tokyo', 'Shanhai', 'Jakarta', 'Seoul', 'Guangzhou', 'Beijing', 'Karachi', 'Shenzhen', 'Delhi']
This is Comprehension List = 
['Shanhai', 'Seoul', 'Shenzhen']

2๏ธโƒฃ ๋ฌธ์ œ

๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋„์‹œ, ์ธ๊ตฌ์ˆ˜๊ฐ€ ํŠœํ”Œ์˜ ๋ฆฌ์ŠคํŠธ๋กœ ์ฃผ์–ด์กŒ์„๋•Œ, ํ‚ค๊ฐ€ ๋„์‹œ ๊ฐ’์ด ์ธ๊ตฌ์ˆ˜์ธ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ ์ปดํ”„๋ฆฌํ—จ์…˜์„ ์‚ฌ์šฉํ•œ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์„ธ์š”.

population_of_city = [("Tokyo", 36923000), ("Shanghai", 34000000), ("Jakarta", 30000000), ("Seoul", 25514000), (
    "Guangzhou", 25000000), ("Beijing", 24900000), ("Karachi", 24300000), ("Shenzhen", 23300000), ("Delhi", 21753486)]

๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ๋ฐ˜ํ™˜์„ ํ•ด์•ผํ•˜๋‹ˆ ์ผ๋‹จ list์™€ ๋‹ค๋ฅด๊ฒŒ my_dictionary๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ƒ์„ฑํ•˜๋“ฏ {}๋กœ ์ดˆ๊ธฐํ™” ํ•ด์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์œ„์—์„œ ๋งํ–ˆ๋“ฏ List Comprehension์˜ ์‚ฌ์šฉ๋ฒ•์€ ๋ฐ˜๋ณต๋ฌธ ์ „์— ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•œ ๊ฐ’์ด ์˜ค๊ฒŒ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์šฐ๋ฆฌ๋Š” element๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•ด ๋”•์…”๋„ˆ๋ฆฌ์— ๊ฐ’์„ ์ฃผ๋“ฏ ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•  ๊ฒƒ์ด๋‹ˆ element[0] : element[1] ์ด๋ ‡๊ฒŒ ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. (์ฐธ๊ณ ๋กœ tuple์€ ์ˆ˜์ •์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‚˜ index์˜ ๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ๋Š” list์™€ ๊ฐ™์•„์„œ ์•ž์— ์˜ค๋Š” ๋„์‹œ์˜ ์ด๋ฆ„์ธ 0๋ฒˆ ์งธ index๋ฅผ ์•ž์—, ์ธ๊ตฌ์ˆ˜์ธ 1๋ฒˆ ์งธ index๋ฅผ ๋’ค์— ์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

population_of_city = [("Tokyo", 36923000), ("Shanghai", 34000000), ("Jakarta", 30000000), ("Seoul", 25514000), (
    "Guangzhou", 25000000), ("Beijing", 24900000), ("Karachi", 24300000), ("Shenzhen", 23300000), ("Delhi", 21753486)]

print("This is Tuples in List")
print(population_of_city)

def make_dictionary(list):
    my_dictionary = {element[0]: element[1] for element in list}
    # my_dictionary = {}
    # for element in list:
    #     my_dictionary[element[0]] = element[1]
    return my_dictionary

print("This is Dictionary")
print(make_dictionary(population_of_city))

์ถœ๋ ฅ ๊ฒฐ๊ณผ

This is Tuples in List
[('Tokyo', 36923000), ('Shanghai', 34000000), ('Jakarta', 30000000), ('Seoul', 25514000), ('Guangzhou', 25000000), ('Beijing', 24900000), ('Karachi', 24300000), ('Shenzhen', 23300000), ('Delhi', 21753486)]
This is Dictionary
{'Tokyo': 36923000, 'Shanghai': 34000000, 'Jakarta': 30000000, 'Seoul': 25514000, 'Guangzhou': 25000000, 'Beijing': 24900000, 'Karachi': 24300000, 'Shenzhen': 23300000, 'Delhi': 21753486}
profile
๐Ÿ‡ Rabbit can take a rest, but ๐Ÿข turtle can't

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