-1

What code can I write for nested loops to print the row, column and number for each non-empty location in bd.

bd = [ [ '1', '.', '.', '.', '2', '.', '.', '3', '7'],
       [ '.', '6', '.', '.', '.', '5', '1', '4', '.'],
       [ '.', '5', '.', '.', '.', '.', '.', '2', '9'],
       [ '.', '.', '.', '9', '.', '.', '4', '.', '.'],
       [ '.', '.', '4', '1', '.', '3', '7', '.', '.'],
       [ '.', '.', '1', '.', '.', '4', '.', '.', '.'],
       [ '4', '3', '.', '.', '.', '.', '.', '1', '.'],
       [ '.', '1', '7', '5', '.', '.', '.', '8', '.'],
       [ '2', '8', '.', '.', '4', '.', '.', '.', '6'] ]

Output should be

(0,0) has 1

..and so on for the rest of the bd table

cHao
  • 84,970
  • 20
  • 145
  • 172
  • 1
    Please don't go removing your question once you have an answer. Part of the value of this site is in the questions others have asked (and the answers to them). – cHao Jul 21 '13 at 22:46

1 Answers1

1

Give it a try:

for row, items in enumerate(bd):
    for col, value in enumerate(items):
        if value != ".":
            print "(%s, %s) has %s" % (row, col, value)

prints:

(0, 0) has 1
(0, 4) has 2
(0, 7) has 3
(0, 8) has 7
(1, 1) has 6
...

Hope that helps.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195