编辑:如果它对上下文有所帮助,我实际上是在尝试将其推广到for和while循环中,在这里我希望这样的东西可以工作
if dictA[certainkey:'certainvalue'] == dictB[certainkey:'certainvalue']:
return True
原始问题: 有没有一种方法可以比较Python3中两个字典中的相同键,看它们是否具有相同的值?
例如,说
dictA = {1:'Y', 2:'E', 3:'E'}
dictB = {1:'Y', 2:'A', 3:'W'}
如果dictA中的一组键值对与dictB中的同一组键值对匹配,我希望程序为我返回True值。在这种情况下,键1在dictA和dictB中都与值“ Y”配对。我不知道是否有好的方法可以做到这一点,所以我自然尝试了
print(dictA[1:'Y'] == dictB[1:'Y'])
希望能印出我的真相。由于它不起作用(而且我对字典还不了解),我想知道是否还有其他方法可以做到这一点。谢谢!
答案 0 :(得分:3)
您可以在相等(键,值)对上合并字典,然后对合并的字典运行后续检查。
select
创建def rock_paper_scissors():
playerScore = 0
computerScore = 0
print("")
player = input("Choose Rock, Paper, or Scissors: ")
player = player.lower()
choices = ["rock", "paper", "scissors"]
computer = random.choice(choices)
if player == computer:
print("I chose " + str(computer) + " and you chose " + player + ". It's a tie!")
elif player == "rock" and computer == "scissors":
playerScore += 1
print("I chose " + str(computer) + " and you chose " + player + ". Congratulations! You won! " + player + " beats " + str(computer) + ".")
elif player == "paper" and computer == "rock":
playerScore += 1
print("I chose " + str(computer) + " and you chose " + player + ". Congratulations! You won! " + player + " beats " + str(computer) + ".")
elif player == "scissors" and computer == "paper":
playerScore += 1
print("I chose " + str(computer) + " and you chose " + player + ". Congratulations! You won! " + player + " beats " + str(computer) + ".")
elif computer == "rock" and player == "scissors":
computerScore += 1
print("I chose " + str(computer) + " and you chose " + player + ". You lost! " + str(computer) + " beats " + player + ".")
elif computer == "paper" and player == "rock":
computerScore += 1
print("I chose " + str(computer) + " and you chose " + player + ". You lost! " + str(computer) + " beats " + player + ".")
elif computer == "scissors" and player == "paper":
computerScore += 1
print("I chose " + str(computer) + " and you chose " + player + ". You lost! " + str(computer) + " beats " + player + ".")
else:
print("Sorry, but you entered an invalid option. The game has ended. See below for the final score. Thank you for playing")
print("")
print("Your score:", str(playerScore) + ", Computer score:", str(computerScore))
return playerScore, computerScore
playerFinal = 0
computerFinal = 0
while True:
player, computer = rock_paper_scissors()
playerFinal += player
computerFinal += computer
print("Your score:", str(playerFinal) + ", Computer score:", computerFinal)
很容易,因为>>> dictA = {1:'Y', 2:'E', 3:'E'}
>>> dictB = {1:'Y', 2:'A', 3:'W'}
>>>
>>> merged = dict(dictA.items() & dictB.items())
>>> merged
{1: 'Y'}
>>>
>>> 1 in merged
True
>>> 3 in merged
False
,merged
和dict.keys
的返回值在诸如联合,交集等操作方面支持dict.values
接口等
注意:在您的字典中需要可哈希值。如果您具有不可散列的值,请通过
创建dict.items
set
答案 1 :(得分:2)
首先获取字典之间的匹配键,然后使用字典理解来比较每个匹配键。
matching_keys = dictA.keys() & dictB.keys()
>>> {k: dictA[k] == dictB[k] for k in matching_keys}
{1: True, 2: False, 3: False}
要仅获取值匹配的键,请使用列表理解:
keys_with_matching_values = [k for k in matching_keys if dictA[k] == dictB[k]]
# Or: [k for k in dictA if k in dictA and dictA[k] == dictB[k]]
>>> keys_with_matching_values
[1]
请注意,这两种方法都可以提高内存效率,因为不必将匹配值存储在字典中。
答案 2 :(得分:1)
for key in dictA
i = 0
if key in dictA:
word = dictA[0][i]
if dictA[0][i] == dictB[0][0]:
print(word + 'matches dict 0')
if dictA[0][i] == dictB[0][1]:
print(word + 'matches dict 1')
if dictA[0][i] == dictB[0][2]:
print(word + 'matches dict 2')
i += 1
我在工作中的班车上写得很快,它马虎,我可以为dictB做个循环。但这应该可以使查找单词的过程自动化。 (但可能不起作用,因为我写得很快)
答案 3 :(得分:1)
您可以做您在列表理解中提到的事情
print([True if dicta[k] == dictb[k] else False for k in dicta])
# [True, False, False]
扩展
for k in dicta:
if dicta[k] == dictb[k]:
print(True)
else:
print(False)