[英]Iterate over pairs in order of sum of absolute values
我想按绝对值之和的顺序迭代整数对。 该列表应如下所示:
(0,0)
(-1,0)
(0,1)
(0,-1)
(1,0)
(-2,0)
(-1,1)
(-1,-1)
(0,2)
(0,-2)
(1,1)
(1,-1)
(2,0)
[...]
对于具有相同绝对值总和的对,我不介意它们的顺序。
理想情况下,我希望能够永远创建对,以便我可以依次使用每一对。 你怎么能这样做?
对于固定范围,我可以用丑陋的方式制作对列表:
sorted([(x,y)for x in range(-20,21)for y in range(-20,21)if abs(x)+abs(y)<21],key=lambda x:sum(map(abs,x))
这不允许我永远迭代,也不会一次给我一对。
这似乎可以解决问题:
from itertools import count # Creates infinite iterator
def abs_value_pairs():
for absval in count(): # Generate all possible sums of absolute values
for a in range(-absval, absval + 1): # Generate all possible first values
b = -absval + abs(a) # Compute matching second value (arbitrarily do negative first)
yield a, b
if b: # If first b is zero, don't output again, otherwise, output positive b
yield a, -b
这将永远运行,并且高效运行(避免不必要地重新计算任何内容)。
这将做到。 如果您真的希望它是无限的,请将for _
语句替换for _
while True
。
def makepairs(count=3):
yield (0,0)
base = 0
for _ in range(count):
base += 1
for i in range(base+1):
yield (i, base-i)
if base != i:
yield (i, i-base)
if i:
yield (-i, base-i)
if base != i:
yield (-i, i-base)
print(list(makepairs(9)))
下面的解决方案生成一个包含任意长度元组的 sum 流:
from itertools import count
def pairs(l = 2):
def groups(d, s, c = []):
if not d and sum(map(abs, c)) == s:
yield tuple(c)
elif d:
for i in [j for k in d[0] for j in {k, -1*k}]:
yield from groups(d[1:], s, c +[i])
for i in count():
yield from groups([range(i+1) for _ in range(l)], i)
p = pairs()
for _ in range(10):
print(next(p))
您可以创建一个无限生成器函数:
def pairSums(s = 0): # base generation on target sum to get pairs in order
while True: # s parameter allows starting from a given sum
for i in range(s//2+1): # partitions
yield from {(i,s-i),(s-i,i),(i-s,-i),(-i,i-s)} # no duplicates
s += 1 # next target sum
输出:
for p in pairSums(): print(p)
(0, 0)
(0, 1)
(0, -1)
(1, 0)
(-1, 0)
(2, 0)
(-2, 0)
(0, -2)
(0, 2)
(1, 1)
(-1, -1)
(3, 0)
(0, 3)
(0, -3)
(-3, 0)
(1, 2)
(-1, -2)
(2, 1)
...
首先请注意,您可以在非负值的网格中列出总计:
x
3|3
2|23
1|123
0|0123
-+----
|0123y
在这里,我们可以看到一个模式,其中对角线是您的总数。 让我们通过它们追踪一些系统的路线。 以下显示了您可以浏览它们的顺序:
x
3|6
2|37
1|148
0|0259
-+----
|0123y
这里矩阵包含迭代的顺序。
这解决了 x 和 y 非负值的问题。 为了得到剩下的,你可以否定 x 和 y,确保当它们为零时不要这样做。 像这样的东西:
def generate_triplets(n):
yield 0, (0, 0)
for t in range(1, n + 1): # Iterate over totals t
for x in range(0, t + 1): # Iterate over component x
y = t - x # Calclulate component y
yield t, (x, y) # Default case is non-negative
if y > 0:
yield t, (x, -y)
if x > 0:
yield t, (-x, y)
if x > 0 and y > 0:
yield t, (-x, -y)
def generate_pairs(n):
yield from (pair for t, pair in generate_triplets(n))
# for pair in generate_pairs(10):
# print(pair)
for t, (x, y) in generate_triplets(3):
print(f'{t} = abs({x}) + abs({y})')
这输出
0 = abs(0) + abs(0)
1 = abs(0) + abs(1)
1 = abs(0) + abs(-1)
1 = abs(1) + abs(0)
1 = abs(-1) + abs(0)
2 = abs(0) + abs(2)
2 = abs(0) + abs(-2)
2 = abs(1) + abs(1)
2 = abs(1) + abs(-1)
2 = abs(-1) + abs(1)
2 = abs(-1) + abs(-1)
2 = abs(2) + abs(0)
2 = abs(-2) + abs(0)
3 = abs(0) + abs(3)
3 = abs(0) + abs(-3)
3 = abs(1) + abs(2)
3 = abs(1) + abs(-2)
3 = abs(-1) + abs(2)
3 = abs(-1) + abs(-2)
3 = abs(2) + abs(1)
3 = abs(2) + abs(-1)
3 = abs(-2) + abs(1)
3 = abs(-2) + abs(-1)
3 = abs(3) + abs(0)
3 = abs(-3) + abs(0)
或成对:
(0, 0)
(0, 1)
(0, -1)
(1, 0)
(-1, 0)
(0, 2)
(0, -2)
(1, 1)
(1, -1)
(-1, 1)
(-1, -1)
(2, 0)
(-2, 0)
...
对于每个和,在一个象限中走对角线并将每个坐标旋转到其他象限:
from itertools import count
def coordinates():
yield 0, 0
for sum in count(1):
for x in range(sum):
y = sum - x
yield x, y
yield y, -x
yield -x, -y
yield -y, x
我使用了 itertools 产品:
>>> for i in sorted(itertools.product(range(-5, 4), range(-5, 4)), key=lambda tup: abs(tup[0]) + abs(tup[1])): print(i)
...
(0, 0)
(-1, 0)
(0, -1)
(0, 1)
(1, 0)
(-2, 0)
(-1, -1)
(-1, 1)
(0, -2)
(0, 2)
(1, -1)
(1, 1)
(2, 0)
(-3, 0)
(-2, -1)
(-2, 1)
(-1, -2)
(-1, 2)
(0, -3)
(0, 3)
(1, -2)
(1, 2)
(2, -1)
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.