![](/img/trans.png)
[英]Python Join 2 dictionaries into 3rd, 2D dictionary where key from dictionary 2 is list value in dictionary 1
[英]Is there a way to attach a 3rd value in Python dictionaries?
這可能是一個愚蠢的問題,我相信我很有可能采取完全錯誤的方法來解決我的問題,所以如果有人知道更好的方法,請隨時糾正我/指出我正確的方向。
我正在嘗試用Python制作一個基本的測驗程序以獲得樂趣,我似乎無法找到一個好方法來實際上用他們的答案調用問題並存儲正確答案應該是什么。 理想情況下,我也希望在問題中改變答案,例如#1的答案也不總是“A”,但目前這個步驟太過分了。
無論如何,我認為字典可以工作。 我會做這樣的事情:
test = {
'1': "What is 1+1?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n", #A
'2': "What is 2+2?\nA) 2\nB) 4\nC) 1\nD) None of the above.\n\n", #B
'3': "What is 3+3?\nA) 2\nB) 11\nC) 6\nD) None of the above.\n\n", #C
'4': "What is 4+4?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n", #D
'5': "What is 5+5?\nA) 2\nB) 11\nC) 10\nD) None of the above.\n\n", #C
'6': "What is 6+6?\nA) 2\nB) 12\nC) 1\nD) None of the above.\n\n", #B
}
answer = raw_input(test['1'])
我可以使用它來按順序或隨機稍微修改輕松打印出問題。 但是,我無法將正確的答案附加到詞典中,這意味着我必須做一些像做出荒謬的if
語句的事情。 有誰知道解決方案? 有沒有辦法為每個字典條目添加第3個值? 一個解決方案,我完全忽略了? 任何幫助將不勝感激。
將字典的值設為元組:
'1': ("What is 1+1?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n", 'A')
然后,您可以通過test['1'][0]
和通過test['1'][1]
的答案訪問問題。
看起來你的數據結構錯誤。 您應該使用字典列表,而不是您的其他答案所建議的元組或列表字典。
questions = [
{'question':"What is 1+1?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n",
'answer':"#A"},
{'question':"What is 2+2?\nA) 2\nB) 4\nC) 1\nD) None of the above.\n\n",
'answer':"#B"},
...}
這將讓您通過以下方式迭代您的問題:
for q in questions:
print(q['question'])
ans = input(">> ")
if ans == q['answer']:
# correct!
else:
# wrong!
如果你仍然需要數字,你可以將number
保存為字典的另一個鍵(使這種類似於數據庫中的行, number
是主鍵):
{'number': 1,
'question': ...,
'answer': ...}
或者使用帶有start
關鍵字參數的enumerate
迭代您的問題
for q_num, q in enumerate(questions, start=1):
print("{}. {}".format(q_num, q['question']))
...
將值放入list
中,如下所示
test = {
'1': ["What is 1+1?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n", '#A' ]
'2': ["What is 2+2?\nA) 2\nB) 4\nC) 1\nD) None of the above.\n\n", '#B' ]
'3': ["What is 3+3?\nA) 2\nB) 11\nC) 6\nD) None of the above.\n\n", '#C' ]
'4': ["What is 4+4?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n", '#D' ]
'5': ["What is 5+5?\nA) 2\nB) 11\nC) 10\nD) None of the above.\n\n", '#C' ]
'6': ["What is 6+6?\nA) 2\nB) 12\nC) 1\nD) None of the above.\n\n", '#B' ]
}
然后訪問它
question = raw_input(test['1'][0])
answer = raw_input(test['1'][1])
拋棄字典,讓python為你做更多的工作。 您可以使用列表表示每個問題/答案組,其中第一項是問題,最后一項是答案,其間的所有項都是多項選擇答案。 把它們放在另一個列表中,很容易將它們改組。
import string
import random
# a list of question/choices/answer lists
test = [
# question choices.... answer
["What is 1+1?", "2", "11", "1", "None of the above", 0],
["What is 2+2?", "2", "11", "4", "None of the above", 2],
# ... more questions ...
]
# shuffle the test so you get a different order every time
random.shuffle(test)
# an example for giving the test...
# 'enumerate' gives you a count (starting from 1 in this case) and
# each test list in turn
for index, item in enumerate(test, 1):
# the question is the first item in the list
question = item[0]
# possible answers are everything between the first and last items
answers = item[1:-1]
# the index to the real answer is the final item
answer = item[-1]
# print out the question and possible answers. 'string.uppercase' is a list
# of "ABC...XYZ" so we can change the index of the answer to a letter
print "%d) %s" % (index, question)
for a_index, answer in enumerate(answers):
print " %s) %s" % (string.uppercase[a_index], answer)
# prompt for an answer
data = raw_input("Answer? ")
# see if it matches expected
if data == string.uppercase[answer]:
print "Correct!"
else:
print "No... better luck next time"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.