일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- gas
- Google Spreadsheet
- numpy
- c#
- dataframe
- SQL
- Redshift
- string
- Kotlin
- PANDAS
- array
- Mac
- Python
- math
- Google Excel
- django
- GIT
- hive
- Excel
- matplotlib
- list
- PostgreSQL
- Java
- google apps script
- PySpark
- Github
- Tkinter
- Apache
- 파이썬
- Today
- Total
목록Dictionary (13)
달나라 노트
Syntax dictionary.keys() dictionary에 존재하는 모든 key를 return 합니다. dictionary.values() dictionary에 존재하는 모든 value를 return 합니다. dictionary.items() dictionary를 구성하는 모든 key, value 값을 tuple로 묶어서 return 합니다. 예시를 봅시다. dict_test = { 'key1': 1, 'key2': 2, 'key3': 3 } print(dict_test.keys()) -- Result dict_keys(['key1', 'key2', 'key3']) - dict_test.keys() dict_test에 keys() method를 적용했습니다. 그 결과 값을 보니 dict_keys(..
Syntax dictionary.setdefault(key, default_value) setdefault method는 dictionary에 적용할 수 있는 method이며 2개의 인자를 받습니다. - key setdefault method는 전달받은 key값에 대한 value를 dictionary에서 찾아 return 해줍니다. - default_value 만약 전달받은 key값이 dictionary에 없을 경우 default_value를 return합니다. dict_test = { 'key1': 1, 'key2': 2, 'key3': 3 } val_result_1 = dict_test.setdefault('key1', 10) print(val_result_1) -- Result 1 위 예시를 봅시다..
dictionary의 값들을 정렬하는 방법에 대해 알아보겠습니다. dict_test = { 3: 'c', 5: 'a', 4: 'b', 1: 'e', 2: 'd', } list_test_sorted = sorted(dict_test.items(), key=lambda x: x[0], reverse=False) dict_test_sorted = dict(list_test_sorted) print(dict_test_sorted) -- Result {1: 'e', 2: 'd', 3: 'c', 4: 'b', 5: 'a'} 위 예시를 봅시다. 초기 dict_test에 정의된 값들은 key가 1, 2, 3, 4, 5가 있는데 전혀 정렬이 되어있지 않습니다. 그래서 sorted method를 이용해서 정렬을 했더니 결..
dict_test = { 'a': 1, 'b': 2, 'c': 3, } sample_value = dict_test['d'] print(sample_value) -- Result KeyError: 'd' 위 코드를 실행시켜보면 KeyError가 발생합니다. 왜냐면 dict_test라는 dictionary에는 a, b, c라는 key가 존재하는데, 존재하지 않는 d라는 이름의 key를 전달했기 때문이죠. 위 예시에서처럼 간단한 dictionary를 사용하면 사실 KeyError가 발생할 일도 거의 없고, KeyError가 발생한다고 해도 금방 코드를 수정할 수 있습니다. 하지만 dictionary가 복잡해지고 경우에 따라 key의 종류가 변동될 수 있는 상황에서 dictionary의 key를 전달해야한다면..