arr=[1,2,3,4,5,6,7] # the array
n=2
j=0
while j<2:
temp1= arr[j]
j =1
for i in range (n,len(arr)):
arr[i-n]=arr[i]
arr[len(arr)-1] = temp1
print (arr) #print
Blockquote
this is the output [3, 4, 5, 6, 7, 6, 2] but i need [3, 4, 5, 6, 7, 1, 2] can resolve my problem to re-edit my code
CodePudding user response:
From your problem I understand that you want to split your array at position 2 and append it at the end.If my understanding is correct this code might help you, otherwise please elaborate your problem.
arr=[1,2,3,4,5,6,7] # the array
n=2
requires_arr=arr[n:] arr[0:n]
print(requires_arr)
CodePudding user response:
arr=[1,2,3,4,5,6,7] # the array
n=2
j=0
temp1 = []
while j<n:
temp1.append(arr[j])
j =1
for i in range (n,len(arr)):
arr[i-n]=arr[i]
arr[-n:] = temp1
print (arr) #print
CodePudding user response:
Based on the information you're providing, looks like you need to move the sublist(j, n) to the end of the original list. Being this the case, you can accomplish it by means of slicing:
arr = [1,2,3,4,5,6,7]
n = 2
j = 0
arr = arr[0:j] arr[n:] arr[j:n]
print(arr)
Result would be:
[3, 4, 5, 6, 7, 1, 2]
Another example:
arr = [1,2,3,4,5,6,7]
n = 3
j = 1
arr = arr[0:j] arr[n:] arr[j:n]
print(arr)
Results in:
[1, 4, 5, 6, 7, 2, 3]
