2 Replies - 580 Views - Last Post: 28 April 2013 - 09:14 PM Rate Topic: -----

#1 tina9899  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-April 13

Changing a 2D list

Posted 28 April 2013 - 03:10 PM

I am asking a user to enter a number. Then add that number to the first column of the list, replacing the zeros with the new number. How do i do this with a for loop?

COLS = 2
ROWS = 5
values = [[10,0], [13, 0], [36, 0], [74,0], [22,0]]

for r in range(ROWS):
for c in range(COLS):
values[c][r] =


That's all i have so far
Is This A Good Question/Topic? 0
  • +

Replies To: Changing a 2D list

#2 Hqtitan  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 51
  • Joined: 28-August 10

Re: Changing a 2D list

Posted 28 April 2013 - 03:46 PM

You would need to perform the calculations for each row in the list, so you could use the first loop you've got there:

for r in range(ROWS):


The first column is the 0th element in each row, and the 0 is the 1st element.

The variable r would be the current row.

To set the zero to the sum of the first column plus the entered number, you could set values[c][1] to the sum of values[c][0] and the entered number.

This post has been edited by Hqtitan: 28 April 2013 - 03:47 PM

Was This Post Helpful? 0
  • +
  • -

#3 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Changing a 2D list

Posted 28 April 2013 - 09:14 PM

You should be iterating over the list itself; not creating counters.

values = [[10,0], [13, 0], [36, 0], [74,0], [22,0]]
whatever = 5

for val in values:
    val[1] = val[0]+whatever

print(values)

-Mek

Edit: Oh yeah. Use code tags when you post code. Otherwise you lose the indentation. You can find the code tag button in the tool bar.

This post has been edited by Mekire: 28 April 2013 - 09:16 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1