w3resource

NumPy: Combine last element with first element of two given ndarray with different shapes

NumPy: Array Object Exercise-166 with Solution

Write a NumPy program to combine last element with first element of two given ndarray with different shapes.

Sample Solution:

Python Code:

import numpy as np
array1 = ['PHP','JS','C++']
array2 = ['Python','C#', 'NumPy']    
print("Original arrays:")          
print(array1)
print(array2)          
result  = np.r_[array1[:-1], [array1[-1]+array2[0]], array2[1:]]   
print("\nAfter Combining:")       
print(result)

Sample Output:

Original arrays:
['PHP', 'JS', 'C++']
['Python', 'C#', 'NumPy']

After Combining:
['PHP' 'JS' 'C++Python' 'C#' 'NumPy']

Pictorial Presentation:

NumPy: Combine last element with first element of two given ndarray with different shapes.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to merge three given NumPy arrays of same shape.
Next: Write a NumPy program to convert a Python dictionary to a Numpy ndarray.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Set comprehension:

>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}