在python中使用for循环时如何更新列表
我正在制作一个程序,旨在利用函数解决单词搜索问题。我从函数find_horizontal得到的输出 是:在python中使用for循环时如何更新列表,python,list,function,for-loop,append,Python,List,Function,For Loop,Append,我正在制作一个程序,旨在利用函数解决单词搜索问题。我从函数find_horizontal得到的输出 是: 问题是它在列表Outpuz中添加了一行,将找到的单词划掉,然后在划掉之前再添加第二行。我期望的输出是: 这是我的全部代码: if __name__ == '__main__': Puzzle = ["FUNCTIONRRIRAI", "RAIOONFRCCPWON", "PTCSNOBEUITOLO",
问题是它在列表Outpuz中添加了一行,将找到的单词划掉,然后在划掉之前再添加第二行。我期望的输出是:
这是我的全部代码:
if __name__ == '__main__':
Puzzle = ["FUNCTIONRRIRAI",
"RAIOONFRCCPWON",
"PTCSNOBEUITOLO",
"BNCACIANTOSLIH",
"RBYOLILYNREFBT",
"HYYNOGESTIBRIY",
"AATTSIONCMCENP",
"UORTENRRCBFVAU",
"CEBEECVWIERORI",
"PROCESSORTOPYF",
"OHCOMPUTERHSOS",
"YCYPRESREOSMRW",
"OATHBRMVTHHCTR",
"PGORWOOUIPSCHP"]
def load_words_to_find(file_name):
word_list = []
file = open(file_name, "r")
for line in file.readlines():
word_list.append(line)
word_list = list(map(lambda s: s.strip(), word_list))
return word_list
def find_horizontal(Puzzle, Words, ReplaceWith, Found):
# Parameters :- List:Puzzle, List:Words, Character:ReplaceWith, List:Found
# Return :- List:Outpuz, List:Found
# Find all words which are horizontally in place (left to right and right to left), return the puzzle and list of found words
add = True
Outpuz = []
for line in Puzzle:
for word in Words:
if word in line:
Found.append(word)
print("Found: ", word)
Outpuz.append(line.replace(word,ReplaceWith*len(word)))
add = False
if not add:
pass
elif add:
Outpuz.append(line)
Outpuz.append(line)
print(Outpuz)
return Outpuz, Found
find_horizontal(Puzzle, load_words_to_find("words.txt"), "*", [])
大约一年前,我做了类似的事情
我认为问题是Outpuz。append(line)只是将拼图中的每一行添加到输出
你的文件名是什么?发现和输出的应该是什么?我猜是从拼图中找到的单词列表。但是,什么是outpuz
我可以从以下位置更改它:
if word in line:
Found.append(word)
print("Found: ", word)
Outpuz.append(line.replace(word,ReplaceWith*len(word)))
add = False
if not add:
pass
elif add:
Outpuz.append(line)
Outpuz.append(line
Outpuz.append(line)
为此:
if word in line:
Found.append(word)
print("Found: ", word)
Outpuz.append(line.replace(word,ReplaceWith*len(word)))
#Adds the line if it has the change
else:
Outpuz.append(line) #Adds the line if it doesnt have the change.
大约一年前,我做了类似的事情
我认为问题是Outpuz。append(line)只是将拼图中的每一行添加到输出
你的文件名是什么?发现和输出的应该是什么?我猜是从拼图中找到的单词列表。但是,什么是outpuz
我可以从以下位置更改它:
if word in line:
Found.append(word)
print("Found: ", word)
Outpuz.append(line.replace(word,ReplaceWith*len(word)))
add = False
if not add:
pass
elif add:
Outpuz.append(line)
Outpuz.append(line
Outpuz.append(line)
为此:
if word in line:
Found.append(word)
print("Found: ", word)
Outpuz.append(line.replace(word,ReplaceWith*len(word)))
#Adds the line if it has the change
else:
Outpuz.append(line) #Adds the line if it doesnt have the change.
我不确定你的文件名是什么意思,你能澄清一下吗?您对“查找”的看法是正确的,Outpuz应该是更新的单词搜索(它是用于大学家庭作业的,我的老师设计了代码为他进行自我标记,因此我无法更改返回名称,因此我假设它是更新的单词搜索)请参见我的编辑,堆栈溢出的新功能,因此不确定我是否正确使用了它。我更改了代码,因此现在如果找到了if,它会将其添加到Outpuz,但如果找不到它(如果单词不在line/else),则会添加该行,但即使找到它,它仍会添加该行:我不确定您的文件名中包含的内容是什么意思,请澄清?您对“查找”的看法是正确的,Outpuz应该是更新的单词搜索(它是用于大学家庭作业的,我的老师设计了代码为他进行自我标记,因此我无法更改返回名称,因此我假设它是更新的单词搜索)请参见我的编辑,堆栈溢出的新功能,因此不确定我是否正确使用了它。我更改了代码,因此现在如果找到了,则将其添加到Outpuz,但如果未找到(如果单词不在行/else),则添加行,但即使找到了,仍会添加行: