如何将列表分割为不同长度的子列表



上下文:我想制作多词搜索网格

我有一个文本行的输入(包含一串数字和字母(,如下所示:

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

所有这些都作为输入出现在一个列表中

但是,我需要在内部传递变量。所以我假设我需要将列表拆分/切片为另一个列表,其中包含我需要的所有变量。

我想我需要像这样拆分catdog的变量:

#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']

这些都是我需要的变量。但我不知道如何从上面的输入中分离出来。

如果有任何不同的方法,请随时解释。谢谢

我已经将您的输入保存在一个名为"input.txt";并使用了以下方法:

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)

输出

[3, 5]
[4, 5]
[['catt', 'aata', 'tatc'], ['gogog', 'ooooo', 'godog', 'ooooo', 'gogog']]
['cat', 'dog']

我的假设是,您想对width变量执行某些操作,因此我将每个单词都剪切为cols[-1]字符。现在,您需要决定如果len(matrix) > n_animals该怎么办。

跟进

结合一些效率反馈:

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?

使用next迭代文件对象:

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']

注意:如果字符串列表而不是列表列表是可以的,那么您可以替换matrix =行:

# 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)]

如果您的输入已经存储为字符串列表,而不是要读取的文件,那么您仍然可以在迭代器上使用next

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())

如果信息在每一行中的位置总是"固定的",那么最简单的选择是将行转换为列表,然后具体引用每一行。类似于:

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)

最新更新