python 中的单词搜索:return 索引字母的搜索
word search in python :a search which return the indices the letters
#以下程序检查二维数组中的单词,returns words.the 算法的嵌套索引列表检查水平方向(左和右)、垂直方向(向上和向下)和对角线(向下方向从左到右)
**我面临的问题**
#the program returns 纠正了一些单词的索引,但对于一些单词 returns none.for 一些单词程序崩溃给出了索引错误(列表索引超出范围)
代码
def find(word):
new_list=[]
print("lenght",len(word))
for i in range(3):
for j in range(3):
if board[i][j]==word[0] and board[i][j+1]==word[1] and board[i][j+2]==word[2]:#horizontal(right)
x=i
z=j
for k in range(len(word)):
#print('valuw of ',k)
if board[x][z]==word[k]:
new_list.append(x)
new_list.append(z)
print(new_list)
z+=1
if len(new_list) ==len(word)*2:
print(new_list)
return new_list #returns nested list
elif board[i][j]==word[0] and board[i+1][j]==word[1] and board[i+2][j]==word[2]: vertical(downward)
u=i
v=j
for k in range(len(word)):
#print('valuw of ',k)
if board[u][v]==word[k]:
new_list.append(u)
new_list.append(v)
print(new_list)
u+=1
if len(new_list) ==len(word)*2:
print(new_list)
return new_list
elif board[i][j]==word[0] and board[i+1][j+1]==word[1] and board[i+2][j+2]==word[2]: #diagonal
a=i
b=j
for k in range(len(word)):
#print('valuw of ',k)
if board[a][b]==word[k]:
new_list.append(a)
new_list.append(b)
print(new_list)
a+=1
b+=1
if len(new_list) ==len(word)*2:
print(new_list)
return new_list
elif board[i][j]==word[0] and board[i][j-1]==word[1] and board[i][j-2]==word[2]: #horizontal(left)
c=i
d=j
for k in range(len(word)):
#print('valuw of ',k)
if board[c][d]==word[k]:
new_list.append(c)
new_list.append(d)
print(new_list)
d-=1
if len(new_list) ==len(word)*2:
print(new_list)
return new_list
elif board[i][j]==word[0] and board[i-1][j]==word[1] and board[i-2][j]==word[2]: #verical(upward)
f=i
g=j
for k in range(len(word)):
#print('valuw of ',k)
if board[c][d]==word[k]:
new_list.append(f)
new_list.append(g)
print(new_list)
f-=1
if len(new_list) ==len(word)*2:
print(new_list)
return new_list
board=[['p','o','t'],
['x','y','z'],
['a','p','o']]
output=[]
l=['pot','pyo','ozt']
for word in l:
output.append(find(word))
print(output)#print the nested list containing the indices enter code here
您需要检查索引是否对数组有效。如果你有一个 3x3 数组并且 i=2,j=2,那么 j+1 将不在板上并且 Python 会给你一个 IndexError。你必须更加小心负指数,因为它们不会给出错误,因为 board[-1][-1] 与 board[2][2].
相同
此代码有很多地方可能出错。
- 列表索引超出范围:
您正在对给定板的行和列执行嵌套 for
循环,而没有检查您是否正在访问板中的有效索引。在您的代码中的某些区域,您可以像 j
一样执行 i+1
和 i+2
。这些当然可以超出您的董事会范围。
- 函数returns None:
您没有在嵌套的 for
循环中处理 else
案例。在 python 中,如果您的函数没有达到 return 语句,它会自动 returns None 类型。你的函数 returns None 在它没有达到任何 return new_list
语句的情况下。
检查这段代码,看看我在其中添加了一个新函数,该函数会检查您是否正在访问看板中的有效位置。
def is_valid_position(i,j):
return True if i<len(board) and j<len(board[0]) else False
def find(word):
new_list = []
print("length", len(word))
for i in range(3):
for j in range(3):
if is_valid_position(i,j) and is_valid_position(i,j+1) and is_valid_position(i,j+2):
if board[i][j]==word[0] and board[i][j+1]==word[1] and board[i][j+2]==word[2]:
x=i
z=j
for k in range(len(word)):
if board[x][z]==word[k]:
new_list.append(x)
new_list.append(z)
print(new_list)
z+=1
if len(new_list) ==len(word)*2:
print(new_list)
return new_list
if is_valid_position(i,j) and is_valid_position(i+1,j) and is_valid_position(i+2,j):
if board[i][j]==word[0] and board[i+1][j]==word[1] and board[i+2][j]==word[2]:
u=i
v=j
for k in range(len(word)):
if board[u][v]==word[k]:
new_list.append(u)
new_list.append(v)
print(new_list)
u+=1
if len(new_list) ==len(word)*2:
print(new_list)
return new_list
if is_valid_position(i,j) and is_valid_position(i+1,j+1) and is_valid_position(i+2,j+2):
if board[i][j]==word[0] and board[i+1][j+1]==word[1] and board[i+2][j+2]==word[2]:
a=i
b=j
for k in range(len(word)):
if board[a][b]==word[k]:
new_list.append(a)
new_list.append(b)
print(new_list)
a+=1
b+=1
if len(new_list) ==len(word)*2:
print(new_list)
return new_list
if is_valid_position(i,j) and is_valid_position(i,j-1) and is_valid_position(i,j-2):
if board[i][j]==word[0] and board[i][j-1]==word[1] and board[i][j-2]==word[2]:
c=i
d=j
for k in range(len(word)):
if board[c][d]==word[k]:
new_list.append(c)
new_list.append(d)
print(new_list)
d-=1
if len(new_list) ==len(word)*2:
print(new_list)
return new_list
if is_valid_position(i,j) and is_valid_position(i-1,j) and is_valid_position(i-2,j):
if board[i][j]==word[0] and board[i-1][j]==word[1] and board[i-2][j]==word[2]:
f=i
g=j
for k in range(len(word)):
if board[f][g]==word[k]:
new_list.append(f)
new_list.append(g)
print(new_list)
f-=1
if len(new_list) ==len(word)*2:
print(new_list)
return new_list
board=[['p','o','t'],
['x','y','z'],
['a','p','o']]
output=[]
l=['pot','pyo','ozt']
for word in l:
output.extend(find(word))
print(output)
以上代码输出如下
length 3
[0, 0]
[0, 0, 0, 1]
[0, 0, 0, 1, 0, 2]
[0, 0, 0, 1, 0, 2]
length 3
[0, 0]
[0, 0, 1, 1]
[0, 0, 1, 1, 2, 2]
[0, 0, 1, 1, 2, 2]
length 3
[2, 2]
[2, 2, 1, 2]
[2, 2, 1, 2, 0, 2]
[2, 2, 1, 2, 0, 2]
[0, 0, 0, 1, 0, 2, 0, 0, 1, 1, 2, 2, 2, 2, 1, 2, 0, 2]
#以下程序检查二维数组中的单词,returns words.the 算法的嵌套索引列表检查水平方向(左和右)、垂直方向(向上和向下)和对角线(向下方向从左到右) **我面临的问题** #the program returns 纠正了一些单词的索引,但对于一些单词 returns none.for 一些单词程序崩溃给出了索引错误(列表索引超出范围) 代码
def find(word):
new_list=[]
print("lenght",len(word))
for i in range(3):
for j in range(3):
if board[i][j]==word[0] and board[i][j+1]==word[1] and board[i][j+2]==word[2]:#horizontal(right)
x=i
z=j
for k in range(len(word)):
#print('valuw of ',k)
if board[x][z]==word[k]:
new_list.append(x)
new_list.append(z)
print(new_list)
z+=1
if len(new_list) ==len(word)*2:
print(new_list)
return new_list #returns nested list
elif board[i][j]==word[0] and board[i+1][j]==word[1] and board[i+2][j]==word[2]: vertical(downward)
u=i
v=j
for k in range(len(word)):
#print('valuw of ',k)
if board[u][v]==word[k]:
new_list.append(u)
new_list.append(v)
print(new_list)
u+=1
if len(new_list) ==len(word)*2:
print(new_list)
return new_list
elif board[i][j]==word[0] and board[i+1][j+1]==word[1] and board[i+2][j+2]==word[2]: #diagonal
a=i
b=j
for k in range(len(word)):
#print('valuw of ',k)
if board[a][b]==word[k]:
new_list.append(a)
new_list.append(b)
print(new_list)
a+=1
b+=1
if len(new_list) ==len(word)*2:
print(new_list)
return new_list
elif board[i][j]==word[0] and board[i][j-1]==word[1] and board[i][j-2]==word[2]: #horizontal(left)
c=i
d=j
for k in range(len(word)):
#print('valuw of ',k)
if board[c][d]==word[k]:
new_list.append(c)
new_list.append(d)
print(new_list)
d-=1
if len(new_list) ==len(word)*2:
print(new_list)
return new_list
elif board[i][j]==word[0] and board[i-1][j]==word[1] and board[i-2][j]==word[2]: #verical(upward)
f=i
g=j
for k in range(len(word)):
#print('valuw of ',k)
if board[c][d]==word[k]:
new_list.append(f)
new_list.append(g)
print(new_list)
f-=1
if len(new_list) ==len(word)*2:
print(new_list)
return new_list
board=[['p','o','t'],
['x','y','z'],
['a','p','o']]
output=[]
l=['pot','pyo','ozt']
for word in l:
output.append(find(word))
print(output)#print the nested list containing the indices enter code here
您需要检查索引是否对数组有效。如果你有一个 3x3 数组并且 i=2,j=2,那么 j+1 将不在板上并且 Python 会给你一个 IndexError。你必须更加小心负指数,因为它们不会给出错误,因为 board[-1][-1] 与 board[2][2].
相同此代码有很多地方可能出错。
- 列表索引超出范围:
您正在对给定板的行和列执行嵌套for
循环,而没有检查您是否正在访问板中的有效索引。在您的代码中的某些区域,您可以像j
一样执行i+1
和i+2
。这些当然可以超出您的董事会范围。 - 函数returns None:
您没有在嵌套的for
循环中处理else
案例。在 python 中,如果您的函数没有达到 return 语句,它会自动 returns None 类型。你的函数 returns None 在它没有达到任何return new_list
语句的情况下。
检查这段代码,看看我在其中添加了一个新函数,该函数会检查您是否正在访问看板中的有效位置。
def is_valid_position(i,j):
return True if i<len(board) and j<len(board[0]) else False
def find(word):
new_list = []
print("length", len(word))
for i in range(3):
for j in range(3):
if is_valid_position(i,j) and is_valid_position(i,j+1) and is_valid_position(i,j+2):
if board[i][j]==word[0] and board[i][j+1]==word[1] and board[i][j+2]==word[2]:
x=i
z=j
for k in range(len(word)):
if board[x][z]==word[k]:
new_list.append(x)
new_list.append(z)
print(new_list)
z+=1
if len(new_list) ==len(word)*2:
print(new_list)
return new_list
if is_valid_position(i,j) and is_valid_position(i+1,j) and is_valid_position(i+2,j):
if board[i][j]==word[0] and board[i+1][j]==word[1] and board[i+2][j]==word[2]:
u=i
v=j
for k in range(len(word)):
if board[u][v]==word[k]:
new_list.append(u)
new_list.append(v)
print(new_list)
u+=1
if len(new_list) ==len(word)*2:
print(new_list)
return new_list
if is_valid_position(i,j) and is_valid_position(i+1,j+1) and is_valid_position(i+2,j+2):
if board[i][j]==word[0] and board[i+1][j+1]==word[1] and board[i+2][j+2]==word[2]:
a=i
b=j
for k in range(len(word)):
if board[a][b]==word[k]:
new_list.append(a)
new_list.append(b)
print(new_list)
a+=1
b+=1
if len(new_list) ==len(word)*2:
print(new_list)
return new_list
if is_valid_position(i,j) and is_valid_position(i,j-1) and is_valid_position(i,j-2):
if board[i][j]==word[0] and board[i][j-1]==word[1] and board[i][j-2]==word[2]:
c=i
d=j
for k in range(len(word)):
if board[c][d]==word[k]:
new_list.append(c)
new_list.append(d)
print(new_list)
d-=1
if len(new_list) ==len(word)*2:
print(new_list)
return new_list
if is_valid_position(i,j) and is_valid_position(i-1,j) and is_valid_position(i-2,j):
if board[i][j]==word[0] and board[i-1][j]==word[1] and board[i-2][j]==word[2]:
f=i
g=j
for k in range(len(word)):
if board[f][g]==word[k]:
new_list.append(f)
new_list.append(g)
print(new_list)
f-=1
if len(new_list) ==len(word)*2:
print(new_list)
return new_list
board=[['p','o','t'],
['x','y','z'],
['a','p','o']]
output=[]
l=['pot','pyo','ozt']
for word in l:
output.extend(find(word))
print(output)
以上代码输出如下
length 3
[0, 0]
[0, 0, 0, 1]
[0, 0, 0, 1, 0, 2]
[0, 0, 0, 1, 0, 2]
length 3
[0, 0]
[0, 0, 1, 1]
[0, 0, 1, 1, 2, 2]
[0, 0, 1, 1, 2, 2]
length 3
[2, 2]
[2, 2, 1, 2]
[2, 2, 1, 2, 0, 2]
[2, 2, 1, 2, 0, 2]
[0, 0, 0, 1, 0, 2, 0, 0, 1, 1, 2, 2, 2, 2, 1, 2, 0, 2]