我有这个代码段,它在GUI中“生成”图形的中心……探测速度太慢,因为解决方案迭代太多。所以我想知道如何实现一个生成器以获取我需要的exacle值。
from pprint import pprint
def center(i, j):
s = 50
for k, v1 in enumerate(range(int(s / 2), s * 8, s)):
for n, v2 in enumerate(range(int(s / 2), s * 8, s)):
if (i, j) == (k, n):
return (v1, v2)
pprint(center(0,0)) # -> (25, 25)
pprint(center(6,3)) # -> (325, 175)
所以我制造了这个发电机:
def centerGenerator():
s = 50
for k in range(int(s / 2), s * 8, s):
for n in range(int(s / 2), s * 8, s):
yield k, n
def center(i, j):
for c in centerGenerator():
pass # how to take only c(i,j) from the Generator?
pprint(center(0,0)) # -> (25, 25)
pprint(center(6,3)) # -> (325, 175)
但是idk如何实现它。有什么想法吗?