用户输入经过字典列表。

huangapple go评论108阅读模式
英文:

User Input going through list of Dictionaries

问题

我尝试使输出返回先前的列表而不是相同的列表。我创建了两个列表,一个用于每次搜索的新条目,另一个用于保存搜索以便在用户输入为'back'时使用。

  1. string = "Sam 23 pizza cola park\nJoe 19 hamburger sprite\nMike 25 cake fanta museum\nBen 22 pie"
  2. a = ['Name', 'Age', 'Food', 'Drink', 'Place']
  3. b = ['Name', 'Age', 'Food', 'Drink']
  4. c = ['Name', 'Age', 'Food']
  5. e = []
  6. f = []
  7. l = []
  8. pre = []
  9. # 根据它们的值的数量将键与值关联起来:
  10. for line in string.splitlines():
  11. words = line.split(' ')
  12. f.append(words)
  13. if (len(words) == 5):
  14. e.append((dict(zip(a, line.split())))
  15. elif (len(words) == 4):
  16. e.append((dict(zip(b, line.split())))
  17. else:
  18. e.append((dict(zip(c, line.split())))
  19. search = input("Enter key: ") # 获取第一个输入
  20. while search != "q": # 如果输入等于q,退出
  21. entry = search.split() # 如果输入了多个键,将搜索分割为列表
  22. if 'back' in entry and len(pre) == 0: # 返回原始数据列表
  23. print('\n', e, '\n')
  24. elif 'back' in entry and len(pre) != 0: # 返回以前的条目
  25. print('\n', pre, '\n')
  26. pre.clear() # 打印后清除以前的条目
  27. else:
  28. pre.clear() # 清除以前的条目
  29. for val in e: # 在列表中查找Name
  30. for word in entry:
  31. if word in val["Name"]:
  32. l.append(val)
  33. pre.append(val)
  34. if len(l) != 0: # 如果输出列表包含内容,请打印它
  35. print('\n', l, '\n')
  36. l.clear() # 为新条目清除
  37. search = input("Enter key: ") # 下一个搜索
  38. else:
  39. print("\ngoodbye. . .") # 如果输入为q,则打印Goodbye

从脚本中我得到:

  1. Enter key: Sam
  2. [{'Name': 'Sam', 'Age': '23', 'Food': 'pizza', 'Drink': 'cola', 'Place': 'park'}]
  3. Enter key: Joe Mike
  4. [{'Name': 'Joe', 'Age': '19', 'Food': 'hamburger', 'Drink': 'sprite'}, {'Name': 'Mike', 'Age': '25', 'Food': 'cake', 'Drink': 'fanta', 'Place': 'museum'}]
  5. Enter key: back
  6. [{'Name': 'Joe', 'Age': '19', 'Food': 'hamburger', 'Drink': 'sprite'}, {'Name': 'Mike', 'Age': '25', 'Food': 'cake', 'Drink': 'fanta', 'Place': 'museum'}]

我想要的是:

  1. Enter key: Sam
  2. [{'Name': 'Sam', 'Age': '23', 'Food': 'pizza', 'Drink': 'cola', 'Place': 'park'}]
  3. Enter key: Joe Mike
  4. [{'Name': 'Joe', 'Age': '19', 'Food': 'hamburger', 'Drink': 'sprite'}, {'Name': 'Mike', 'Age': '25', 'Food': 'cake', 'Drink': 'fanta', 'Place': 'museum'}]
  5. Enter key: back
  6. [{'Name': 'Sam', 'Age': '23', 'Food': 'pizza', 'Drink': 'cola', 'Place': 'park'}]

我认为这与如何保存以前的条目有关,但不确定如何返回它。任何有助于引导我到正确方法的帮助都将有所帮助。

英文:

I am trying to have the output to return the previous list instead of the same list. I created two lists, one for the new entry for every search and one to save the search to be used if the User input is 'back'.

  1. string = "Sam 23 pizza cola park\nJoe 19 hamburger sprite\nMike 25 cake fanta museum\nBen 22 pie"
  2. a = ['Name','Age','Food','Drink','Place']
  3. b = ['Name','Age','Food','Drink']
  4. c = ['Name','Age','Food',]
  5. e =[]
  6. f = []
  7. l = []
  8. pre = []
  9. #Attaching keys with their values based off the number of values(words) they have:
  10. for line in string.splitlines():
  11. words = line.split(' ')
  12. f.append(words)
  13. if (len(words) == 5):
  14. e.append((dict(zip(a, line.split()))))
  15. elif (len(words) == 4):
  16. e.append((dict(zip(b, line.split()))))
  17. else:
  18. e.append((dict(zip(c, line.split()))))
  19. search = input("Enter key: ") # Get first input
  20. while search != "q": # If input equals to q, quit
  21. entry = search.split() # Split the search into a list if multiply keys are inputted
  22. if 'back' in entry and len(pre) == 0 : # return the orginal data list
  23. print('\n',e,'\n')
  24. elif 'back' in entry and len(pre) != 0: # Return previous entry
  25. print('\n',pre,'\n')
  26. pre.clear() # Clear the previous entry after printing
  27. else:
  28. pre.clear() # Clear the previous entry
  29. for val in e: # Look for Name in List
  30. for word in entry:
  31. if word in val["Name"]:
  32. l.append(val)
  33. pre.append(val)
  34. if len(l) != 0: # If the the output list contains something print it
  35. print('\n',l,'\n')
  36. l.clear() # Clear for the new entry
  37. search = input("Enter key: ") # Next Search
  38. else:
  39. print("\ngoodbye. . .") # Print Goodbye if input was q

From my script I get:

  1. Enter key: Sam
  2. [{'Name': 'Sam', 'Age': '23', 'Food': 'pizza', 'Drink': 'cola', 'Place': 'park'}]
  3. Enter key: Joe Mike
  4. [{'Name': 'Joe', 'Age': '19', 'Food': 'hamburger', 'Drink': 'sprite'}, {'Name': 'Mike', 'Age': '25', 'Food': 'cake', 'Drink': 'fanta', 'Place': 'museum'}]
  5. Enter key: back
  6. [{'Name': 'Joe', 'Age': '19', 'Food': 'hamburger', 'Drink': 'sprite'}, {'Name': 'Mike', 'Age': '25', 'Food': 'cake', 'Drink': 'fanta', 'Place': 'museum'}]

I would like to get

  1. Enter key: Sam
  2. [{'Name': 'Sam', 'Age': '23', 'Food': 'pizza', 'Drink': 'cola', 'Place': 'park'}]
  3. Enter key: Joe Mike
  4. [{'Name': 'Joe', 'Age': '19', 'Food': 'hamburger', 'Drink': 'sprite'}, {'Name': 'Mike', 'Age': '25', 'Food': 'cake', 'Drink': 'fanta', 'Place': 'museum'}]
  5. Enter key: back
  6. [{'Name': 'Sam', 'Age': '23', 'Food': 'pizza', 'Drink': 'cola', 'Place': 'park'}]

I believe it has something to do with how I save the previous entry but not sure how to go back it. Any help to lead me to the correct approach would be helpful

答案1

得分: 0

你需要两个变量。一个用于存储最后的结果,另一个用于存储倒数第二的结果。在每次搜索之前,将最后的结果复制到倒数第二的结果。当输入"back"时,打印倒数第二的结果。

  1. string = "Sam 23 pizza cola park\nJoe 19 hamburger sprite\nMike 25 cake fanta museum\nBen 22 pie"
  2. keys = ['Name', 'Age', 'Food', 'Drink', 'Place']
  3. e = []
  4. f = []
  5. l = []
  6. pre = []
  7. pre_pre = []
  8. #根据它们拥有的值(单词)的数量,将键与它们的值关联起来:
  9. for line in string.splitlines():
  10. words = line.split()
  11. f.append(words)
  12. e.append(dict(zip(keys, words)))
  13. search = input("Enter key: ") # 获取第一个输入
  14. while search != "q": # 如果输入等于"q",则退出
  15. entry = search.split() # 如果输入了多个键,将搜索拆分成列表
  16. if 'back' in entry and len(pre_pre) == 0 : # 返回原始数据列表
  17. print('\n', e, '\n')
  18. elif 'back' in entry and len(pre_pre) != 0: # 返回上一个条目
  19. print('\n', pre_pre, '\n')
  20. pre.clear() # 打印后清除上一个条目
  21. else:
  22. pre_pre = pre
  23. pre = [] # 清除上一个条目
  24. for val in e: # 在列表中查找"Name"
  25. for word in entry:
  26. if word in val["Name"]:
  27. l.append(val)
  28. pre.append(val)
  29. if len(l) != 0: # 如果输出列表包含内容,则打印它
  30. print('\n', l, '\n')
  31. l.clear() # 为新的搜索清除
  32. search = input("Enter key: ") # 下一个搜索
  33. else:
  34. print("\ngoodbye. . .") # 如果输入是"q",则打印"再见"
英文:

You need two variables. One for the last result, another for the 2nd-to-last result. Before each search, copy the last to the 2nd-to-last. When they enter back, print the 2nd-to-last.

  1. string = "Sam 23 pizza cola park\nJoe 19 hamburger sprite\nMike 25 cake fanta museum\nBen 22 pie"
  2. keys = ['Name','Age','Food','Drink','Place']
  3. e =[]
  4. f = []
  5. l = []
  6. pre = []
  7. pre_pre = []
  8. #Attaching keys with their values based off the number of values(words) they have:
  9. for line in string.splitlines():
  10. words = line.split()
  11. f.append(words)
  12. e.append(dict(zip(keys, words)))
  13. search = input("Enter key: ") # Get first input
  14. while search != "q": # If input equals to q, quit
  15. entry = search.split() # Split the search into a list if multiply keys are inputted
  16. if 'back' in entry and len(pre_pre) == 0 : # return the orginal data list
  17. print('\n',e,'\n')
  18. elif 'back' in entry and len(pre_pre) != 0: # Return previous entry
  19. print('\n',pre_pre,'\n')
  20. pre.clear() # Clear the previous entry after printing
  21. else:
  22. pre_pre = pre
  23. pre = [] # Clear the previous entry
  24. for val in e: # Look for Name in List
  25. for word in entry:
  26. if word in val["Name"]:
  27. l.append(val)
  28. pre.append(val)
  29. if len(l) != 0: # If the the output list contains something print it
  30. print('\n',l,'\n')
  31. l.clear() # Clear for the new entry
  32. search = input("Enter key: ") # Next Search
  33. else:
  34. print("\ngoodbye. . .") # Print Goodbye if input was q

huangapple
  • 本文由 发表于 2023年3月7日 05:02:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定