Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python shift array

# Shift Array in Python Using the collections Module

# We can use the deque.rotate(n) method of the collections module to rotate an array in Python.
# The deque.rotate(n) method rotates the deque class object n positions,
# where the sign of n indicates whether to rotate the deque in the left or right direction.

# If the value of n is positive, then the input will be rotated from the left to right direction,
# and if the n is negative, the input will be rotated from the right to left direction.
# The code below demonstrates how to rotate an array using the deque.rotate(n) method in Python.


from collections import deque

myarray = deque([1, 2, 3, 4, 5, 6])
myarray.rotate(2) #rotate right
print(list(myarray))
myarray.rotate(-3) #rotate left
print(list(myarray))

# Output:

# [5, 6, 1, 2, 3, 4
# [2, 3, 4, 5, 6, 1]

# Shift Array in Python Using the numpy.roll() Method

# The numpy.roll(array, shift, axis) method takes the array as input and rotates it to the positions equal to the shift value.
# If the array is a two-dimensional array,
# we will need to specify on which axis we need to apply the rotation; otherwise,
# the numpy.roll() method will apply the rotation on both axes.

# Just like the deque.rotate() method,
# the numpy.roll() also rotates the array from right to left if the value is positive and right to left if the value is negative.
# The below example code demonstrates how to rotate an array in Python using the numpy.roll() method.


import numpy as np

myarray = np.array([1, 2, 3, 4, 5, 6])
newarray = np.roll(myarray, 2) #rotate right
print(newarray)
newarray =np.roll(myarray, -2) #rotate left
print(newarray)

# Output:

# [5 6 1 2 3 4]
# [3 4 5 6 1 2]


# Shift Array in Python Using the Array Slicing

# We can also implement the rotate function using the array slicing in Python.
# This method does not need any additional library but is less efficient than the methods explained above.

# The below example code demonstrates how to use the array slicing to rotate or shift an array in Python.

def rotate(input, n):
    return input[n:] + input[:n]

myarray = [1, 3, 5, 7, 9]
print(rotate(myarray, 2)) #rotate left
print(rotate(myarray, -2)) #rotate right


# Output:

# [5, 7, 9, 1, 3]
# [7, 9, 1, 3, 5]
Comment

PREVIOUS NEXT
Code Example
Python :: rename a column 
Python :: combine two dataframes of same length 
Python :: length of a list python 
Python :: how to sort a dictionary in python without sort function 
Python :: how to comment in python 
Python :: where are python libraries installed ubuntu 
Python :: python multiprocessing queue 
Python :: create a pandas dataframe 
Python :: how to replace a character in python 
Python :: dataframe 
Python :: sort 2d list python 
Python :: check how many letters in a string python 
Python :: pygame buttons 
Python :: python bool() 
Python :: list from dataframe python 
Python :: python ^ symbol 
Python :: list unpacking python 
Python :: * pattern program in python 
Python :: how to read frame width of video in cv2 
Python :: pandas read_csv drop column 
Python :: filter json python 
Python :: python code checker 
Python :: joining two lists in python using for loop 
Python :: while loop in python for do you want to continue 
Python :: numpy difference between two arrays 
Python :: pandas difference between dates in hours 
Python :: pynput keyboard backspace 
Python :: how to create an auto clicker in python 
Python :: round() function in python 
Python :: how to generate two random numbers in python 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =