import numpy as np
numbers = [1, 2, 3, 4, 5]
print(numbers)
numbers2 = []
for i in range(1,6):
numbers2.append(i)
print(numbers2)
# List comprehension
numbers3 = [n for n in range(1,6)]
print(numbers3 )
실행결과
[1]
[1, 2, 3, 4, 5]
[1, 2]
[1, 2, 3, 4, 5]
[1, 2, 3]
[1, 2, 3, 4, 5]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
# 2, 4, 6, 8, 10
even = [2 * n for n in range (1,6)]
print(even)
실행결과
[2, 4, 6, 8, 10]
# 1, 4, 9, 16, 25
squares = [n **2 for n in range(1,6)]
print(squares)
실행결과
[1, 4, 9, 16, 25]
# 난수 10개 배열
randoms = [np.random.randint(0, 101)for x in range(10)]
print(randoms)
실행결과
[94, 66, 87, 92, 23, 78, 44, 72, 36, 22]
# 2, 4, 6, 8, 10
even2 = []
for n in range(1,11):
if n%2 ==0:
even2.append(n)
print(even2)
실행결과
[2, 4, 6, 8, 10]
# 2, 4, 6, 8, 10
even3 = [n for n in range(1,11) if n%2 == 0]
print(even3)
실행결과
[2, 4, 6, 8, 10]
# 주사위 2개를 던졌을 때의 경우의 수
# (1,1), (1,2).. (1,6),
# (2,1) ..(2,6)
# ...
# (6,6)
dice1 = [] # 빈 리스트 생성
for x in range(1,7):
for y in range(1,7):
dice1.append((x, y)) #(x,y) tuple를 리스트에 추가
print(dice1)
실행결과
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]
dice2 = [(x,y) for x in range(1,7) for y in range(1,7)]
print(dice2)
실행결과
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]
# (1,1)
# (2,1), (2,2)
# (3,1), (3,2), (3,3)
# ...
# (x,y) if x >= y
dice3 = []
for x in range(1,7):
for y in range(1,7):
if x>=y:
dice3.append((x,y))
print(dice3)
실행결과
[(1, 1), (2, 1), (2, 2), (3, 1), (3, 2), (3, 3), (4, 1), (4, 2), (4, 3), (4, 4), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]
dice4 = [(x,y)
for x in range(1,7)
for y in range(1,7)
if x>=y]
print(dice4)
실행결과
[(1, 1), (2, 1), (2, 2), (3, 1), (3, 2), (3, 3), (4, 1), (4, 2), (4, 3), (4, 4), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]
# 시험점수(0 ~ 100) 10개를 가지고 있는 리스트
scores = [np.random.randint(0,101) for _ in range(10)]
print(scores)
실행결과
[37, 12, 72, 9, 75, 5, 79, 64, 16, 1]
# 평균보다 높은 점수들의 리스트
mean = np.mean(scores)
above_mean = []
for s in scores:
if s > mean:
above_mean.append(s)
print(above_mean)
실행결과
[72, 75, 79, 64]
list comprehension에 IF절 적용하기 : 3항 연산자
def identity(x, y):
result = 0
if x==y:
result = 1
else:
result = 0
return result
위와 아래는 동일한 함수이다.
def identity(x, y):
return result = 1 if x == y else 0 # 3항 연산자
# result에 if 조건을 만족하면 1, 만족하지 않으면 0을 저장한다.
'Python > Python기초' 카테고리의 다른 글
Python 13_ while (0) | 2019.12.04 |
---|---|
Python 12_dictionary comprehension (0) | 2019.12.03 |
Python 10_for 구문 (0) | 2019.11.29 |
Python 9_if구문 (0) | 2019.11.28 |
Python 8_딕트dict와 set (0) | 2019.11.27 |