-3

I would like to duplicate an array inside the array to account for a duplicated separate array. The data I have is for thousands of numbers but for simplification, this is what I'm looking for:

a = [1,2,3,4,5]
b = [2,6,5,7,3]

new_a = [1,2,3,4,5,1,2,3,4,5]
new_b = [2,6,5,7,3,2,6,5,7,3]

#array b
magnitude = data[:,1]

# phaseresult_i is array a

for t in range(len(time)):
    floor = math.floor((time[t]-time[0])/(bestperiod))
    phase_i = ((time[t]-time[0])/(bestperiod))-floor
    phaseresult_i.append(phase_i)

newphaseresult_i = phaseresult_i +phaseresult_i
newmagnitude = magnitude + magnitude

Above is what I have exactly in my code, and the length of the first array doubles but the length of the second array does not.

4
  • 2
    to account for a duplicated separate array What does that mean? Commented Oct 24, 2018 at 15:39
  • Possible duplicate of Problem concatenating Python list Commented Oct 24, 2018 at 15:41
  • @jpp Duplicate both arrays so they match the original array values according to the index Commented Oct 24, 2018 at 15:50
  • Your edit make the post not be in accordance with Minimal, Complete, and Verifiable example standards Commented Oct 24, 2018 at 15:59

5 Answers 5

1

Just use the + operator:

a = [1,2,3,4,5]
b = [2,6,5,7,3]

new_a = a + a
new_b = b + b
Sign up to request clarification or add additional context in comments.

5 Comments

For my "a" the array duplicated, but for my "b" the array did not.
What has in b after doing this?
so I have len(a) = 1325, len(a_new)=2650, len(b)=1325, len(b_new)=1325. I've edited my original post to show what my a and b arrays are
Please, double check your code. It's a prety straight forward operation. a and b are list of integers as in your example, right?
I added a+a and b+b. Please see above.
1

You can use the + operator:

a = [1,2,3,4,5]
b = [2,6,5,7,3]
new_a = a+a 
new_b = b+b 

print(new_a) #[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
print(new_b) #[2, 6, 5, 7, 3, 2, 6, 5, 7, 3]

Comments

1

You could do it lazily with an itertools recipe assuming that you wanted to repeat the concatenation more than once (but it still works if you set n=2 for a single repeat).

from itertools import chain, repeat

def ncycles(iterable, n):
    "Returns the sequence elements n times"
    return chain.from_iterable(repeat(tuple(iterable), n))

a = [1,2,3,4,5]
b = ncycles(a, 3)

Then either call list() on b or iterate through it. For large arrays this will be more efficient, and it's a bit more convenient than using + concatenation.

Comments

0

You can simply add the old array to itself.

new_a = a + a
new_b = b + b

Comments

0

The + operator will work if you are looking to create a new variable.

a = [1,2,3,4,5]
b = [2,6,5,7,3]

new_a = a + a
new_b = b + b

Alternatively if you wish to just change the list in place you could use .extend

a = [1,2,3,4,5]
b = [2,6,5,7,3]

a.extend(a)
b.extend(b)

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.