gpt4 book ai didi

python - 如何打印特定的 numpy 数组对

转载 作者:行者123 更新时间:2023-12-04 02:29:29 25 4
gpt4 key购买 nike

我想在一个 numpy 数组中制作特定的对,并用一个简单的打印函数显示我想要的内容。我有两个数组:

points=np.arange(1,15)

然后我有另一个数组:

repetition= np.array([4, 4, 2, 2, 1, 1])

现在,我想打印以下对(我只是写了注释来显示我想要的):

1 5 # first value of points and (1+4)th value of point
2 6 # second value of points and (2+4)th value of point
3 7 # third value of points and (3+4)th value of point
4 8 # fourth value of points and (3+4)th value of point
7 9 # seventh value of points and (6+2)th value of point
8 10 # eighth value of points and (8+2)th value of point
9 11 # ninth value of points and (9+2)th value of point
10 12 # tenth value of points and (10+2)th value of point
12 13 # twelfth value of points and (11+2)th value of point
13 14 # thirteenth value of points and (13+1)th value of point

我尝试了下面的代码,但它没有给我预期的结果:

for m, n in zip (points, repetition):
print (m, m+n)

在图中,我想象了我的问题,其中红线显示了我的对。我非常感谢提前提供的任何帮助。

enter image description here

最佳答案

您实际上可以在 Python 中完成这一切。由于您的图片似乎表明数组参差不齐,因此在 numpy 中执行此操作可能有点棘手。

from itertools import islice, zip_longest, count, tee

def pairwise(iterable): # as per itertools recipes
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return zip(a, b)

def idx_pairs(repetitions):
it = count()
z = [list(islice(it, n))[::-1] for n in repetitions]
idx = sorted([
(i, j) for seq in zip_longest(*z)
for i, j in pairwise([k for k in seq if k is not None])])
return idx

[(points[i], points[j]) for i, j in idx_pairs(repetition)]

输出:

[(1, 5),
(2, 6),
(3, 7),
(4, 8),
(7, 9),
(8, 10),
(9, 11),
(10, 12),
(12, 13),
(13, 14)]

为了更好地理解这些步骤,我建议检查:

  • z
  • idx
  • 列表(zip_longest(*z))

特别是后者(直接在点上而不是点索引上完成)显示了与 OP 的绘图非常相似的内容:

it = iter(points)
z = [list(islice(it, n))[::-1] for n in repetition]
list(zip_longest(*z))
# out:
[(4, 8, 10, 12, 13, 14),
(3, 7, 9, 11, None, None),
(2, 6, None, None, None, None),
(1, 5, None, None, None, None)]

顺便说一句,看看当 repetition 列表不是单调递减时会发生什么很有趣:

repetition = np.array([4, 2, 4, 2, 1, 1])

it = iter(points)
z = [list(islice(it, n))[::-1] for n in repetition]
list(zip_longest(*z))

# out:
[(4, 6, 10, 12, 13, 14),
(3, 5, 9, 11, None, None),
(2, None, 8, None, None, None),
(1, None, 7, None, None, None)]

我相信这样一个repetition的正确输出应该是:

[(1, 7),
(2, 8),
(3, 5),
(4, 6),
(5, 9),
(6, 10),
(9, 11),
(10, 12),
(12, 13),
(13, 14)]

为了好玩,点可以包含任何东西;这是一个字符串示例:

points = [
'foo', 'bar', 'hi', 'hello', 'world',
'fuzz', 'ball', 'apple', 'tom', 'nancy',
'fred', 'james', 'mary', 'bob', 'lisa',
]
repetition = np.array([4, 2, 4, 2, 1, 1])
[(points[i], points[j]) for i, j in idx_pairs(repetition)]

# out:
[('foo', 'ball'),
('bar', 'apple'),
('hi', 'world'),
('hello', 'fuzz'),
('world', 'tom'),
('fuzz', 'nancy'),
('tom', 'fred'),
('nancy', 'james'),
('james', 'mary'),
('mary', 'bob')]

关于python - 如何打印特定的 numpy 数组对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65239681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com