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

잔글 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
8번째 줄: 8번째 줄:


==예제 1==
==예제 1==
<source lang='python' run>
<syntaxhighlight lang='python' run>
colors = ['red', 'blue', 'green', 'black', 'white']
colors = ['red', 'blue', 'green', 'black', 'white']
print( colors ) # ['red', 'blue', 'green', 'black', 'white']
print( colors ) # ['red', 'blue', 'green', 'black', 'white']
</source>
</syntaxhighlight>
<source lang='python' run>
<syntaxhighlight lang='python' run>
colors = ['red', 'blue', 'green', 'black', 'white']
colors = ['red', 'blue', 'green', 'black', 'white']
print( colors[2] )  # green
print( colors[2] )  # green
print( colors[-1] ) # white
print( colors[-1] ) # white
print( colors[-2] ) # black
print( colors[-2] ) # black
</source>
</syntaxhighlight>
<source lang='python' run>
<syntaxhighlight lang='python' run>
colors = ['red', 'blue', 'green', 'black', 'white']
colors = ['red', 'blue', 'green', 'black', 'white']
print( colors[2:2] ) # []
print( colors[2:2] ) # []
print( colors[2:3] ) # ['green']  
print( colors[2:3] ) # ['green']  
print( colors[2:4] ) # ['green', 'black']
print( colors[2:4] ) # ['green', 'black']
</source>
</syntaxhighlight>
<source lang='python' run>
<syntaxhighlight lang='python' run>
colors = ['red', 'blue', 'green', 'black', 'white']
colors = ['red', 'blue', 'green', 'black', 'white']
print( colors[:3] ) # ['red', 'blue', 'green']
print( colors[:3] ) # ['red', 'blue', 'green']
print( colors[3:] ) # ['black', 'white']
print( colors[3:] ) # ['black', 'white']
</source>
</syntaxhighlight>
<source lang='python' run>
<syntaxhighlight lang='python' run>
colors = ['red', 'blue', 'green', 'black', 'white']
colors = ['red', 'blue', 'green', 'black', 'white']
print( colors[::2] ) # ['red', 'green', 'white']
print( colors[::2] ) # ['red', 'green', 'white']
print( colors[1::2] ) # ['blue', 'black']
print( colors[1::2] ) # ['blue', 'black']
print( colors[0::3] ) # ['red', 'black']
print( colors[0::3] ) # ['red', 'black']
</source>
</syntaxhighlight>


==예제 2==
==예제 2==
<source lang='python' run>
<syntaxhighlight lang='python' run>
colors1 = ['red', 'blue', 'green']
colors1 = ['red', 'blue', 'green']
colors2 = ['black', 'white']
colors2 = ['black', 'white']
44번째 줄: 44번째 줄:
print( colors2 * 2 )      # ['black', 'white', 'black', 'white']
print( colors2 * 2 )      # ['black', 'white', 'black', 'white']
print( 2 * colors2 )      # ['black', 'white', 'black', 'white']
print( 2 * colors2 )      # ['black', 'white', 'black', 'white']
</source>
</syntaxhighlight>


==메소드==
==메소드==
50번째 줄: 50번째 줄:


{{소스헤더|append() - 요소 추가}}
{{소스헤더|append() - 요소 추가}}
<source lang='python' run>
<syntaxhighlight lang='python' run>
numbers = [0, 1, 2, 3, 4, 5]
numbers = [0, 1, 2, 3, 4, 5]
numbers.append(6)
numbers.append(6)
print( numbers ) # [0, 1, 2, 3, 4, 5, 6]
print( numbers ) # [0, 1, 2, 3, 4, 5, 6]
</source>
</syntaxhighlight>


{{소스헤더|sort() - 정렬}}
{{소스헤더|sort() - 정렬}}
<source lang='python' run>
<syntaxhighlight lang='python' run>
lst = ['c', 'a', 'b']
lst = ['c', 'a', 'b']
lst.sort()
lst.sort()
print( lst ) # ['a', 'b', 'c']
print( lst ) # ['a', 'b', 'c']
</source>
</syntaxhighlight>


==리스트 길이==
==리스트 길이==
{{참고|Python len()}}
{{참고|Python len()}}
{{소스헤더|len() - 리스트 길이}}
{{소스헤더|len() - 리스트 길이}}
<source lang='python'>
<syntaxhighlight lang='python'>
>>> letters = ['a', 'b', 'c', 'd']
>>> letters = ['a', 'b', 'c', 'd']
>>> len(letters)
>>> len(letters)
4
4
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

2021년 4월 14일 (수) 16:00 판

1 개념

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

2 예제 1

colors = ['red', 'blue', 'green', 'black', 'white']
print( colors ) # ['red', 'blue', 'green', 'black', 'white']
colors = ['red', 'blue', 'green', 'black', 'white']
print( colors[2] )  # green
print( colors[-1] ) # white
print( colors[-2] ) # black
colors = ['red', 'blue', 'green', 'black', 'white']
print( colors[2:2] ) # []
print( colors[2:3] ) # ['green'] 
print( colors[2:4] ) # ['green', 'black']
colors = ['red', 'blue', 'green', 'black', 'white']
print( colors[:3] ) # ['red', 'blue', 'green']
print( colors[3:] ) # ['black', 'white']
colors = ['red', 'blue', 'green', '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)
print( numbers ) # [0, 1, 2, 3, 4, 5, 6]
sort() - 정렬
lst = ['c', 'a', 'b']
lst.sort()
print( lst ) # ['a', 'b', 'c']

5 리스트 길이

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

6 같이 보기

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