0

I have some numbers and want to make some pairs out of them. This is my data:

points=np.arange(0,10)

And this array defines how to make the pairs:

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

Then, I want to create such pairs:

[(0, 1), (1, 2), (2, 4), (3, 5), (4, 7), (5, 8), (6, 9)]

I have tried the following code but it is not giving me what I want:

L=[]
k=0
for r in repetition:
    last= k+r
    L.append(points[k:last])
    k= last
lmax= len(max(L,key=len))
L=[ np.concatenate((np.full(lmax-len(l),None),l))  for l in L ]
L= [[e for e in l if e] for l in zip(*L)]
result= sorted([p  for l in L for p in zip(l,l[1:])])

I have also uploaded a fig to clarify my request more. red lines are connecting the pairs. I reality, my data are much more complex but they follow the same role. The repetition defines that each value of points should be connected to which one. In advance, I do appreciate any help. Thanks for devoting time.

enter image description here

1
  • Dear @Ashutosh Kumar, the way I want to pair them is exactly like repetition. Firtst value in repetition is 1, so the first variable should be paired with next one. For the second, repetition is 1, so it should be connected with next one. For the third variable of points, in the repetition we reach 2: it means variables numbered 3 and 4 of points should be connected to their next two variable (2 with 4 and 3 with 5). For the fifth variable of points the number in repetition is 3 which means variables named 4,5 and 6 should be paired with 7, 8, 9, respectively. Commented Dec 11, 2020 at 10:43

1 Answer 1

1

code:

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

pairs = []
value = 0
for r in repetition:
    subpairs = np.arange(r)
    for s in subpairs:
        n = len(subpairs)
        a = value+s
        b = value+s+n
        pairs.append((a,b))
    value += n

result:

[(0, 1), (1, 2), (2, 4), (3, 5), (4, 7), (5, 8), (6, 9), (7, 10), (8, 11), (9, 12)]
Sign up to request clarification or add additional context in comments.

2 Comments

Dear @fullfine. Thanks for you big help. It is exactly what I needed. Just one comment. It is giving me the indices. HOw can I have the real data? In reality my data do not strat from 0. And also how can I get rid of the points created after the end of my limit? I mean I do not need pairs after (6,9). Thanks again for your help.
If you open another stack question I'll answer you there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.