데이터 매핑

CODEDRAGON Development/Python

반응형



 

 

데이터 매핑

·       format 인자들의 인덱스 사용

·       format 인자들의 key 사용

·       format 인자로 Dictionary 사용

 

 

 

format 인자들의 인덱스 사용

·       문자열 내에서 어떤 값이 들어가길 원하는 곳은 {}로 표시를 합니다.

·       {} 안의 숫자는 숫서를 의미하며, format 인자들의 인덱스를 사용하여 값을 매칭시킵니다.

 

{0}는 첫 번째 인자인 "apple"을 나타내고 {1}은 두 번째 인자인 "red"를 나타냅니다.

>>> print("{0} is {1}".format("apple", "red"))
apple is red

 

 

 

 

 

 

format 인자들의 key 사용

{} 안의 값을 지정할 때 인덱스 대신 format의 인자로 키(key)와 값(value)을 주어 인덱스 사용과 동일한 결과를 얻을 수 있습니다.

 

>>> print("{item} is {color}".format(item="apple", color="red"))
apple is red

 

 

 

 

 

 

format 인자로 Dictionary 사용

dictionary를 입력으로 받아 사용하는 경우입니다.

 

>>> dic = {"item":"apple", "color":"red"}

>>> print("{0[item]} is {0[color]}".format(dic))
apple is red

 

 

 

 

** 기호를 사용

** 기호를 사용하면 dictionary를 입력으로 받은 것으로 판단하고 인자를 하나만 받아 불필요한 index는 생략할 수 있습니다.

 

>>> print("{item} is {color}".format(**dic))
apple is red

 

 


반응형

'Development > Python' 카테고리의 다른 글

[Python] 제어문  (0) 2020.01.17
input()함수  (0) 2020.01.17
포맷팅(formatting)  (0) 2020.01.17
print() 형식  (0) 2020.01.17
멤버쉽 연산자 - in, not in  (0) 2020.01.17