저는 파이썬 2.7을 사용하고 있습니다. 여전히 사전에 대해 배우고 있습니다. 사전에 대한 수치 계산을 수행하는 데 중점을두고 있으며 도움이 필요합니다.사전의 제곱 값
나는 사전을 가지고 있고 그 안에 값을 제곱하고 싶습니다 :
dict1 = {'dog': {'shepherd': 5,'collie': 15,'poodle': 3,'terrier': 20},
'cat': {'siamese': 3,'persian': 2,'dsh': 16,'dls': 16},
'bird': {'budgie': 20,'finch': 35,'cockatoo': 1,'parrot': 2}
을 내가 원하는 :
dict1 = {'dog': {'shepherd': 25,'collie': 225,'poodle': 9,'terrier': 400},
'cat': {'siamese': 9,'persian': 4,'dsh': 256,'dls': 256},
'bird': {'budgie': 400,'finch': 1225,'cockatoo': 1,'parrot': 4}
내가 시도 :
dict1_squared = dict**2.
dict1_squared = pow(dict,2.)
dict1_squared = {key: pow(value,2.) for key, value in dict1.items()}
내가하지 않았다 내 시도로 성공. 당신이 사전을 중첩 때문에
당신은 루프 (2 단계 깊이)을 반복해야합니다. 그렇다면 스스로 시간을 할애해야합니다. –