파이썬 문법 - Lists

Junyeong Choi·2021년 5월 13일

Description

  • Ordered sequence
  • Use []
  • indexing과 slicing 가능

mylist = [1,2,3]
mylist = ['String', 100, 20.3]
len(mylist) = 3

만약

mylist = ['one', 'two', 'three']
mylist[0] = 'one'
mylist[1:] = 'two', 'three'

anotherlist = ['four', 'five']
mylist + anotherlist = 'one', 'two', 'three', 'four', 'five'

리스트는 mutable이라서 안에 것들을 바꿀 수 있다. 예를들어
newlist = mylist + anotherlist
newlist[0] = 'Hello'라 그러면
newlist = 'Hello', 'two', 'three', 'four', 'five'


append()

newlist.append('six') = 'Hello', 'two', 'three', 'four', 'five', 'six'
이렇게 다음으로 쌓이게 된다.


pop()

newlist.pop() = 'six'
newlist = 'Hello', 'two', 'three', 'four', 'five'
이렇게 다음 것이 빠지게 된다.

pop() 은 index number를 지정 할 수 있다.
newlist.pop(0) = 'Hello'


sort()

newlist = ['a', 'e', 'd', 'c', 'b']
numlist = [4,1,2,3]

newlist.sort() = ['a', 'b', 'c', 'd', 'e']
numlist.sort() = [1,2,3,4]


reverse()

newlist = ['a', 'e', 'd', 'c', 'b']
numlist = [4,1,2,3]

newlist.sort() = ['e', 'd', 'c', 'b', 'a']
numlist.sort() = [4,3,2,1]

0개의 댓글