0

I working through a tutorial working on lists and using tic-tac-toe.

I am given a string like: XOXOXOXXO

And I'd like to convert this into a matrix or a list of list, for example:

tic_tac_toe= [[ 'X', 'O', 'X'], [ 'O', 'X', 'O'], [ 'O', 'X', 'X']]

I can get this as a 'list', but I can't get this as a list of lists.

I've been searching various posts, and one challenge is I see a lot that are 5 or 7 years old, and I have to wonder how relevant some of the answers still are, and I haven't been able to make work those prior recommendations.

player_actions = 'XO_OOX_X_'

cells = list()

print('Before loop:', player_actions)

for char in player_actions:
    cells.insert(0, ''.join(char.split()))

# Output
print('After loop:', cells)

Output example:
Before loop: XOXOXOXXO
After loop: ['O', 'X', 'X', 'O', 'X', 'O', 'O', 'X', 'X']

Goal: [['O', 'X', 'X'],['O', 'X', 'O'],['O', 'X', 'X']]
2
  • 1
    Your desired output [['O', 'X', 'X'],['O', 'X', 'O'],['O', 'X', 'X']] doesn't seem to match your input string XOXOXOXXO Commented Jun 17, 2020 at 1:34
  • piecing together the above from several runs, and I grabbed the wrong parts for illustration. I apologize for the confusion. Commented Jun 17, 2020 at 3:25

3 Answers 3

1

Using slicing, create a batch of equally distributed elements:

s = 'XOXOXOXXO'
splitted = [s[x:x+3] for x in range(0,len(s),3)]
print([[i] for i in splitted])

one-liner:

print([[i] for i in [s[x:x+3] for x in range(0,len(s),3)]]) # [['XOX'], ['OXO'], ['XXO']] 
print([[i for i in s[x:x+3]] for x in range(0,len(s),3)])   # [['X', 'O', 'X'], ['O', 'X', 'O'], ['X', 'X', 'O']]
Sign up to request clarification or add additional context in comments.

1 Comment

I kept trying this with standard for loops but would run into index out of range errors. Thanks for sharing the list comprehension version.
1

Use numpy array and reshape it after that

code

import numpy as np

arr = ['O','X','O','X','X','O','X','X','O'] #let's take this list as an example

arr = np.array(arr).reshape(3,3) #this creates a 3x3 matrix of the given list

arr = arr.tolist() #if you want to convert numpy array back to list

print(arr)

#output
# [['O', 'X', 'O'], ['X', 'X', 'O'], ['X', 'X', 'O']]

2 Comments

I saw the numpy modules mentioned with matrix. I was trying to do this without importing a module, but this works pretty well. Thanks for sharing it.
@Wolftales Happy to help, just my two cents I thinks it's better to use numpy when dealing with multi dimensional arrays.
1

Assuming your actual desired output for

player_actions = 'XOXOXOXXO'

is

[['X', 'O', 'X'], ['O', 'X', 'O'], ['X', 'X', 'O']]

you can generate that using a nested list comprehension:

cells = [[c for c in player_actions[i:i+3]] for i in range(0, len(player_actions), 3)]

1 Comment

This was my intent. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.