2014-09-28 2 views
0

그래서 사전에서 사전의 사전을 반복해야합니다. 기본적으로 나는 사전에이 같은 정보를 저장하고있다 : 예를 들어이 내가 지금까지 가지고있는, 그래서 함수에서 값을 반복 함

accounts = {} 

def accountcreator(): 
    newusername = raw_input() 
    newpassword = raw_input() 
    UUID = 0 
    UUID += 1 
    accounts[newusername] = {newpassword:UUID} 

그런 다음 다른 함수에 나는이 모든 값을 통해 루프합니다. 이렇게하면 모든 새 사용자 이름이 올바르게 반복됩니다.

def accounts(): 
    for usernames in accounts: 
    #I do not know what to do from here on out 
    #I want it to loop through all of the newpasswords and UUID 
    #And the UUIDs would be saved to a new variable 

모든 값을 반복하는 간단한 방법을 알려주십시오. 감사합니다!

def accountcreator(): 
    newusername = raw_input() #For raw input I put in cool-account-name 
    newpassword = raw_input() #For raw input I put in this-is-a-password 
    UUID = 0 
    UUID += 1 
    accounts[newusername] = {newpassword:UUID} #So basically what is being saved is accounts[cool-account-name] = {this-is-a-password:1} 

그래서 그런 일이 후 나는이 계정 기능을 발생하려면 :

편집 그래서 기본적으로 이것은 예입니다. 각각의 개별 항목을 인쇄하기를 원하므로 기본적으로 사용자 이름, 암호 및 UUID를 각각 인쇄합니다. 위의 정보와 함께 제공되므로 사용자 이름 : cool-account-name, 암호 : this-is-a-password 및 UUID : 1을 출력합니다.

+0

[이 질문에 대한 답변보기 (http://stackoverflow.com/questions/3294889/iterating-over-dictionary-for-loops-in-python)을 참조하십시오. dict를 반복하는 것과 비슷한 문제 – fluidmotion

답변

0

계정의 값에 다른 루프를 추가하기 만하면됩니다 [ 당신이 .values ​​() 또는 .keys를 사용해야합니다 있도록 사용자 이름]

def accounts(): 
    for usernames in accounts: 
    for passwords in accounts[usernames]: 
     # Here you can access the UUID you want through: accounts[usernames][passwords] 
0

사전은,리스트 다르게 작동()을 반복합니다.

accounts.keys()은 사전에 모든 키를 반환 : 또한 각 기능에 global accounts 줄을 추가해야한다

d = {1:2,3:4} 

for v in d.values(): 
    print (v) 
# Would print 2 and 4 

:

d = {1:2,3:4} 

for v in d.keys(): 
    print (v) 
# Would print 1 and 3 

# And 
for v in d.keys(): 
    print (d[v]) 
# Would print 2 and 4 

accounts.values() 사전에 그 키의 모든 값을 반환 따라서 외부에서 정의 된 accounts 변수에 액세스 할 수 있습니다. 그렇지 않으면 각 함수가 자체 계정 변수를 만들거나 오류가 발생합니다.