리스트(list)

김성우·2023년 12월 11일

파이썬실습(기초)

목록 보기
12/25
  • 자료구조에서 중요
  • 리스트 자료형(순서O, 중복O, 수정O, 삭제O)
# 선언
a = []
b = list()
print(type(a))

c = [70, 75, 80, 85] # 0부터 3까지
print(len(c))

d = [1000, 10000, 'Ace', 'Base', 'Captine']
e = [1000, 10000, ['Ace', 'Base', 'Captine']] # 리스트 안에 리스트
f = [21.42, 'sungwoo', 3, 4, False, 3.14159]
# 인덱싱
print('d = ', type(d))
print('d[1] = ', d[1])
print('d[0] + d[1] = ', d[0] + d[1])

print('e - ', e[-1][1])
print('e - ', type(e[-1][1]))
print('e - ', list(e[-1][1]))

# 슬라이싱
print('d - ', d[0:3])
print('d - ', d[2:])
print('e - ', e[-1][1:3])

# 리스트 연산
print('c + d = ', c + d)
print('c * 3 = ', c * 3)
# print("'Test' + c[0]", "Test" + c[0])  TypeError: can only concatenate str (not "int") to str 앞에는 문자라 오류가 난다
print("'Test' + c[0]", "Test" + str(c[0]))

# 값 비교
print(c == c[:3] + c[3:])
print(c)
print(c[:3] + c[3:])

# Identity(id)
temp = c
print(temp, c)
print(id(temp))
print(id(c))

# 리스트 수정, 삭제
c[0] = 4 # 수정
print('c = ', c)

c[1:2] = ['a', 'b', 'c'] # 아래와 차이점은 구조가 다르다
print('c = ', c)

c[1] = ['a', 'b', 'c']
print('c = ', c)

# 리스트 함수
a = [5, 2, 3, 1, 4]

print('a =', a)

a.append(10) # append() 끝부분에 추가시키는 함수
print('a =', a)

a.sort() # 오름차순으로 정렬
print('a =', a)

a.reverse() # 반대로 위치를 바꿈
print('a =', a)

print('a =', a.index(3))

a.insert(2, 7) # 3번째 위치에 7 삽입하고 뒤로 밀림
print('a =', a) 

a.remove(10) # 제거
print('a =', a)

print( a.pop()) # pop() 끝에 있는 원소를 꺼내오고 기존의 리스트에서 그것을 제거한다.
print('a =', a)

print( a.count(4)) # 4의 갯수

ex = [8, 9]
a.extend(ex) # 새로 삽입
print('a =', a)

# 삭제 : remove, pop, del 이중에선 remove를 가장 많이 사용

# 반복문 활용
while a :
    data = a.pop()
    print(data)

profile
빅데이터 정복하기

0개의 댓글