Q:

Python- Shifting the position of each object to left or right in a random Array

I have learned not long time ago how to shift objects in an array, usually numbers.
Now I attached it to my piece of code that should generate random objects in an array or list.
Question: How do I modify the second part so that I can get it to work with the first part?
This is the most confusing part of programming. I know it's a stupid question but i am still studying and i'm not very good but i really want to become better.
def randomArray(n_Elementi, my_vector):
    for i in range(0, n_Elementi):
        newElement=random.randint(1,500)
        my_Vector.append(newElement)

return 

def shift(seq, n):
    newElement = []
    for i in range(len(seq)):
    newElement.append(seq[(i-n) % len(seq)])
return newElement

randomArray(n_Elementi, my_vector)
shift (newElement)

A:

There were some indent problems, capitals mismatch but on the whole your code works:
import random
def randomArray(n_Elementi, my_Vector):
    for i in range(0, n_Elementi):
        newElement=random.randint(1,500)
        my_Vector.append(newElement)
    return 

def shift(seq, n):
    newElement = []
    for i in range(len(seq)):
        newElement.append(seq[(i-n) % len(seq)])
    return newElement

n_Elementi = 10
my_vector=[]
randomArray(n_Elementi, my_vector)
print my_vector
print shift (my_vector, 3)

however, this can greatly simplified. Here is one way using numpy (Numeric Python, it's like the MAtlab of python)
import random
import numpy as np

#rand creates array between 0,1. multiple by 500 and recast to int to get the range you want
def randomArray(n_Elementi):    
    return np.array(np.random.rand(n_Elementi)*500,dtype=int) 

#seq[-n:] takes the elements n from the end to the end [-n:]. This is called splicing. similarly [:-n] ...
def npshift(seq, n):
    return np.concatenate((seq[-n:], seq[:-n]))

n_Elementi = 10
my_vector=randomArray(n_Elementi)
print my_vector
print shift (my_vector, 3)

