3

I have an array like :

a=np.array([2,7])

a=[2,7]

and I want to swap the same array like 7,2 is there anyway to do?

answer should be like 7,2

a=[7,2]
Abercrombie
  • 1,012
  • 2
  • 13
  • 22
Radhe
  • 45
  • 3

2 Answers2

3

Try np.flip(a,0) See this for more information.

Abercrombie
  • 1,012
  • 2
  • 13
  • 22
3
#a=np.array([2,7]) 
a=[2,7]

# Reversing a list using slice notation
print (a[::-1]) # [7, 2]

# The reversed() method
print (list(reversed(a))) # [7, 2]

swap two elements in a list:

# Swap function
def swapPositions(list, pos1, pos2):
    list[pos1], list[pos2] = list[pos2], list[pos1]
    return list


a=[2,7]
pos1, pos2 = 0, 1

print(swapPositions(a, pos1 - 1, pos2 - 1))
ncica
  • 7,015
  • 1
  • 15
  • 37