동영상 강좌는 유튜브 채널 '웹학교'를 이용하시기 바랍니다.
Dictionary
Dictionary은 정렬되지 않고 변경 가능하며 색인이 생성 된 콜렉션입니다. dictionary는 중괄호로 쓰여 있으며 키와 값을 가지고 있습니다.
len()메소드를 사용하여 항목수 반환
thisdict = {
"apple": "green",
"banana": "yellow",
"cherry": "red"
}
print(thisdict)
사과색을 'red'로 변경하기
thisdict = {
"apple": "green",
"banana": "yellow",
"cherry": "red"
}
thisdict["apple"] = "red"
print(thisdict)
dict()생성자
dict()생성자를 사용하여 dictionary를 만들 수 있습니다.
thisdict = dict(apple="green", banana="yellow", cherry="red")
# note that keywords are not string literals
# note the use of equals rather than colon for the assignment
print(thisdict)
항목 추가하기
dictionary에 항목을 추가하는 작업은 새 색인 키를 사용하고 값을 할당하여 수행됩니다.
thisdict = dict(apple="green", banana="yellow", cherry="red")
thisdict["damson"] = "purple"
print(thisdict)
항목 제거하기
dictionary 항목 제거는 del() 함수를 사용하여 수행해야 합니다.
thisdict = dict(apple="green", banana="yellow", cherry="red")
del(thisdict["banana"])
print(thisdict)
Dictionary 길이 구하기
len()함수는 dictionary 크기를 반환합니다.
thisdict = dict(apple="green", banana="yellow", cherry="red")
print(len(thisdict))
등록된 댓글이 없습니다.