提问人:user18457417 提问时间:3/13/2023 最后编辑:Driftr95user18457417 更新时间:3/14/2023 访问量:74
如何使用 int 搜索作为浮点数的键,该 int 将四舍五入
How to search for a key that is a float using an int that the float would round to
问:
我有两个列表将 x 和 y 坐标表示为一个平面,平面上的每个点都有一个分配给它的随机数,我用它作为搜索坐标的键,问题是我需要有可能分配给一个点的随机数是重复的,所以我使用了浮点数,但现在我需要使用 int 搜索字典作为查看该数字是否存在以及它被分配到哪个点的键。所以从本质上讲,我想知道是否可以搜索键的舍入版本,例如,如果键是 10.3,我需要使用等于 10 的 int 找到它。
x_amount = int(input("Height of room: "))
y_amount = int(input("Width of room: "))
# Asks for height and width of plane
Room_Total = x_amount * y_amount
numbers = []
length = len(str(Room_Total)) + 1
x_values = {}
y_values = {}
create_number = y_amount
def num_find(num):
y_coord = y_values[num]
x_coord = x_values[num]
# Here is where i want to search for the coordinates using an int
for num in range(1, (y_amount + 1)):
for n in range(1, (x_amount + 1)):
number = 1
while number != 0:
number = random.uniform(1, Room_Total)
if number not in numbers:
numbers.append(round(number))
x_values[number] = num
y_values[number] = n
number = 0
# Randomly generates a number for every place in the grid
plane = input("Want to see the plane? y/n ")
if plane == "y":
for i in numbers:
num_gap = length - len(str(i))
create_number = create_number - 1
if create_number == -1:
print("")
create_number = y_amount - 1
print(i,end=" ")
for num in range(1, num_gap):
print(" ", end="")
else:
print(i,end=" ")
for num in range(1, num_gap):
print(" ", end="")
# Prints grid if the user wants it (So that if the grid is 100 by 100 for example it doesn't print it)
我搜索了有答案的类似问题,但没有找到适用于这种情况的任何内容,也不确定还有什么可以尝试的。
答:
[如果我理解正确,您需要有关该功能的帮助。一个整数可能有多个四舍五入的浮点键,因此您可以使用列表推导式返回四舍五入的键的坐标列表。num_find
num
num
def num_find(num):
knums = [k for k in x_values if round(k)==num] ## random `number`s that round to num
return [{'value':k, 'x':x_values[k], 'y':y_values[k]} for k in knums]
使用下面描述的 5x5 平面:
numbers = [22, 17, 17, 1, 7, 5, 23, 4, 21, 15, 7, 17, 12, 22, 12, 6, 24, 17, 7, 3, 11, 3, 9, 10, 16]
x_values = { 21.56616443204579: 1, 16.88672504030793: 1, 16.61564085529154: 1, 1.4623292817213773: 1, 7.186315822551395: 1, 5.387703554349793: 2, 23.004220335362547: 2, 4.30105672619399: 2, 20.7082214032309: 2, 15.161515180582256: 2, 7.038981849299793: 3, 17.32470245778697: 3, 11.939846516953184: 3,
21.985484460089975: 3, 12.186946846244577: 3, 5.953912098756866: 4, 24.077177195496617: 4, 16.932061089221378: 4, 6.965151311285224: 4, 2.8087793024821837: 4, 10.66278368789144: 5, 3.2461449455136293: 5, 8.615396657669486: 5, 9.667998604001081: 5, 15.636821583006896: 5 }
y_values = { 21.56616443204579: 1, 16.88672504030793: 2, 16.61564085529154: 3, 1.4623292817213773: 4, 7.186315822551395: 5, 5.387703554349793: 1, 23.004220335362547: 2, 4.30105672619399: 3, 20.7082214032309: 4, 15.161515180582256: 5, 7.038981849299793: 1, 17.32470245778697: 2,
11.939846516953184: 3, 21.985484460089975: 4, 12.186946846244577: 5, 5.953912098756866: 1, 24.077177195496617: 2, 16.932061089221378: 3, 6.965151311285224: 4, 2.8087793024821837: 5, 10.66278368789144: 1, 3.2461449455136293: 2, 8.615396657669486: 3, 9.667998604001081: 4, 15.636821583006896: 5 }
num_find(7)
会回来[ {'value': 7.186315822551395, 'x': 1, 'y': 5}, {'value': 7.038981849299793, 'x': 3, 'y': 1}, {'value': 6.965151311285224, 'x': 4, 'y': 4} ]
num_find(5)
会返回,并且[{'value': 5.387703554349793, 'x': 2, 'y': 1}]
num_find(29)
将返回(一个空列表,因为没有匹配项)[]
另一方面,
拥有两个单独的字典(和)具有完全相同的键集有点多余。最好有一个
元组
s 的字典。而不是x_values[number]=num
和y_values[number]=n
(如果 number 不是 in numbers...
),您可以只用一个字典填充。x_values
y_values
xy_values
xy_values[number] = (num, n)
由于是一个浮点数,并且是作为整数列表构建的,因此检查数字
而不是数字是
多余的:number
numbers
if number not in numbers: numbers.append(round(number))
我知道你不能使用,因为你想要重复的整数,但这只是检查重复的整数键。重复“随机”浮点数(不指定小数位限制)的机会很小,但如果你要检查重复的键(如果你不想冒着因覆盖它们而丢失任何坐标的风险),你应该检查所有键,而不仅仅是整数。
if round(number) not in numbers:
我建议用类似的东西填满你的“飞机”:
xy_values = {} ## instead of `x_values={}` and `y_values={}`
for num in range(1, (y_amount + 1)):
for n in range(1, (x_amount + 1)):
number = random.uniform(1, Room_Total)
while number in xy_values: ## [ `in xy_values` <===> `in xy_values.keys()` ]
number = random.uniform(1, Room_Total)
numbers.append(round(number))
xy_values[number] = (num, n) ## [ only one dictionary to fill ]
虽然不受此更改的影响,但对于我在上面描述的示例中的飞机,如下所示:numbers
xy_values
# xy_values = \ { 21.56616443204579: (1, 1), 16.88672504030793: (1, 2), 16.61564085529154: (1, 3), 1.4623292817213773: (1, 4), 7.186315822551395: (1, 5), 5.387703554349793: (2, 1), 23.004220335362547: (2, 2), 4.30105672619399: (2, 3), 20.7082214032309: (2, 4), 15.161515180582256: (2, 5), 7.038981849299793: (3, 1), 17.32470245778697: (3, 2), 11.939846516953184: (3, 3), 21.985484460089975: (3, 4), 12.186946846244577: (3, 5), 5.953912098756866: (4, 1), 24.077177195496617: (4, 2), 16.932061089221378: (4, 3), 6.965151311285224: (4, 4), 2.8087793024821837: (4, 5), 10.66278368789144: (5, 1), 3.2461449455136293: (5, 2), 8.615396657669486: (5, 3), 9.667998604001081: (5, 4), 15.636821583006896: (5, 5) }
可以简单地定义为num_find
def num_find(num):
return [{'value':k, 'x':x, 'y':y} for k,(x,y) in xy_values.items() if round(k)==num]
[它将产生与以前相同的结果,但效率会更高一些,因为只有一个循环(用于遍历一个字典)而不是两个循环,您必须遍历字典以收集匹配的键,然后遍历匹配的键并从两个单独的字典中获取值...]
此外,我建议研究 f 字符串和内置的枚举
函数,也许还可以研究条件表达式,因为您的 if plane == “y”:...
块可以简化为:
# plane = input("Want to see the plane? y/n ")
if plane == "y":
for n, i in enumerate(numbers, 1):
print(f'{i:<{length}}', end=' ' if n%y_amount else '\n')
[您可以使用 if n%y_amount 而不是 if n%y_amount
!=0
,因为 0
的计算结果为 False
。
评论