Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
Tags
- mongoose
- OOAD
- macos
- ubuntu
- MongoDB
- linux
- typeorm
- python
- Android
- wireshark
- algorithm
- S3
- HTML
- Crawling
- DATABASE
- Network
- TypeScript
- AWS
- Express
- Scheduling
- docker
- Kotlin
- mysql
- postman
- OS
- sequelize
- React
- css
- Util
- node.js
Archives
- Today
- Total
SW
[Python] 리스트, 튜플, 셋, 딕셔너리 정리하기 본문
반응형
[Python] 리스트, 튜플, 셋, 딕셔너리 정리하기
Python Collections
- 리스트, 튜플, 셋, 딕셔너리 4가지가 존재
- List : 원소들이 순서가 있는 상태로 나열되어 있는 것
- Set : 원소들이 순서가 없는 상태로 나열되어 있는 것
- 인덱스 접근이 불가능하다.
- Tuple : 리스트와 비하지만 원소들의 값을 바꿀 수 없다는 특징이 있음
- Dictionary : key - value 쌍으로 이루어져 있고, 순서가 없다.
- key 값을 기준으로 인덱싱을 하기 때문이다.
List
- Constructor 을 통해서 생성할 수 있다.
- 서로 다른 타입의 데이터를 담을 수 있다.
- 가변적이고 순차적인 특징을 가지고 있다.
# list 라는 메소드를 통해서 만든 예시
thislist = list(("apple", "banana", "cherry"))
# 이렇게 할당을 통해 만들 수도 있음
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
# 서로 다른 타입의 데이터가 올 수 있다.
thislist = [1, 2, "apple", True, False]
리스트에서 사용할 수 있는 내장 메소드는 다음과 같다.
MethodDescription| append() | Adds an element at the end of the list |
| clear() | Removes all the elements from the list |
| copy() | Returns a copy of the list |
| count() | Returns the number of elements with the specified value |
| extend() | Add the elements of a list (or any iterable), to the end of the current list |
| index() | Returns the index of the first element with the specified value |
| insert() | Adds an element at the specified position |
| pop() | Removes the element at the specified position |
| remove() | Removes the first item with the specified value |
| reverse() | Reverses the order of the list |
| sort() | Sorts the list |
Tuple
- 소괄호를 사용하여 만들 수 있다.
- count(), index() 메소드만 사용 가능하다.
- 인덱싱 접근이 가능하다.
- 데이터 변경이 불가능하다.
- thistuple[0] = "melon" 이런 식으로 바꿀 수 없다.
thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
튜플에서 사용할 수 있는 내장 메소드는 다음과 같다.
MethodDescription| count() | Returns the number of times a specified value occurs in a tuple |
| index() | Searches the tuple for a specified value and returns the position of where it was found |
Set
- 대괄호를 사용하여 만든다.
- 인덱스 접근이 불가능하다. ⇒ 위치가 없다. ⇒ 멤버 하나하나를 부르는 수 밖에 없다.
- 데이터 중복이 불가능한 자료형이다.
- 합집합, 교집합 등과 같은 수학적 연산이 가능하다.
- 가변적인 특징을 가지고 있다.
# 할당을 통해서 만들 수 있다.
thisset = {"apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"}
# 생성자를 통해서도 만들 수 있다.
thisset = set(("apple","banana","cherry"))
셋에서 사용할 수 있는 내장 메소드는 다음과 같다.
MethodDescription| add() | Adds an element to the set |
| clear() | Removes all the elements from the set |
| copy() | Returns a copy of the set |
| difference() | Returns a set containing the difference between two or more sets |
| difference_update() | Removes the items in this set that are also included in another, specified set |
| discard() | Remove the specified item |
| intersection() | Returns a set, that is the intersection of two or more sets |
| intersection_update() | Removes the items in this set that are not present in other, specified set(s) |
| isdisjoint() | Returns whether two sets have a intersection or not |
| issubset() | Returns whether another set contains this set or not |
| issuperset() | Returns whether this set contains another set or not |
| pop() | Removes an element from the set |
| remove() | Removes the specified element |
| symmetric_difference() | Returns a set with the symmetric differences of two sets |
| symmetric_difference_update() | inserts the symmetric differences from this set and another |
| union() | Return a set containing the union of sets |
| update() | Update the set with another set, or any other iterable |
Dictionary
- Key 중복을 허용하지 않는 자료형이다.
- .keys(), .values(), .itmes() 명령어를 이용하여 유용하게 파싱이 가능하다.
- 가변적인 특징을 가지고 있다.
# 생성자를 이용하여 생성할 수 있다.
thisdict = dict(brand = "Ford", model = "Mustang", year = 1964)
#할당을 통해서도 생성할 수 있다.
thisdict = {"brand": "Ford", "model": "Mustang", "year": 1964}
딕셔너리에서 사용할 수 있는 내장 메소드는 다음과 같다.
MethodDescription| clear() | Removes all the elements from the dictionary |
| copy() | Returns a copy of the dictionary |
| fromkeys() | Returns a dictionary with the specified keys and value |
| get() | Returns the value of the specified key |
| items() | Returns a list containing a tuple for each key value pair |
| keys() | Returns a list containing the dictionary's keys |
| pop() | Removes the element with the specified key |
| popitem() | Removes the last inserted key-value pair |
| setdefault() | Returns the value of the specified key. If the key does not exist: insert the key, with the specified value |
| update() | Updates the dictionary with the specified key-value pairs |
| values() | Returns a list of all the values in the dictionary |
출처
'Python' 카테고리의 다른 글
| [Python] 크롤링으로 네이버 국내 증시 코스피 항목 추출하기 (0) | 2022.05.08 |
|---|---|
| [Python] CSS selector를 이용하여 Daum 뉴스기사 제목 크롤링하기 (0) | 2022.05.08 |
| [Python] HTML 코드를 이해하여 크롤링 다루기 (0) | 2022.05.08 |
| [Python] 크롤링이란? 예시로 크롤링 이해하기 (0) | 2022.05.08 |
| Fast API란? 튜토리얼을 통해 이해하기 (0) | 2022.05.07 |
Comments