greenhelix
greenhelix
greenhelix
05-30 15:12
  • All (229)
    • Algorithm (118)
      • Algorithm (17)
      • Graph (0)
      • Core (6)
      • Python (18)
      • PythonSnippet (4)
      • Java (59)
      • Kotlin (14)
    • Project (0)
    • Study (8)
      • License (5)
      • EIP (3)
    • Programming (63)
      • Android (41)
      • Flutter (1)
      • Bugs Life (21)
      • Linux (0)
    • Tech (32)
      • Tech (17)
      • Drone (4)
      • Hacking (11)
    • Life (6)
      • INGRESS (1)
      • 심시티빌드잇 (0)
250x250

티스토리

hELLO · Designed By 정상우.
greenhelix

greenhelix

Dict(Dict())  표현
Algorithm/PythonSnippet

Dict(Dict()) 표현

2021. 5. 16. 17:47

딕셔너리 참고 사이트 

 

fares = [[4, 1, 10], [3, 5, 24], [5, 6, 2], [3, 1, 41], [
    5, 1, 24], [4, 6, 50], [2, 4, 66], [2, 3, 22], [1, 6, 25]]


def solution(fares):
    total = 0
    matrix = {i+1: dict() for i in range(n)}

    for i, j, k in fares:
        matrix[i].update({j: k})
        matrix[j].update({i: k})

    print(matrix)
    print(*matrix.items(), sep='\n')

    return total


solution(n, s, a, b, fares)

>>> {1: {4: 10, 3: 41, 5: 24, 6: 25}, 2: {4: 66, 3: 22}, 3: {5: 24, 1: 41, 2: 22}, 4: {1: 10, 6: 50, 2: 66}, 5: {3: 24, 6: 2, 1: 24}, 6: {5: 2, 4: 50, 1: 25}}

>>> (1, {4: 10, 3: 41, 5: 24, 6: 25})
>>> (2, {4: 66, 3: 22})
>>> (3, {5: 24, 1: 41, 2: 22})       
>>> (4, {1: 10, 6: 50, 2: 66})       
>>> (5, {3: 24, 6: 2, 1: 24})        
>>> (6, {5: 2, 4: 50, 1: 25})    

 

728x90
반응형
저작자표시 비영리 변경금지 (새창열림)

'Algorithm > PythonSnippet' 카테고리의 다른 글

합집합, 교집합, 차집합, 대칭 차집합 표현하기  (0) 2021.05.11
Defaultdict, dict sort 딕셔너리 정렬방법  (0) 2021.05.06
How to list in dict ? 리스트를 딕셔너리로  (0) 2021.05.06
    'Algorithm/PythonSnippet' 카테고리의 다른 글
    • 합집합, 교집합, 차집합, 대칭 차집합 표현하기
    • Defaultdict, dict sort 딕셔너리 정렬방법
    • How to list in dict ? 리스트를 딕셔너리로
    greenhelix
    greenhelix
    개발에 관한 것들과 개인적인 것을 담는 블로그

    티스토리툴바