Python/Python Lib

파이썬 : 딕셔너리, 리스트

heheh 2020. 12. 23. 19:18

 Dictionary 

"키 - 값" 쌍으로 구성되며 키를 통해 값을 쉽게 찾을수 있도록 한다.

dict = {}
dict = ['key'] = 'value'

print(dict)
# {'key' : 'value'}

 

 

메서드
  • keys(), values(), items()
print(dict.keys())
# 
print(dict.values())
#
print(dict.items())
# [('key', 'value')]

 

  • get()

dict["kkeeyy"]를 실행할 경우 에러가 발생한다.

print( dict.get("kkeeyy") )
# None

 

 

  • update()

여러 데이터를 갱신할 때 사용한다.

dict.update({'key2' : 'value2' , 'key3' : 'value3'})
print(dict)
# {'key' : 'value' , 'key2' : 'value2' , 'key3' : 'value3'}

 

 List 

  • 리스트 원소를 띄어쓰기 없이 출력하기
print(''.join(listname))

 

  • 정렬된 리스트에서 특정 원소가 들어갈 위치 찾기 
from bisect import bisect_left, bisect_right #인덱스 값을 반환한다.

l = [1,2,3,3,4,5]

bisect_left(l,3) # 2
bisect_right(l,3) # 4
bisect_right - bisect_left # 2 (원소 3의 개수)

 

 

# 데이터 결과값 예쁘게 출력

pp = pprint.PrettyPrinter(indent=4)

print(pp.pprint(contents))