Python搜索回退功能异常:无法返回上一次搜索结果
问题:实现搜索结果的回退功能
需求:输入back时返回上一次搜索结果列表,而非当前结果。已尝试用列表存储新搜索结果与回退用历史结果,但逻辑存在问题。
现有代码
string = "Sam 23 pizza cola park\nJoe 19 hamburger sprite\nMike 25 cake fanta museum\nBen 22 pie" a = ['Name','Age','Food','Drink','Place'] b = ['Name','Age','Food','Drink'] c = ['Name','Age','Food',] e =[] f = [] l = [] pre = [] #Attaching keys with their values based off the number of values(words) they have: for line in string.splitlines(): words = line.split(' ') f.append(words) if (len(words) == 5): e.append((dict(zip(a, line.split())))) elif (len(words) == 4): e.append((dict(zip(b, line.split())))) else: e.append((dict(zip(c, line.split())))) search = input("Enter key: ") # Get first input while search != "q": # If input equals to q, quit entry = search.split() # Split the search into a list if multiply keys are inputted if 'back' in entry and len(pre) == 0 : # return the orginal data list print('\n',e,'\n') elif 'back' in entry and len(pre) != 0: # Return previous entry print('\n',pre,'\n') pre.clear() # Clear the previous entry after printing else: pre.clear() # Clear the previous entry for val in e: # Look for Name in List for word in entry: if word in val["Name"]: l.append(val) pre.append(val) if len(l) != 0: # If the the output list contains something print it print('\n',l,'\n') l.clear() # Clear for the new entry search = input("Enter key: ") # Next Search else: print("\ngoodbye. . .") # Print Goodbye if input was q
当前运行结果
Enter key: Sam [{'Name': 'Sam', 'Age': '23', 'Food': 'pizza', 'Drink': 'cola', 'Place': 'park'}] Enter key: Joe Mike [{'Name': 'Joe', 'Age': '19', 'Food': 'hamburger', 'Drink': 'sprite'}, {'Name': 'Mike', 'Age': '25', 'Food': 'cake', 'Drink': 'fanta', 'Place': 'museum'}] Enter key: back [{'Name': 'Joe', 'Age': '19', 'Food': 'hamburger', 'Drink': 'sprite'}, {'Name': 'Mike', 'Age': '25', 'Food': 'cake', 'Drink': 'fanta', 'Place': 'museum'}]
期望运行结果
Enter key: Sam [{'Name': 'Sam', 'Age': '23', 'Food': 'pizza', 'Drink': 'cola', 'Place': 'park'}] Enter key: Joe Mike [{'Name': 'Joe', 'Age': '19', 'Food': 'hamburger', 'Drink': 'sprite'}, {'Name': 'Mike', 'Age': '25', 'Food': 'cake', 'Drink': 'fanta', 'Place': 'museum'}] Enter key: back [{'Name': 'Sam', 'Age': '23', 'Food': 'pizza', 'Drink': 'cola', 'Place': 'park'}]
问题分析与解决思路
问题根源
当前代码中pre列表仅存储当前次的搜索结果,每次新搜索都会清空pre并覆盖为当前结果。执行back时,打印的是当前搜索的结果,而非上一次的,完全不符合回退逻辑。
解决思路
改用历史栈+当前结果的模式来记录搜索轨迹:
- 用
current_result变量保存当前展示的结果,初始值为原始数据e。 - 用
history列表作为栈,保存每次搜索前的结果,用于回退时恢复。 - 正常搜索时:
- 基于原始数据
e生成新的搜索结果。 - 若新结果非空,将当前结果存入
history栈,再把current_result更新为新结果并打印。
- 基于原始数据
- 输入
back时:- 若
history栈不为空,弹出栈顶元素作为新的current_result(即回到上一次结果)。 - 若栈为空,说明已经是原始数据,直接打印
current_result即可。
- 若
修改后的代码
string = "Sam 23 pizza cola park\nJoe 19 hamburger sprite\nMike 25 cake fanta museum\nBen 22 pie" a = ['Name','Age','Food','Drink','Place'] b = ['Name','Age','Food','Drink'] c = ['Name','Age','Food',] e =[] for line in string.splitlines(): words = line.split(' ') if len(words) == 5: e.append(dict(zip(a, line.split()))) elif len(words) == 4: e.append(dict(zip(b, line.split()))) else: e.append(dict(zip(c, line.split()))) current_result = e # 当前展示的结果,初始为原始数据 history = [] # 历史结果栈,用于回退 search = input("Enter key: ") while search != "q": entry = search.split() if 'back' in entry: # 处理回退逻辑 if history: current_result = history.pop() print('\n', current_result, '\n') else: # 正常搜索逻辑 new_result = [] for val in e: for word in entry: if word in val["Name"]: new_result.append(val) if new_result: # 将当前结果存入历史,更新为新结果 history.append(current_result) current_result = new_result print('\n', current_result, '\n') search = input("Enter key: ") else: print("\ngoodbye. . .")
这样修改后,就能实现输入back回到上一次搜索结果的需求,匹配期望运行结果。
内容的提问来源于stack exchange,提问作者Chicken_Cutlet
相关产品推荐
相关产品推荐

