2017-12-01 2 views
0

그러나 많은 노력을 기울 였지만 여전히 함수에서 사전을 반환하는 데 문제가 있습니다. 예를 들어 함수 내부에서 d1과 d2를 인쇄 할 수 있지만 외부에서는 인쇄 할 수 없습니다. 다음 스크립트를 사용할 때 NameError : name 'd1'이 정의되지 않았습니다. 도와 줘서 고마워!python return dictionary from function

ref = """text a""" 

target = """text b""" 

def text_to_dict(x): 
    # value formatting to list = new_values 
    # key formatting to list = keys 
    # dict creation from keys and new_values 
    if x == ref: 
     d1 = dict(zip(keys, new_values)) 
     return d1 
    elif x == target: 
     d2 = dict(zip(keys, new_values)) 
     return d2 

text_to_dict(ref) 

text_to_dict(target) 

print(d1) 

print(d2) 

답변

1

d1 및 d2는 함수 변수이므로 함수 밖에서는 볼 수 없습니다.

dict1 = text_to_dict(ref) 

dict2 = text_to_dict(target) 

print(dict1) 

print(dict2)