简体   繁体   English

如何从 Python 中的 2 个列表和其他字典创建字典?

[英]How to create a dictionary from 2 lists and other dictionary in Python?

Answer here .在这里回答。

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.首先,我在代码中创建了变量POWERgandalfsaruman ,然后创建了一个名为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

  1. Then created two variables called gandalf_wins and saruman_wins.然后创建了两个名为 gandalf_wins 和 saruman_wins 的变量。 Set both of them to 0.将它们都设置为 0。
gandalf_wins=0
saruman_wins=0
  1. And Lastly two variables called gandalf_power and saruman_power to store the list of spell powers of each sorcerer.最后是两个名为 gandalf_power 和 saruman_power 的变量,用于存储每个巫师的法术能力列表。
gandalf_power=[]
saruman_power=[]
  1. The battle starts, Using the variables you've created above.战斗开始,使用您在上面创建的变量。 code the execution of spell clashes.编码拼写冲突的执行。 Remember that a sorcerer wins if he succeeds in winning 3 spell clashes in a row, If a clash ends up in a tie.请记住,如果巫师连续赢得 3 次法术冲突,则巫师获胜,如果冲突以平局告终。 the counter of wins in a row is not restarted to 0. Remember to print who is the winner of the battle.连胜的计数器不会重新开始为 0。记得打印谁是战斗的赢家。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM