# ๐ List Comprehension

Min-Ho Leeยท2020๋ 6์ 30์ผ
0

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

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}
๐ Rabbit can take a rest, but ๐ข turtle can't