|
| 1 | +# 3.10.1. 리스트\(Lists\) |
| 2 | + |
| 3 | +리스트는 대괄호 사이에 쉼표로 구분된 값\(item\)들의 목록으로 작성될 수 있는 파이썬에서 가장 다재 다능한 데이터 유형입니다. 리스트는 목록이라는 뜻으로, 다양한 데이터를 담을 수 있고 내용을 변경할 수 있는 시퀀스입니다. 리스트의 중요한 점은 리스트의 항목이 동일한 유형 일 필요는 없다는 것입니다. 리스트의 인덱스는 0부터 시작합니다. |
| 4 | + |
| 5 | +리스트를 만들 때는 위에서 보는 것과 같이 대괄호\(\[ \]\)로 감싸 주고 각 항목 값들은 쉼표\(,\)로 구분해 줍니다. |
| 6 | + |
| 7 | +```python |
| 8 | +list1 = ['physics', 'chemistry', 1997, 2000]; |
| 9 | +list2 = [1, 2, 3, 4, 5 ]; |
| 10 | +list3 = ["a", "b", "c", "d"] |
| 11 | +list4 = [1, 2, 'Life', 'is'] |
| 12 | +list5 = [1, 2, ['Life', 'is']] |
| 13 | +``` |
| 14 | + |
| 15 | +리스트의 각 요소들을 액세스하려면 대괄호를 사용하여 색인과 함께 슬라이싱하고 해당 색인에 있는 값을 구하면 됩니다. \(\* 슬라이싱: 범위를 정해 선택하기\) |
| 16 | + |
| 17 | +```python |
| 18 | +list1 = ['physics', 'chemistry', 1997, 2000]; |
| 19 | +list2 = [1, 2, 3, 4, 5, 6, 7 ]; |
| 20 | +print "list1[0]: ", list1[0] |
| 21 | +print "list2[1:5]: ", list2[1:5] |
| 22 | +``` |
| 23 | + |
| 24 | +대입 연산자 = 의 왼쪽에 리스트\[인덱스\] 를 제공하여 목록의 단일 또는 여러 요소를 업데이트 할 수 있으며 append\(\) 메서드를 사용하여 목록의 요소에 추가할 수 있습니다. 예를 들어 - |
| 25 | + |
| 26 | +```python |
| 27 | +list = ['physics', 'chemistry', 1997, 2000]; |
| 28 | +print "Value available at index 2 : " |
| 29 | +print list[2] |
| 30 | +list[2] = 2001; |
| 31 | +print "New value available at index 2 : " |
| 32 | +print list[2] |
| 33 | +``` |
| 34 | + |
| 35 | +목록 요소를 제거하려면 삭제할 요소를 정확히 알고 있는 경우 del 문을 사용하고, 모르는 경우 remove\(\) 메서드를 사용할 수 있습니다. 예를 들어 - |
| 36 | + |
| 37 | +```python |
| 38 | +list1 = ['physics', 'chemistry', 1997, 2000]; |
| 39 | +print list1 |
| 40 | +del list1[2]; |
| 41 | +print "After deleting value at index 2 : " |
| 42 | +print list1 |
| 43 | +``` |
| 44 | + |
| 45 | +리스트는 문자열과 매우 비슷하게 +와 \* 연산자를 사용합니다. 연산의 결과가 새로운 목록이지 문자열이 아니라는 것을 제외하고 + 연산자는 연결, \* 연산자는 반복을 의미합니다. |
| 46 | + |
| 47 | +| **Python Expression** | **Results** | **Description** | |
| 48 | +| :--- | :--- | :--- | |
| 49 | +| len\(\[1, 2, 3\]\) | 3 | Length | |
| 50 | +| \[1, 2, 3\] + \[4, 5, 6\] | \[1, 2, 3, 4, 5, 6\] | Concatenation | |
| 51 | +| \['Hi!'\] \* 4 | \['Hi!', 'Hi!', 'Hi!', 'Hi!'\] | Repetition | |
| 52 | +| 3 in \[1, 2, 3\] | True | Membership | |
| 53 | +| for x in \[1, 2, 3\]: print x, | 1 2 3 | Iteration | |
| 54 | + |
| 55 | +리스트는 시퀀스이므로 문자열과 동일한 방식으로 인덱싱과 슬라이싱 동작을 합니다. 파이썬에는 다음과 같은 리스트 함수와 메서드들이 있습니다. |
| 56 | + |
| 57 | +| [**cmp\(list1, list2\)**](https://www.tutorialspoint.com/python/list_cmp.htm) 두 리스트의 요소를 비교합니다. | |
| 58 | +| :--- | |
| 59 | +| [**len\(list\)**](https://www.tutorialspoint.com/python/list_len.htm) 리스트의 전체 길이 | |
| 60 | +| [**max\(list\)**](https://www.tutorialspoint.com/python/list_max.htm) 리스트에서 최대 값을 가진 항목을 반환 | |
| 61 | +| [**min\(list\)**](https://www.tutorialspoint.com/python/list_min.htm) 리스트에서 최대 값을 가진 항목을 반환 | |
| 62 | +| [**list\(seq\)**](https://www.tutorialspoint.com/python/list_list.htm) 튜플을 리스트로으로 변환 | |
| 63 | +| [**list.append\(obj\)**](https://www.tutorialspoint.com/python/list_append.htm) obj 오브젝트를리스트에 추가 | |
| 64 | +| [**list.count\(obj\)**](https://www.tutorialspoint.com/python/list_count.htm) 리스트에 obj가 발생한 횟수를 반환 | |
| 65 | +| [**list.extend\(seq\)**](https://www.tutorialspoint.com/python/list_extend.htm) 리스트에 seq의 내용을 추가 | |
| 66 | +| [**list.index\(obj\)**](https://www.tutorialspoint.com/python/list_index.htm) obj가 나타나는 리스트 내의 가장 작은 인덱스를 리턴 | |
| 67 | +| [**list.insert\(index, obj\)**](https://www.tutorialspoint.com/python/list_insert.htm) 리스트에 객체 obj를 오프셋 index 위치에 삽입 | |
| 68 | +| [**list.pop\(obj=list\[-1\]\)**](https://www.tutorialspoint.com/python/list_pop.htm) 리스트에서 마지막 객체 또는 obj를 제거하여 반환. | |
| 69 | +| [**list.remove\(obj\)**](https://www.tutorialspoint.com/python/list_remove.htm) 오브젝트 obj를 리스트로부터 삭제 | |
| 70 | +| [**list.reverse\(\)**](https://www.tutorialspoint.com/python/list_reverse.htm) 리스트의 대상을 반전 | |
| 71 | +| [**list.sort\(\[func\]\)**](https://www.tutorialspoint.com/python/list_sort.htm) 리스트의 객체를 정렬하고, 주어진 compare \[func\]를 사용합니다. | |
| 72 | + |
0 commit comments