#일반적 사용
squares = list()
for x in range(10):
    squares.append(x**2)
print(squares) #[0, 1, 4, 9, 16, 25, 36, 49, 64, 81] 

#리스트 컴프리헨션을 사용했을 때
squares = [x**2 for x in range(10)] #x를 정의해줘야 한다
print(squares) #[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]squares

combs = []
for x in [1, 2, 3]:
    for y in [2, 3, 4]:
        if x != y:
             combs.append((x, y)) #튜플 형태로 요소를 추가하라
print(combs) [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 2), (3, 4)]         

combs = [(x, y) for x in [1, 2, 3] for y in [2, 3, 4] if x != y]
print(combs) #[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 2), (3, 4)]

from math import pi
l = [str(round(pi, i)) for i in range(1, 6)]
print(l) #['3.1', '3.14', '3.142', '3.1416', '3.14159']

vec = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for row in vec:
    print(row)
"""   
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
"""
l = [num ** 2 for e in vec for num in e] #바깥쪽에서 안쪽으로 작성해가니 순서대로 간다
print(l) #[1, 4, 9, 16, 25, 36, 49, 64, 81] #1차원적 리스트로 나타냄

#구구단
gugudan = list("%d * %d = %d" % (i, j, i*j) for i in range(2, 10, 2) for j in range(1, 10))
print(gugudan)
"""
['2 * 1 = 2', '2 * 2 = 4', '2 * 3 = 6', '2 * 4 = 8', '2 * 5 = 10', '2 * 6 = 12', '2 * 7 
= 14', '2 * 8 = 16', '2 * 9 = 18', '4 * 1 = 
4', '4 * 2 = 8', '4 * 3 = 12', '4 * 4 = 16', '4 * 5 = 20', '4 * 6 = 24', '4 * 7 = 28', 
'4 * 8 = 32', '4 * 9 = 36', '6 * 1 = 6', '6 * 2 = 12', '6 * 3 = 18', '6 * 4 = 24', '6 * 5 = 30', 
'6 * 6 = 36', '6 * 7 = 42', '6 * 8 = 48', '6 * 9 = 54', '8 * 1 = 8', '8 * 2 = 16', '8 * 3 = 24', 
'8 * 4 = 32', '8 * 5 = 40', '8 * 6 = 48', '8 * 7 = 56', '8 * 8 = 64', '8 * 9 = 72']
"""
#튜플 컴프리헨션: tuple()을 붙여야 작동한다
t = tuple(num for num in range(10))
print(t) #(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

