OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to get fastly as possible a specific sequence of numbers (all numbers twice, except for the first and last) with numpy?

  • Thread starter Thread starter Certes
  • Start date Start date
C

Certes

Guest
Knowing a final number (for example 5), I would like to create an array containing the following sequence:

[0,1,1,2,2,3,3,4,4,5]

Meaning that the list should contain all numbers repeated twice, except for the first and last.

Here is the code I use to achieve this :

Code:
import numpy as np

# final number
last = 35
# first sequence
sa = np.arange(0,last,1)
# second sequence (shifted by 1 unit)
sb = np.arange (1,last+1,1) 
# concatenation and flattening
sequence = np.stack((sa, sb), axis=1).ravel()
# view the result
print(sequence)

Do you think there would be a more direct and/or effective way to achieve the same result?
<p>Knowing a final number (for example 5), I would like to create an array containing the following sequence:</p>
<p>[0,1,1,2,2,3,3,4,4,5]</p>
<p>Meaning that the list should contain all numbers repeated twice, except for the first and last.</p>
<p>Here is the code I use to achieve this :</p>
<pre class="lang-py prettyprint-override"><code>
import numpy as np

# final number
last = 35
# first sequence
sa = np.arange(0,last,1)
# second sequence (shifted by 1 unit)
sb = np.arange (1,last+1,1)
# concatenation and flattening
sequence = np.stack((sa, sb), axis=1).ravel()
# view the result
print(sequence)

</code></pre>
<p>Do you think there would be a more direct and/or effective way to achieve the same result?</p>
 

Latest posts

S
Replies
0
Views
1
Safwan Aipuram
S
Top