"Python 리스트"의 두 판 사이의 차이

80번째 줄: 80번째 줄:


==같이 보기==
==같이 보기==
* [[파이썬 리스트 스터디]]
* [[파이썬 리스트 예제]]
* [[파이썬 리스트 예제]]
* [[파이썬 리스트 메소드]]
* [[파이썬 리스트 메소드]]
* [[파이썬 리스트 스터디]]
* [[파이썬 len()]]
* [[파이썬 len()]]
* [[파이썬 2차원 리스트]]
* [[파이썬 2차원 리스트]]

2020년 1월 18일 (토) 12:52 판

1 개념

Python list
파이썬 리스트
  • 리스트는 Python에서 객체의 한 타입임
  • 요소의 집합이며 인덱스로 접근 가능함
  • []를 통해 생성
  • ','로 요소간 분리가 가능함

2 예제 1

colors = ['red', 'blue', 'green', 'black', 'white']
print( colors )
# ['red', 'blue', 'green', 'black', 'white']

print( colors[2] )
# green
print( colors[-1] )
# white
print( colors[-2] )
# black
print( colors[2:2])
# []
print( colors[2:3])
# ['green'] 
print( colors[2:4] )
# ['green', 'black']
print( colors[:3] )
# ['red', 'blue', 'green']
print( colors[3:] )
# ['black', 'white']
print( colors[::2] )
# ['red', 'green', 'white']
print( colors[1::2] )
# ['blue', 'black']
print( colors[0::3] )
# ['red', 'black']

3 예제 2

colors1 = ['red', 'blue', 'green']
colors2 = ['black', 'white']

print( colors1 + colors2 )
# ['red', 'blue', 'green', 'black', 'white']

print( colors2 * 2 )
# ['black', 'white', 'black', 'white']
print( 2 * colors2 )
# ['black', 'white', 'black', 'white']

4 메소드

append() - 요소 추가
>>> numbers = [0, 1, 2, 3, 4, 5]
>>> numbers.append(6)
>>> numbers
[0, 1, 2, 3, 4, 5, 6]
sort() - 정렬
>>> lst = ['c', 'a', 'b']
>>> lst.sort()
>>> lst
['a', 'b', 'c']

5 리스트 길이

len() - 리스트 길이
>>> letters = ['a', 'b', 'c', 'd']
>>> len(letters)
4

6 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}