Tuesday_June_30
.
.
.
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)
๋ค์๊ณผ ๊ฐ์ ๋์๋ชฉ๋ก์ ๋ฆฌ์คํธ๊ฐ ์ฃผ์ด์ก์๋, ๋์์ด๋ฆ์ด 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']
๋ค์๊ณผ ๊ฐ์ ๋์, ์ธ๊ตฌ์๊ฐ ํํ์ ๋ฆฌ์คํธ๋ก ์ฃผ์ด์ก์๋, ํค๊ฐ ๋์ ๊ฐ์ด ์ธ๊ตฌ์์ธ ๋์ ๋๋ฆฌ๋ฅผ ๋์ ๋๋ฆฌ ์ปดํ๋ฆฌํจ์ ์ ์ฌ์ฉํ ํจ์๋ฅผ ์์ฑํด ๋ณด์ธ์.
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}