Inserting an array into every nth element of another
You can always use a custom Enumerator
:
a1 = ['a', 'b', 'c', 'd', 'e', 'f']
a2 = ['g', 'h', 'i']
enum = Enumerator.new do |y|
e1 = a1.each
e2 = a2.each
loop do
y << e1.next << e1.next << e2.next
end
end
enum.to_a #=> ["a", "b", "g", "c", "d", "h", "e", "f", "i"]
Or for the general case:
n.times { y << e1.next }
Insert array items into another array eventy nth elements
This should get you on the right track. Note that this modifies the original arrays:
let arr1 = [1,2,3,4,5,6,7,8,9];let arr2 = ['up', 'down', 'left', 'right'];let nth = 2;let result = arr1.reduce((carry, current_value, index, original)=>{ // insert current element of arr1 carry.push(current_value); // insert from arr2 if on multiple of nth index and there's any left if((index+1) % nth === 0 && arr2.length!=0){ carry.push(arr2.shift()) } return carry;}, []);console.log('result', result);
How to insert an element after every other element in python list
Inserting into the middle of a list a bunch of times is going to be very slow for a large dataset. It seems like you can just build a new list like you're doing:
first = [1, 2, 3, 4, 5]
second = []
for i in range(0, len(first) - 1):
avg = (first[i] + first[i+1]) / 2
second.append(first[i])
second.append(avg)
second.append(first[-1])
print(second)
Which prints:
[1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]
Javascript: insert new member of array after each nth member
Simply change your code to this. Add 3 to itemIndex to account for 1 insertion as well.
var dataRow = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];for (var itemIndex = 2; itemIndex < dataRow.length; itemIndex += 3) { dataRow.splice(itemIndex, 0, 'insert this');}
console.log(dataRow);
Insert element in Python list after every nth element
I've got two one liners.
Given:
>>> letters = ['a','b','c','d','e','f','g','h','i','j']
Use
enumerate
to get index, add'x'
every 3rd letter, eg:mod(n, 3) == 2
, then concatenate into string andlist()
it.>>> list(''.join(l + 'x' * (n % 3 == 2) for n, l in enumerate(letters)))
['a', 'b', 'c', 'x', 'd', 'e', 'f', 'x', 'g', 'h', 'i', 'x', 'j']But as @sancho.s points out this doesn't work if any of the elements have more than one letter.
Use nested comprehensions to flatten a list of lists(a), sliced in groups of 3 with
'x'
added if less than 3 from end of list.>>> [x for y in (letters[i:i+3] + ['x'] * (i < len(letters) - 2) for
i in xrange(0, len(letters), 3)) for x in y]
['a', 'b', 'c', 'x', 'd', 'e', 'f', 'x', 'g', 'h', 'i', 'x', 'j']
(a) [item for subgroup in groups for item in subgroup]
flattens a jagged list of lists.
Numpy array insert every second element from second array
np.insert would work well:
>>> A = np.array([1, 3, 5, 7])
>>> B = np.array([2, 4, 6, 8])
>>> np.insert(B, np.arange(len(A)), A)
array([1, 2, 3, 4, 5, 6, 7, 8])
However, if you don't rely on sorted values, try this:
>>> A = np.array([5, 3, 1])
>>> B = np.array([1, 2, 3])
>>> C = [ ]
>>> for element in zip(A, B):
C.extend(element)
>>> C
[5, 1, 3, 2, 1, 3]
Javascript insert element at nth index in array, insertion elements from another array
You can iterate through the smaller array using forEach
use splice()
to insert elements.
let arr1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']let arr2 = ['a', 'b', 'c']
function insertArr(arr1,arr2,n){ arr1 = arr1.slice(); arr2.forEach((a,i) => { arr1.splice(i*n+i,0,a); }) return arr1;}
console.log(insertArr(arr1,arr2,4))
How to insert a string into a numpy list every nth index?
The second argument for np.insert
should be the index to place the values, you can try:
n = 3
np.insert(b, range(n, len(b), n), "\n")
# array(['a', 'b', 'c', '\n', 'a', 'b', 'c', '\n', 'a', 'b', 'c', '\n', 'a',
# 'b', 'c'],
# dtype='<U1')
Related Topics
Mini_Magick Gem Doesn't Work with My Imagemagick Install
Initialize the Delayed Jobs Gem by Starting the Workers on Application Start
How to Dry Up Method with Multiple { 'Not Found' }
What Is an Eoferror in Ruby File I/O
Omission of Curly Braces for a Hash in an Array
How to Efficiently Extract Repeated Elements in a Ruby Array
Using the Right Exception Subclass in Ruby
Are There Any Additional Inject Shorthand
Convert String to Datetime Ruby on Rails
How to Get Multiple-Line User Input in Ruby
Share Session Between Two Rails4 Applications
How to Dynamically Create Instance Methods at Runtime
Ruby: Wait for All Threads Completed Using Join and Threadswait.All_Waits - What the Difference
Bundle Install Is Using a Different Ruby Version