list는 배열을 의미합니다. 하지만 list 는 C++보다 다양한 기능을 제공하는 점에서 편하다고 생각합니다.
그럼 먼저 리스트는 리스트 만의 덧셈을 허용합니다.
0. 리스트의 표현
a=list()
a=[]
1. 리스트의 덧셈
def solution():
    alist=[1,2,3,4,5]
    blist=[2,3,4,5,6,7]
    result=alist+blist
    print(result)	#[1, 2, 3, 4, 5, 2, 3, 4, 5, 6, 7]
    
    #참고 : 리스트의 요소를 가지고 오는 것은 배열과 동일하게 list[idx]를 해주면 됩니다
	print(alist[0]) # 1
    
if __name__ == '__main__':
    solution()
2. 리스트의 인덱스 추가 및 삽입
def solution():
    a=[]
    a.append(1)	#[1]
    a.append(2)	#[1,2]
    a.insert(1,7)	#[1,7,2]
    print(a)
if __name__ == '__main__':
    solution()
3. 리스트 값 삭제 및 슬라이싱
def solution():
    a=[1,2,3,4,5,6,7]
    a.pop()     #[1,2,3,4,5,6] 디폴트 설정: 마지막 요소를 제거
    a.pop(0)    #[2,3,4,5,6]
    a.pop(2)    #[2,3,5,6]
    a.remove(5) #[2,3,6]
    alpha=['a','b','c','d','e','f']
    print(alpha[1:4])   #['b', 'c', 'd'] 인덱스 1부터 3까지 
if __name__ == '__main__':
    solution()
4. 정렬, 리스트 count, 다중 리스트
def solution():
    a=[213,31,4,123,51]
    a.sort()    #[4, 31, 51, 123, 213]
    a.reverse() #[213, 123, 51, 31, 4]
    b=[1,2,2,3,3,3,4,4,4,4]
    b.count(1)  #1
    b.count(2)  #2
    data=[]
    data+=[[1,2,3,]]
    data.append([1,2,3])    #[[1, 2, 3], [1, 2, 3]]
    print(data)
if __name__ == '__main__':
    solution()'Language > python' 카테고리의 다른 글
| python - 내장함수 (0) | 2019.12.22 | 
|---|---|
| python - set :집합 (0) | 2019.12.21 | 
| python - append 그리고 extend (0) | 2019.12.21 | 
| python - map,filter,reduce: 조건 변형 (0) | 2019.12.09 | 
| python - combinations,permutations:순열과 조합 (0) | 2019.12.08 | 
