用户输入经过字典列表。

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

User Input going through list of Dictionaries

问题

我尝试使输出返回先前的列表而不是相同的列表。我创建了两个列表,一个用于每次搜索的新条目,另一个用于保存搜索以便在用户输入为'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 = []

# 根据它们的值的数量将键与值关联起来:
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: ")  # 获取第一个输入
while search != "q":  # 如果输入等于q,退出
    entry = search.split()  # 如果输入了多个键,将搜索分割为列表
    if 'back' in entry and len(pre) == 0:  # 返回原始数据列表
        print('\n', e, '\n')
    elif 'back' in entry and len(pre) != 0:  # 返回以前的条目
        print('\n', pre, '\n')
        pre.clear()  # 打印后清除以前的条目
    else:
        pre.clear()  # 清除以前的条目
        for val in e:  # 在列表中查找Name
            for word in entry:
                if word in val["Name"]:
                    l.append(val)
                    pre.append(val)
    if len(l) != 0:  # 如果输出列表包含内容,请打印它
        print('\n', l, '\n')

    l.clear()  # 为新条目清除
    search = input("Enter key: ")  # 下一个搜索
else:
    print("\ngoodbye. . .")  # 如果输入为q,则打印Goodbye

从脚本中我得到:

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'}]

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

英文:

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'.

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
   

From my script I get:

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'}] 

I would like to get

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'}] 

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"时,打印倒数第二的结果。

string = "Sam 23 pizza cola park\nJoe 19 hamburger sprite\nMike 25 cake fanta museum\nBen 22 pie"

keys = ['Name', 'Age', 'Food', 'Drink', 'Place']

e = []
f = []
l = []
pre = []
pre_pre = []

#根据它们拥有的值(单词)的数量,将键与它们的值关联起来:
for line in string.splitlines():
    words = line.split()
    f.append(words)
    e.append(dict(zip(keys, words)))

search = input("Enter key: ")       # 获取第一个输入
while search != "q":                # 如果输入等于"q",则退出
    entry = search.split()          # 如果输入了多个键,将搜索拆分成列表
    if 'back' in entry and len(pre_pre) == 0 :   # 返回原始数据列表
        print('\n', e, '\n')

    elif 'back' in entry and len(pre_pre) != 0:     # 返回上一个条目
        print('\n', pre_pre, '\n')
        pre.clear()                             # 打印后清除上一个条目
    else:
        pre_pre = pre
        pre = []                             # 清除上一个条目
        for val in e:                           # 在列表中查找"Name"
            for word in entry:
                if word in val["Name"]:
                        l.append(val)
                        pre.append(val)
    if len(l) != 0:                             # 如果输出列表包含内容,则打印它
        print('\n', l, '\n')

    l.clear()                                   # 为新的搜索清除
    search = input("Enter key: ")               # 下一个搜索
else:
   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.

string = "Sam 23 pizza cola park\nJoe 19 hamburger sprite\nMike 25 cake fanta museum\nBen 22 pie"

keys = ['Name','Age','Food','Drink','Place']

e =[]
f = []
l = []
pre = []
pre_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)
    e.append(dict(zip(keys, words)))

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_pre) == 0 :   # return the orginal data list
        print('\n',e,'\n')

    elif 'back' in entry and len(pre_pre) != 0:     # Return previous entry
        print('\n',pre_pre,'\n')
        pre.clear()                             # Clear the previous entry after printing
    else:
        pre_pre = pre
        pre = []                             # 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

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:

确定