Python Forum

Full Version: Lists in lists
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have the following code, for making an AI in tic tac toe (or knots and crosses).

Pre: I've written a program to make every possible configuration to a text file. I've loaded the text file and loaded into lm as a list.

    # Set the value of "o" equal from 0 to 4.
    for o in range(0, 5):

        # Set the value of "x" equal from 1 to 5.
        for x in range(1, 6):

            # For each board configuration in the list lm.
            for board in lm:

                # If the number of o's and x's found in the board configuration is equal to the value o and x.
                if board.count("o") == o and board.count("x") == x:

                    # Set i equal to the amount of moves made: the value of x plus the value of o.
                    i = x + o

                    # Replace the empty spaces with the number of moves left.
                    board = board.replace(" ", str(9 - i))

                    # Insert the board configuration to the move list #i.
                    eval("moves" + str(i)).insert(0, board)
I found out that using eval is not very safe to use. So I thought, no problem, let's make a multidimensional array! It should start out as a nine by two list. Name the first column after the moves and if it doesn't fit, make the array larger by adding columns.

So here's the thing... python doesn't do multidimensional arrays or lists... It only does do lists in lists...

So I thinking of making a list like this:
moves=[[moves1],[moves2],[moves3],[moves4],[moves5],[moves6],[moves7],[moves8],[moves9]]
That's fine by me, one reason is that the lists of moves1 and moves9 are 3 strings long, moves5 on the other hand is way larger, anyway. How do I access those lists easily like I did with the eval function?
1. How do I itterate through a 'lists in list'-list?
2. How do I index a list in a list? So I need in move 4, the second listed string, how do I acces that?
moves[0][3]

for move in moves:
    for item in move:
        print(item)
... What the hell... I feel so dumb right now... The answer is so simple, yet I couldn't get my head around it... Time for coffee!

Thank you!!
Quote:So here's the thing... python doesn't do multidimensional arrays or lists... It only does do lists in lists...

pylist = []
for i in range(9):
    pylist.append([])
    for j in range(9):
        pylist[i].append( (j, j * 2) )

print(pylist)
empty list
pylist = []
for i in range(9):
    pylist.append([])
print(pylist)
1. How do I itterate through a 'lists in list'-list?
2. How do I index a list in a list? So I need in move 4, the second listed string, how do I acces that?
There are two ways to do this. Either use the iterator object or use indexing method as your would do with any other language.

ll = [[1,2], [2,3], [3,4]]

for fl in ll:
	for sl in fl:
		print(sl)

for i in range(len(ll)):
	for j in range(len(ll[i])):
		print(ll[i][j])