Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Lists in lists
#1
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?
Reply
#2
moves[0][3]

for move in moves:
    for item in move:
        print(item)
99 percent of computer problems exists between chair and keyboard.
Reply
#3
... 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!!
Reply
#4
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)
99 percent of computer problems exists between chair and keyboard.
Reply
#5
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])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare lists w_i_k_i_d 6 407 May-23-2024, 07:23 PM
Last Post: deanhystad
Question Using Lists as Dictionary Values bfallert 8 607 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  problem with print lists MarekGwozdz 4 806 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
  python convert multiple files to multiple lists MCL169 6 1,718 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Lists blake7 6 933 Oct-06-2023, 12:46 PM
Last Post: buran
  Trying to understand strings and lists of strings Konstantin23 2 896 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Why do the lists not match? Alexeyk2007 3 918 Jul-01-2023, 09:19 PM
Last Post: ICanIBB
  ''.join and start:stop:step notation for lists ringgeest11 2 2,527 Jun-24-2023, 06:09 AM
Last Post: ferdnyc
  Need help with sorting lists as a beginner Realist1c 1 821 Apr-25-2023, 04:32 AM
Last Post: deanhystad
  Pip lists the module but python does not find it Dato 2 1,387 Apr-13-2023, 06:40 AM
Last Post: Dato

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020