Python 字典值不';t保持不变
最终值不同于单个键填充期间,当键“0”被填充时的值Python 字典值不';t保持不变,python,Python,最终值不同于单个键填充期间,当键“0”被填充时的值 {0: (0, 0), 1: (1, 0), 2: (2, 0), 3: (3, 0), 4: (4, 0), 5: (5, 0), 6: (6, 0), 7: (7, 0), 8: (8, 0), 9: (9, 0), 10: (10, 0), 11: (11, 0), 12: (12, 0) 等等,;这些值在外部while循环完全完成后更改。它们变成: {0: (0, 0), 1: (0, 0), 2: (-1, 0), 3: (-2
{0: (0, 0), 1: (1, 0), 2: (2, 0), 3: (3, 0), 4: (4, 0),
5: (5, 0), 6: (6, 0), 7: (7, 0), 8: (8, 0), 9: (9, 0),
10: (10, 0), 11: (11, 0), 12: (12, 0)
等等,;这些值在外部while循环完全完成后更改。它们变成:
{0: (0, 0), 1: (0, 0), 2: (-1, 0), 3: (-2, 0), 4: (-3, 0),
5: (-4, 0), 6: (-5, 0), 7: (-6, 0), 8: (-7, 0), 9: (-8, 0),
10: (-9, 0), 11: (-10, 0), 12: (-11, 0)
我不知道为什么。非常感谢您的帮助。您可以取消对print语句的注释以供参考
守则:
import numpy as np
from math import cos,sin,pi
ANGLE_ACCURACY = 0.5
noOfAnglesLimit = (360.0/ANGLE_ACCURACY)*0.5
angle = 0
polarLut = dict()
inner = dict()
while angle<noOfAnglesLimit:
theta = (pi/180.0)*(angle*ANGLE_ACCURACY)
radius = 0
while radius<=200:
x = int(float(radius)*cos(theta)+0.5)
y = int(float(radius)*sin(theta)+0.5)
inner[radius] = (x,y)
radius+=1
polarLut[angle] = inner
#print(polarLut)
angle+=1
import json
with open('file.txt', 'w') as file:
file.write(json.dumps(polarLut))
将numpy导入为np
从数学输入cos,sin,pi
角度_精度=0.5
noOfAnglesLimit=(360.0/角度精度)*0.5
角度=0
polarLut=dict()
内部=dict()
虽然angle您总是在polarLut
中存储相同的词典,因此所有条目都指向相同的原始内部
在while循环中移动internal
的声明。您总是在polarLut
中存储相同的字典,因此所有条目都指向相同的原始internal
将internal
的声明移动到while循环中。您只有一个internal
字典,将其分配给polarLut
字典的所有值,然后继续为下一个角度重新修改它
你会想要像这样的东西
polarLut = dict()
while angle < noOfAnglesLimit:
theta = (pi / 180.0) * (
angle * ANGLE_ACCURACY
)
inner = dict()
radius = 0
while radius <= 200:
x = int(float(radius) * cos(theta) + 0.5)
y = int(float(radius) * sin(theta) + 0.5)
inner[radius] = (x, y)
radius += 1
polarLut[angle] = inner
angle += 1
polarLut=dict()
当角度 而radius只有一个内部
字典,将其分配给polarLut
字典的所有值,然后继续为下一个角度
再次修改它
你会想要像这样的东西
polarLut = dict()
while angle < noOfAnglesLimit:
theta = (pi / 180.0) * (
angle * ANGLE_ACCURACY
)
inner = dict()
radius = 0
while radius <= 200:
x = int(float(radius) * cos(theta) + 0.5)
y = int(float(radius) * sin(theta) + 0.5)
inner[radius] = (x, y)
radius += 1
polarLut[angle] = inner
angle += 1
polarLut=dict()
当角度