좋습니다. 간단한 경우라면 말이죠.
# 1
new_list = [x for x in range(1, 11)]
print(new_list)
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# 2 일반 for loop
odd_numbers = []
for element in range(1, 11):
if (element % 2) == 1:
odd_numbers.append(element)
print(odd_numbers)
# [1, 3, 5, 7, 9]
# 3
list_comprehension = [element for element in range(
1, 11) if (element % 2) == 1]
print(list_comprehension)
# [1, 3, 5, 7, 9]
간단한 컴프리헨션은 다른 딕셔너리, 리스트 또는 세트 생성 기술보다 더 명확하고, 간단해요. generator expressions는 매우 효율적일 수 있다고 합니다. 왜일까요? 그 이유는 generator expresssions는 전체 리스트를 생성하는 것을 피하기때문입니다.
복잡한 컴프리헨션스나 generator expressions에 사용하면 코드를 읽는데 아주아주 어려울 거예요.
간단한 경우에는 사용하는데 오케이!하지만 리스트 컴프리헨션은 한 줄에 다 써야되서, 여러 줄의 for 문이나 filter expresssions에는 리스트 컴프리헨션을 사용하지 말고, 복잡해질 때는 반복문을 쓰세요.
Yes:
result = [mapping_expr for value in iterable if filter_expr]
result = [{'key': value} for value in iterable
if a_long_filter_expression(value)]
result = [complicated_transform(x)
for x in iterable if predicate(x)]
descriptive_name = [
transform({'key': key, 'value': value}, color='black')
for key, value in generate_iterable(some_input)
if complicated_condition_is_met(key, value)
]
result = []
for x in range(10):
for y in range(5):
if x * y > 10:
result.append((x, y))
return {x: complicated_transform(x)
for x in long_generator_function(parameter)
if x is not None}
squares_generator = (x**2 for x in range(10))
unique_names = {user.name for user in users if user is not None}
eat(jelly_bean for jelly_bean in jelly_beans
if jelly_bean.color == 'black')
No:
result = [complicated_transform(
x, some_argument=x+1)
for x in iterable if predicate(x)]
result = [(x, y) for x in range(10) for y in range(5) if x * y > 10]
return ((x, y, z)
for x in range(5)
for y in range(5)
if x != y
for z in range(5)
if y != z)
cities = ["Tokyo", "Shanghai", "Jakarta", "Seoul", "Guangzhou", "Beijing", "Karachi", "Shenzhen", "Delhi" ]
# Assignment 1
cities = ["Tokyo", "Shanghai", "Jakarta", "Seoul",
"Guangzhou", "Beijing", "Karachi", "Shenzhen", "Delhi"]
city_S = [city for city in cities if city[0] != 'S']
print(city_S)
# Assignment 1 (함수)
cities = ["Tokyo", "Shanghai", "Jakarta", "Seoul",
"Guangzhou", "Beijing", "Karachi", "Shenzhen", "Delhi"]
def city_S():
city_S = [city for city in cities if city[0] != 'S']
return city_S
print(city_S())
# ['Tokyo', 'Jakarta', 'Guangzhou', 'Beijing', 'Karachi', 'Delhi']
population_of_city = [(‘Tokyo', 36923000), (‘Shanghai', 34000000), (‘Jakarta', 30000000), (‘Seoul', 25514000), (‘Guangzhou', 25000000), (‘Beijing', 24900000), (‘Karachi', 24300000 ), ( ‘Shenzhen', 23300000), (‘Delhi', 21753486) ]
population_of_city = [('Tokyo', 36923000),
('Shanghai', 34000000),
('Jakarta', 30000000),
('Seoul', 25514000),
('Guangzhou', 25000000),
('Beijing', 24900000),
('Karachi', 24300000),
('Shenzhen', 23300000),
('Delhi', 21753486)]
def convert(input):
dict_population_of_city = dict((city, population)
for city, population in input)
return dict_population_of_city
print(convert(population_of_city))
# {'Tokyo': 36923000, 'Shanghai': 34000000, 'Jakarta': 30000000, 'Seoul': 25514000, 'Guangzhou': 25000000, 'Beijing': 24900000, 'Karachi': 24300000, 'Shenzhen': 23300000, 'Delhi': 21753486}