리스트의 각 항목들은 '[]'로 둘러싸게 됩니다.
리스트 내의 항목들에 대한 구분은 ,(콤마)로 구분합니다.
리스트 내에 또 다른 리스트를 내포할 수 있습니다.
비어 있는 리스트를 만들 수 있습니다.
리스트의 항목들에 인덱스 값으로 접근할 수 있습니다.
리스트의 항목들은 바뀔 수 있습니다.
friends = ['Joseph', 'Glenn', 'Sally']
carryon = ['socks', 'shirt', 'perfume']
colors = ['red', ['yellow','blue'], 'black']
emptyList = []
print(colors[0])
# red라고 출력됨
lotto = [2, 14, 26, 41, 63]
print(lotto)
# [2, 14, 26, 41, 63]이 출력됨
lotto[2] = 28
print(lotto)
# [2, 14, 28, 41, 63]이 출력됨
friends = ['Joseph', ' Glenn', 'Sally']
print(len(friends))
# 3으로 출력됨
for i in range(5):
print(i)
# 0
# 1
# 2
# 3
# 4 로 출력됩니다.
a = [1, 2, 3]
b = [4, 5, 6]
c = a + b
print(c)
# [1, 2, 3, 4, 5, 6]로 출력됩니다.
t = [9, 41, 12, 3, 74, 15]
print(t[1:3])
print(t[:4])
print(t[3:])
print(t[:])
# [41, 12]
# [9, 41, 12, 3]
# [3, 74, 15]
# [9, 41, 12, 3, 74, 15] 로 출력됩니다.
빈 리스트 만들기 - 항목 추가하기 - 항목 정렬하기 - in을 활용해 'Glenn'이 친구 목록에 있는지 확인하기
friends = list()
friends.append('Joseph')
friends.append('Glenn')
friends.append('Sally')
print(friends)
# ['Joseph', 'Glenn', 'Sally']
friends.sort()
print(friends)
# ['Glenn', 'Joseph', 'Sally']
print('Glenn' in friends)
# True로 출력됩니다.
words2 = 'first;second;third'
stuff2 = words2.split()
print(stuff2)
# ['first;second;third']
stuff2 = words2.split(';')
print(stuff2)
# ['first', 'second', 'third']
활용 예시 - 이메일 주소 추출
line = 'From [email protected] Sat Jan 5 09:14:16 2008'
# line 에 uct.ac.za만 추출하는 방법을 찾아 보도록 하겠습니다.
words = line.split()
print(words[1])
# [email protected]이 출력됩니다.
email = words[1]
address = email.split('@')
print(address)
# ['stephen.marquard', 'uct.ac.za'] 가 출력됩니다.
print(address[1])
# uct.ac.za가 출력됩니다.