리스트 함축

지애·2022년 4월 9일
0

Python

목록 보기
9/17

리스트 함축

squares =[]
for x in range(10):
	if x%2 == 0:
		squares.append(x**2)

이 코드를 함축하면,

squares = [x**2 for x in range(10) if x%2 == 0]

prices=[120, 345, 654, 768, -234]
mprices = []
for i in prices:
	if i > 0:
    	mprices.append(i)
    else:
    	mprice.append(0)

이 코드를 함축하면,

prices=[120, 345, 654, 768, -234]
mprices = [i if i>0 else 0 for i in prices]

numbers = []
for x in ['a', 'b', 'c']:
	for y in ['x', 'y', 'z']:
   	numbers.append(x+y)

이 코드를 함축하면,

numbers = [x+y, for x in ['a', 'b', 'c'] for y in ['x', 'y', 'z']]
profile
차근차근

0개의 댓글