如何打印特定的 numpy 数组对
How to print specific pairs of a numpy array
我想在 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)
在图中,我想象了我的问题,其中红线显示了我的对。我非常感谢提前提供的任何帮助。
您实际上可以在 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
list(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')]
经过@Pierre D.的讲解,我才真正明白了问题所在。谢谢。我更正了我以前的代码并在里面放了一些打印函数以简化解释。
points=np.arange(1,15)
repetition= np.array([4, 4, 2, 2, 1, 1])
L=[]
k=0
for r in repetition:
last= k+r
L.append(points[k:last])
k= last
print(L,"\n")
lmax= len(max(L,key=len))
L=[ np.concatenate((np.full(lmax-len(l),None),l)) for l in L ]
print(*L,"\n",sep="\n")
print(*zip(*L),"\n",sep="\n")
L= [ [ e for e in l if e] for l in zip(*L) ]
print(*L,"\n",sep="\n")
P= sorted([ p for l in L for p in zip(l,l[1:]) ])
print(*P,"\n",sep="\n")
我想在 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)
在图中,我想象了我的问题,其中红线显示了我的对。我非常感谢提前提供的任何帮助。
您实际上可以在 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
list(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')]
经过@Pierre D.的讲解,我才真正明白了问题所在。谢谢。我更正了我以前的代码并在里面放了一些打印函数以简化解释。
points=np.arange(1,15)
repetition= np.array([4, 4, 2, 2, 1, 1])
L=[]
k=0
for r in repetition:
last= k+r
L.append(points[k:last])
k= last
print(L,"\n")
lmax= len(max(L,key=len))
L=[ np.concatenate((np.full(lmax-len(l),None),l)) for l in L ]
print(*L,"\n",sep="\n")
print(*zip(*L),"\n",sep="\n")
L= [ [ e for e in l if e] for l in zip(*L) ]
print(*L,"\n",sep="\n")
P= sorted([ p for l in L for p in zip(l,l[1:]) ])
print(*P,"\n",sep="\n")