1 Replies - 501 Views - Last Post: 30 January 2012 - 03:13 PM Rate Topic: -----

Topic Sponsor:

#1 jone kim  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 07-January 10

insert elements in the list as user input

Posted 29 January 2012 - 08:51 AM

I asks the user to input the values of ROW to fill a 2D list. The value of column will be iterativley increase by 1.

list2D = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]

user input = [1,3,0,2] ##indexes of rows as well as values

i.e for 0th column the row = 1

1 column row = 3

2 column row = 0

3 column row = 2
so the new list will be:

newList = [[0,0,0,0],[1,0,0,0],[0,0,0,2],[0,3,0,0]]

How to do this?

Is This A Good Question/Topic? 0
  • +

Replies To: insert elements in the list as user input

#2 sysop_fb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-January 12

Re: insert elements in the list as user input

Posted 30 January 2012 - 03:13 PM

You can use a for loop with an extra counter.

Going by your example here's how it plays out by hand


>>> list2d
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
>>> list2d[1][0] = 1
>>> list2d[3][1] = 3
>>> list2d[0][2] = 0
>>> list2d[2][3] = 2
>>> list2d
[[0, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 2], [0, 3, 0, 0]]




See the patterns?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1