Reputation: 47
Context: I want to make multiple word search grid
I have an Input of lines of text (containing string of numbers and letters) like this:
2 <--- tells how many grids/boxes
3 <--- tells rows/length
4 <--- tells columns/width
catt <--\
aata <--- letters that will fill the box
tatc <--/
cat <--- the word that will be search inside the box
5 <--- and repeat but start with the rows
5
gogog
ooooo
godog
ooooo
gogog
dog
all of that comes in one list as an input
However, I need to pass variables inside. so I assume that I need to split/slice the list into another lists containing all variables that I need.
I think I need to split the variables for cat
and dog
like this:
#variables for cat and dog
rows, cols = [3, 5], [4, 5] #the numbers have to be integer
matrix = [['catt', 'aata', 'tatc'], ['gogog', 'ooooo', 'godog', 'ooooo', 'gogog']]
word = ['cat', 'dog']
those are the variables I need. but I don't know how to split it like that from the input above.
If there are any different ways, feel free to explain. Thank you
Upvotes: 2
Views: 156
Reputation: 15515
Using next
to iterate through a file object:
with open('input.txt') as f:
n_animal = int(next(f).strip())
rows, cols, matrices, words = [], [], [], []
for _ in range(n_animals):
n_row = int(next(f).strip())
n_col = int(next(f).strip())
rows.append(n_row)
cols.append(n_col)
matrix = [list(next(f).strip()) for _ in range(n_row)]
matrices.append(matrix)
words.append(next(f).strip())
print('rows, cols = ', rows, cols)
print('matrices = ', matrices)
print('words = ', words)
# rows, cols = [3, 5] [4, 5]
# matrices = [[['c', 'a', 't', 't'], ['a', 'a', 't', 'a'], ['t', 'a', 't', 'c']], [['g', 'o', 'g', 'o', 'g'], ['o', 'o', 'o', 'o', 'o'], ['g', 'o', 'd', 'o', 'g'], ['o', 'o', 'o', 'o', 'o'], ['g', 'o', 'g', 'o', 'g']]]
# words = ['cat', 'dog']
Note: if a list of strings is okay instead of a list of lists, then you can replace the matrix =
line:
# list of lists
matrix = [list(next(f).strip()) for _ in range(n_row)]
# list of strings
matrix = [next(f).strip() for _ in range(n_row)]
If your input is already stored as a list of strings, rather than as a file to read from, you can still use next
on an iterator:
lines = ['2', '3', '4', 'catt', ...]
f = iter(lines)
n_animal = int(next(f).strip())
rows, cols, matrices, words = [], [], [], []
for _ in range(n_animals):
n_row = int(next(f).strip())
n_col = int(next(f).strip())
rows.append(n_row)
cols.append(n_col)
matrix = [list(next(f).strip()) for _ in range(n_row)]
matrices.append(matrix)
words.append(next(f).strip())
Upvotes: 0
Reputation: 3379
I have saved your input in a file called "input.txt" and used the following approach:
with open("input.txt", "r") as f:
lines = [x.strip() for x in f.readlines()]
n_animals, other_lines = int(lines[0]), lines[1:]
rows, cols, matrix, word = [[] for _ in range(4)] # comment by @Stef - well spotted
while len(other_lines) > 0:
rows.append(int(other_lines[0]))
cols.append(int(other_lines[1]))
matrix.append(list(map(lambda x: x[:cols[-1]], other_lines[2:2 + rows[-1]])))
word.append(other_lines[2 + rows[-1]])
other_lines = other_lines[2 + rows[-1] + 1:]
if len(matrix) == n_animals:
pass # Do we need to take any action here? like break?
print(rows)
print(cols)
print(matrix)
print(word)
OUTPUT
[3, 5]
[4, 5]
[['catt', 'aata', 'tatc'], ['gogog', 'ooooo', 'godog', 'ooooo', 'gogog']]
['cat', 'dog']
My assumption was that you want to do something with that width variable, hence I have cut every word to cols[-1]
characters. Now, you need to decide what to do if len(matrix) > n_animals
.
FOLLOW UP
Incorporating some feedback for efficiency:
i = 0
while i < len(other_lines):
rows.append(int(other_lines[i]))
cols.append(int(other_lines[i + 1]))
matrix.append(list(map(lambda x: x[:cols[-1]], other_lines[i + 2 : i + 2 + rows[-1]])))
word.append(other_lines[i + 2 + rows[-1]])
i += 2 + rows[-1] + 1
if len(matrix) == n_animals:
pass # Do we need to take any action here? like break?
Upvotes: 1
Reputation: 11060
If the position of the information in each line is always 'fixed', then the easiest option is to convert the lines to a list, and then reference each line specifically. Something like:
data = text.splitlines()
grids = data[0]
rows = data[1]
cols = data[2]
letters = data[3:7]
repeat = data[7:9]
remain = data[9:]
print(grids, rows, cols, letters, repeat, remain)
Upvotes: 0