1

Inputs

I have list as follow

r1 = [([[[1, 2, 3], [1, 2, 3]], 
        [[4, 5, 6], [4, 5, 6]]],
       [[[7, 8], [7, 8]], 
        [[9, 10], [9, 10]]]),

      ([[[11, 12, 13], [11, 12, 13]], 
        [[14, 15, 16], [14, 15, 16]]],
       [[[17, 18], [17, 18]], 
        [[19, 20], [19, 20]]])]

I'm going to make 2 torch tensors from the input above.

my desired output is as follow

output

output = 
[tensor([[[ 1,  2,  3],
          [ 1,  2,  3]],
 
         [[ 4,  5,  6],
          [ 4,  5,  6]],
 
         [[11, 12, 13],
          [11, 12, 13]],
 
         [[14, 15, 16],
          [14, 15, 16]]]), 

 tensor([[[ 7,  8],
          [ 7,  8]],
 
         [[ 9, 10],
          [ 9, 10]],
 
         [[17, 18],
          [17, 18]],
 
         [[19, 20],
          [19, 20]]])]

My code is as follows.

output = []
for i in range(len(r1[0])):
    templates = []
    for j in range(len(r1)):
        templates.append(torch.tensor(r1[j][i]))
        template = torch.cat(templates)
    output.append(template)

Is there a simpler or easier way to get the result I want?

1 Answer 1

1

This will do:

output = [torch.Tensor([*a, *b]) for a, b in zip(*r1)]

It concatenates the corresponding items of the two list first then create the Tensor

Sign up to request clarification or add additional context in comments.

Comments

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.