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.

Cleared
  • 2,490
  • 18
  • 35
scenatic
  • 51
  • 4
  • 1
    What is `PaylikeTableWithFee`? An array? An in that case an array of what? – Cleared May 02 '19 at 13:38
  • You'll likely want to [slice](https://stackoverflow.com/questions/509211/understanding-slice-notation) like `arr[1::2]` – Andrew Allen May 02 '19 at 13:44
  • Welcome to SO. Please finish the tour and you will understand [How ask a question](https://stackoverflow.com/help/asking) and modify question accordingly to a [Complete, and Verifiable working example](https://stackoverflow.com/help/mcve). 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 ;-) – ZF007 May 02 '19 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. – Cleared May 02 '19 at 13:55

1 Answers1

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].

Faboor
  • 1,365
  • 2
  • 10
  • 23
  • 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. – scenatic May 02 '19 at 14:14
  • 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`. – Faboor May 05 '19 at 22:09