[英]How to create a dictionary from 2 lists and other dictionary in Python?
1.First I created variables POWER
, gandalf
and saruman
as seen above in the code then a variable called spells
to store the number of spells that the sorcerers cast. 1.首先,我在代码中创建了变量
POWER
、 gandalf
和saruman
,然后创建了一个名为spells
的变量来存储术士施放的法术数量。
code代码
POWER = {
'Fireball': 50,
'Lightning bolt': 40,
'Magic arrow': 10,
'Black Tentacles': 25,
'Contagion': 45
}
gandalf = ['Fireball', 'Lightning bolt', 'Lightning bolt', 'Magic arrow', 'Fireball',
'Magic arrow', 'Lightning bolt', 'Fireball', 'Fireball', 'Fireball']
saruman = ['Contagion', 'Contagion', 'Black Tentacles', 'Fireball', 'Black Tentacles',
'Lightning bolt', 'Magic arrow', 'Contagion', 'Magic arrow', 'Magic arrow']
spells=10
gandalf_wins=0
saruman_wins=0
gandalf_power=[]
saruman_power=[]
I am stucked here because I don't know how to create a dictionary for each with the spells and the power of this spells.我被困在这里,因为我不知道如何为每个咒语和咒语的力量创建字典。 Then, how should I compare them?
那么,我应该如何比较它们呢? Thanks in advance!
提前致谢!
I think the steps are instructed pretty clear, using a for
loop is enough to solve the 3rd part.我认为这些步骤指示得很清楚,使用
for
循环足以解决第三部分。
For the 4th part, you can use zip
, to iterrate both list at the same time.对于第 4 部分,您可以使用
zip
来同时迭代两个列表。
for gandalf_pow, saruman_pow in zip(gandalf_power, saruman_power):
# compare
The below is using zip to loop through the spells and compares each rounds spell power.下面是使用 zip 循环遍历法术并比较每一轮的法术强度。
POWER = {
'Fireball': 50,
'Lightning bolt': 40,
'Magic arrow': 10,
'Black Tentacles': 25,
'Contagion': 45
}
gandalf = ['Fireball', 'Lightning bolt', 'Lightning bolt', 'Magic arrow', 'Fireball', 'Magic arrow', 'Lightning bolt', 'Fireball', 'Fireball', 'Fireball']
saruman = ['Contagion', 'Contagion', 'Black Tentacles', 'Fireball', 'Black Tentacles', 'Lightning bolt', 'Magic arrow', 'Contagion', 'Magic arrow', 'Magic arrow']
spells=10
gandalf_wins=0
saruman_wins=0
for gandalf_spell, saruman_spell in zip(gandalf,saruman):
if POWER[gandalf_spell] > POWER[saruman_spell]:
gandalf_wins+=1
elif POWER[saruman_spell] > POWER[gandalf_spell]:
saruman_wins+=1
print(f"Gandalf won {gandalf_wins} times, Saruman won {saruman_wins} times")
you should refer Python Dictionaries.你应该参考 Python 字典。 They are just like lists and tuples only the difference is that they are like database's table where you have rows and columns for storing different values
它们就像列表和元组,唯一的区别是它们就像数据库的表,你有用于存储不同值的行和列
https://www.w3schools.com/python/python_dictionaries.asp Refer this for help https://www.w3schools.com/python/python_dictionaries.asp参考这里获取帮助
I don't think you need a dictionary for this.我认为您不需要为此使用字典。 You can try:
你可以试试:
for x in range(spells):
gandalf_attack = POWER[gandalf[x]]
saruman_attack = POWER[saruman[x]]
if gandalf_attack>saruman_attack:
# Do something
elif gandalf_attack>saruman_attack:
# Do something
else:
# Tie
Finally, I did this and worked for me:最后,我这样做并为我工作:
for spell in gandalf:
gandalf_power.append(POWER[spell])
for spell in saruman:
saruman_power.append(POWER[spell])
winner = ""
for g, s in zip(gandalf_power, saruman_power):
if g > s:
gandalf_wins+=1
saruman_wins=0
elif s > g:
saruman_wins+=1
gandalf_wins=0
if gandalf_wins==3:
winner="Gandalf"
break
elif saruman_wins==3:
winner="Saruman"
break
if gandalf_wins < 3 and saruman_wins < 3:
print("Tie.")
else:
print(f"The winner is {winner}!")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.