This is my first post here, so please excuse me if I do not make myself clear. I searched for this problem beforehand, but didn't know exactly what terms to phrase it in. This code was originally part of a larger program, but I tried cutting out all the other variables and my problem still remains:
What I want this to do is create a function that categorizes a list of numbers in to 7-number sublists. There must be one sublist for each item in the 'player' list, which is why I simply copied the player list and ran a for loop to replace each element (previously the names of players) with their corresponding seven numbers. The number of sets there should be is drawn from the 'player' list, and the numbers themselves are drawn from the 'playermodifier' list, both of which are defined elsewhere in the program.
players=['Paul', 'Darren']
playermodifiers=[4,5,7,2,8,4,7,3,9,4,6,2,6,4]
def modifiersort():
n=1
global playermodifierssort
global playermodifiers
playermodifierssort=players
for x in playermodifierssort:
playermodifierssort[n-1]=playermodifiers[7*(n-1):7*n]
n=n+1
playermodifiers=playermodifierssort
This sorts the list as I want it to, however, it also somehow changes the original 'players' list to be identical to playermodifierssort and playermodifiers at the end of the function. When I run the function, I get:
playermodifiers = [[4, 5, 7, 2, 8, 4, 7], [3, 9, 4, 6, 2, 6, 4]]
players = [[4, 5, 7, 2, 8, 4, 7], [3, 9, 4, 6, 2, 6, 4]]
This is problematic because I want to preserve the players list. How can this be happening? The 'players' list isn't global, so how can it be permanently changed as a result of this function?