背景關系:我想制作多詞搜索網格
我有一個像這樣的文本行輸入(包含數字和字母字串):
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
所有這些都在一個串列中作為輸入
但是,我需要在內部傳遞變數。所以我假設我需要將串列拆分/切片到另一個包含我需要的所有變數的串列中。
我想我需要拆分變數cat和dog這樣的:
#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']
這些是我需要的變數。但我不知道如何從上面的輸入中拆分它。
如果有任何不同的方式,請隨時解釋。謝謝
uj5u.com熱心網友回復:
我已將您的輸入保存在名為“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']
我的假設是你想用那個寬度變數做一些事情,因此我把每個單詞都切成了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?
uj5u.com熱心網友回復:
使用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())
uj5u.com熱心網友回復:
如果資訊在每一行中的位置總是“固定”的,那么最簡單的選擇是將這些行轉換為串列,然后專門參考每一行。就像是:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/381706.html
上一篇:安全restapi節點js
下一篇:多條件排序功能
