발견하게 된 생소한 문법은 아래와 같은데..

enumerate() 함수를 이용해 이름 목록을 열거하고 인덱스와 함께 

name(키) 와 outputs 라는 구조를 쌍으로 묶어 딕셔너리로 돌려준다.

return {name: outputs[i] for i, name in enumerate(self.output_names)}

 

def create_dict():
    ''' Function to return dict '''
    return {i:str(i) for i in range(10)}
numbers = create_dict()
print(numbers)
# {0: '0', 1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9'}

[링크 : https://blog.finxter.com/python-return-dictionary-from-function/]

 

enumerate() 는 내용과 인덱스를 같이 돌려주고 start 키워드를 이용해 시작 인덱스를 0이 아닌 것으로 설정이 가능하다.

>>> for entry in enumerate(['A', 'B', 'C']):
...     print(entry)
...
(0, 'A')
(1, 'B')
(2, 'C')

[링크 : https://www.daleseo.com/python-enumerate/]

 

enumerate()와 유사하게 두개의 배열을 하나의 쌍으로 묶어주는 함수

>>> numbers = [1, 2, 3]
>>> letters = ["A", "B", "C"]
>>> for pair in zip(numbers, letters):
...     print(pair)
...
(1, 'A')
(2, 'B')
(3, 'C')

[링크 : https://www.daleseo.com/python-zip/]

'Programming > python(파이썬)' 카테고리의 다른 글

python matplotlib 설치  (0) 2023.03.08
python openCV / PIL 포맷 변경  (0) 2022.04.12
python3 opencv2 checker board  (0) 2022.03.14
pdb  (0) 2022.03.14
python debug (pdb)  (0) 2022.03.04
Posted by 구차니