5.1. More on Lists
리스트에 대하여
The list data type has some more methods. Here are all of the methods of list objects:
'리스트'라는 데이터 유형은 다양한 '메소드'를 가지고 있습니다. 이 장은 리스트의 모든 메소드를 다룹니다.
method : 해당 데이터 유형(=객체)안에 미리 저장되어 있는 함수
list .append(x) : x = an item
Add an item to the end of the list.
Equivalent toa[len(a):] = [x]
.
.append( )는 리스트의 마지막에 괄호 안에 주어진 '하나의 항목'을 추가합니다.a[len(a):]=[x]
와 같습니다.
list .extend(L) : L = List
Extend the list by appending all the items in the given list.
Equivalent toa[len(a):]=L
.extend( )는 괄호 안에 주어진 '목록'에 있는 모든 항목을 추가하여 리스트를 확장합니다.a[len(a):]=L
과 같습니다.
list .insert(i,x) : i = index, x = an item
Insert an item at a given position. The first argument is the index of the element before which to insert, so
a.insert(0, x)
inserts at the front of the list, anda.insert(len(a), x)
is equivalent toa.append(x)
.
.insert( )는 괄호안에 주어진 '하나의 항목'을 리스트 안의 '지정된 위치'에 집어넣습니다. 괄호 안에 주어지는 첫번째 인수는 삽입될 항목이 가지게 될 인덱스를 나타냅니다. 따라서a.insert(0, x)
의 경우, x는 리스트의 가장 앞에 삽입됩니다. 그리고a.insert(len(a),x)
의 경우 x는 리스트의 가장 마지막에 삽입되므로,a.append(x)
와 같습니다.
list .remove(x) : x = an item
Remove the first item from the list whose value is x. It is an error if there is no such item.
.remove( )는 괄호안에 주어진 항목과 같은 첫번째 항목을 리스트에서 삭제합니다. 주어진 항목과 같은 것이 리스트 안에 없으면 오류가 생깁니다.
list.pop[i] : i = index
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)
.pop[ ] 은 대괄호 안에 지정된 인덱스의 위치에 있는 항목을 리스트에서 지우고, 그것을 반환합니다. 만약 대괄호 안에 인덱스가 지정되지 않으면, 리스트의 가장 마지막 항목을 지운다음 반환합니다. (i 를 둘러싼 대괄호는 이 메소드에서 매개변수-즉, 인덱스를 지정하는 것이 선택사항이라는 것을 나타냅니다. .pop[ ]에 반드시 대괄호를 사용해야 한다는 뜻이 아니지요. 이러한 표기법은 파이썬 라이브러리 참고자료에서 종종 볼 수 있습니다.)
list.clear()
Remove all items from the list.
Equivalent todel a[:]
리스트 안의 모든 아이템을 삭제합니다.del a[:]
와 같습니다.
list .index(x) : x = value
Return the index in the list of the first item whose value is x. It is an error if there is no such item. .index( )는 리스트 안에서 첫번째로 괄호 안에 주어진 항목과 같은 항목의 인덱스를 알려줍니다. 그러한 항목이 없으면 오류가 발생합니다.
list .count(x) : x = value
Return the number of times x appears in the list
.conunt( )는 괄호 안에 주어진 값이 리스트 안에 몇 개나 존재하는지 알려줍니다.
list .sort( key=None, reverse=False )
Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation).
.sort( )는 리스트 안의 항목을 정렬합니다. 인수를 사용하면 정렬방식을 원하는 대로 조정할 수 있습니다. 정렬방식을 조정하고 싶으면 sorted( )의 설명을 참고하십시오.
list .reverse()
Reverse the elements of the list in place. 리스트 안의 요소들을 뒤집습니다.
list .copy()
Return a shallow copy of the list.
Equivalent toa[:]
리스트의 복사본을 만듭니다.a[:]
와 같습니다.
An example that uses most of the list methods:
예시를 참고하십시오.
>>> a = [66.25, 333, 333, 1, 1234.5]
>>> print(a.count(333), a.count(66.25), a.count('x'))
2 1 0
>>> a.insert(2, -1)
>>> a.append(333)
>>> a
[66.25, 333, -1, 333, 1, 1234.5, 333]
>>> a.index(333)
1
>>> a.remove(333)
>>> a
[66.25, -1, 333, 1, 1234.5, 333]
>>> a.reverse()
>>> a
[333, 1234.5, 1, 333, -1, 66.25]
>>> a.sort()
>>> a
[-1, 1, 66.25, 333, 333, 1234.5]
>>> a.pop()
1234.5
>>> a
[-1, 1, 66.25, 333, 333]
You might have noticed that methods like insert
, remove
or sort
that only modify the list have no return value printed – they return the default None
. 1 This is a design principle for all mutable data structures in Python.insert
, remove
, sort
와 같은 메소드들은 오로지 리스트를 수정할 뿐, 그 수정된 내용에 대해 따로 나타내 주지 않는다는 것을 기억합시다. 이 메소드들은 기본설정값인 None
을 보여줄 뿐 입니다. 이것은 파이썬의 모든 가변형 데이터 구조에 대한 디자인원칙입니다.
각주1
Other languages may return the mutated object, which allows method chaining, such as d->insert("a")->remove("b")->sort();
.
다른 프로그래밍 언어는 변형된 객체를 나타내 줄 수도 있습니다. 다음과 같은 방식으로 말입니다.d->insert("a")->remove("b")->sort();
.