0

I am trying to change something in every second element of my array. In that element then I only want to modify the 5th element (it's an array of arrays). I want to do so by just having a step of 2 in my while-loop. Therefore I added i += 2.

Now it's weird: when I put only in: PaylikeTableWithFee[i] = 'hello' Then it works and only every second array is modified and set to 'hello'.

However when I do so: PaylikeTableWithFee[i][5] = 'hello' Then every array is modified although the loop has a step of 2.

i = 1
while i < len(PaylikeTableWithFee):

    PaylikeTableWithFee[i][5] = 'hello'

    i += 2

Normally only the 5th element of every 2th array inside the main-array should be edited.

4
  • 1
    What is PaylikeTableWithFee? An array? An in that case an array of what? Commented May 2, 2019 at 13:38
  • You'll likely want to slice like arr[1::2] Commented May 2, 2019 at 13:44
  • Welcome to SO. Please finish the tour and you will understand How ask a question and modify question accordingly to a Complete, and Verifiable working example. Without posting your code you risk removal of your question. With your stated trail and error code... people are more willing to help you so we both can learn. Enjoy SO ;-) Commented May 2, 2019 at 13:53
  • @scenatic you need to define more on how you declare PaylikeTableWithFee because that is probably where the problem is. If I define it like PaylikeTableWithFee = [[0] * 6,[0] * 6,[0] * 6,[0] * 6,[0] * 6] only the fith element in every second element is changed, just as you want. Commented May 2, 2019 at 13:55

1 Answer 1

2

The issue is likely in how you created the array of arrays. I suspect you crated it with something like PaylikeTableWithFee = [[0,1,2,3,4,5]] * n. This only creates one inner array PaylikeTableWithFee will be then an array of the same reference n times. Therefore using PaylikeTableWithFee[0][5] = 'hello' would change (what seems like) every inner array.

Make sure that the PaylikeTableWithFee different inner arrays, for example like PaylikeTableWithFee = [[0,1,2,3,4,5] for _ in n].

Sign up to request clarification or add additional context in comments.

2 Comments

Well I created it the following way: for i in range(len(PaylikeTable)): if (i == 0): PaylikeTableWithFee.append(PaylikeTable[i]) else: PaylikeTableWithFee.append(PaylikeTable[i]) PaylikeTableWithFee.append(PaylikeTable[i]) So I guess that your answer is the solution, but I don't know yet how to implement it codewise. Payliketable is another array consisting of arrays and I wanted to duplicate every array of it.
If paytable is storing just simple integers/floats/etc, than instead of doing PaylikeTableWithFee.append(PaylikeTable[i]) do PaylikeTableWithFee.append(PaylikeTable[i].copy()). Otherwise try looking into the copy module, specifically copy.deepcopy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.