Input/python

[python] 파이썬 공식홈페이지 dictionary view objects : dict.keys() / dict.values() / dict.items()

buji-learn 2023. 4. 18. 16:30

  알고리즘 문제를 풀다보면 dictionary의 key와 value값을 list형으로 따로 만들어 활용할 때가 많다. 매번 구글링해서 사용했었는데 파이썬 공식홈페이지에 들어가서 문서를 읽고 정리했다. 내 나름대로 정리해놓으면 나중에 참고하기도 쉽고, 무엇보다 정리하면서 외워져서 좋다. 구글링을 하면 다 찾을 수 있지만, 자주 사용하는 건 외워놓는 게 좋을 테니까.

string constants 복습

import string

uppercase = string.ascii_uppercase

# uppercase
# 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

 

 

[python] 파이썬 공식홈페이지 파고들기 string constants : ascii_letters, digits, punctuation

여태 필요하면 구글에 검색해서 사용하다가 파이썬 공식 문서를 보면서 정리해봤다. 역시 공식 홈페이지에는 뭐가 많다. String constants (문자열 상수) string.ascii_letters : 소문자 a ~ z + 대문자 A ~ Z st

buji-learn.com

대문자 사전 만들기

upper_dict = index : 대문자 알파벳 

upper_dict = {}
for index in range(len(uppercase)):
  upper_dict[index] = uppercase[index]

print 결과

 

  • upper_dict = {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E', 5: 'F', 6: 'G', 7: 'H', 8: 'I', 9: 'J', 10: 'K', 11: 'L', 12: 'M', 13: 'N', 14: 'O', 15: 'P', 16: 'Q', 17: 'R', 18: 'S', 19: 'T', 20: 'U', 21: 'V', 22: 'W', 23: 'X', 24: 'Y', 25: 'Z'}

dict.keys() / dict.values() / dict.items()

index = upper_dict.keys()
alphabets = upper_dict.values()
pairs = upper_dict.items()

print 결과

  • index = dict_keys([0, 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])
  • alphabets = dict_values(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'])
  • pairs = dict_items([(0, 'A'), (1, 'B'), (2, 'C'), (3, 'D'), (4, 'E'), (5, 'F'), (6, 'G'), (7, 'H'), (8, 'I'), (9, 'J'), (10, 'K'), (11, 'L'), (12, 'M'), (13, 'N'), (14, 'O'), (15, 'P'), (16, 'Q'), (17, 'R'), (18, 'S'), (19, 'T'), (20, 'U'), (21, 'V'), (22, 'W'), (23, 'X'), (24, 'Y'), (25, 'Z')])

리스트 형 변환

현재 index, alphabets, pairs 는 list형이 아니다. 따라서 'append' 등으로 list처럼 사용하면  'dict_values' object has no attribute 'append' 와 같은 에러메세지가 뜬다. 해결방법은 list(____)로 감싸서 형 변환을 해주면 된다.

alphabets_list = list(alphabets)

print(type(alphabets_list))    # <class 'list'>
print(alphabets_list)          
# ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

alphabets_list.append('AZ')
print(alphabets_list)          
# ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AZ']

pop(key[, default]) / popitem()

# pop(key[, default])

result_pop = upper_dict.pop(12)
print(result_pop)               # 'M'

# popitem()
# Remove and return a (key, value) pair from the dictionary. 
# Pairs are returned in LIFO order.
# LIFO : Late In, First Out

result_popItem = upper_dict.popitem()
print(result_popItem)           # (25, 'Z')

pop()이나 popitem()을 한 다음, upper_dict을 출력하면 12 : 'M' 과 25 : 'Z'가 빠져있음을 확인할 수 있다.

# del 함수 사용
# del dictionary[key]
# del dictionary[value]  >>  Error

del upper_dict[4]

upper_dict을 출력하면 4 : 'D'가 빠져있음을 확인할 수 있다.

 

Built-in Types

The following sections describe the standard types that are built into the interpreter. The principal built-in types are numerics, sequences, mappings, classes, instances and exceptions. Some colle...

docs.python.org