【问题标题】:How do I compare list item values in a code, then add them together to find what items are most likely going to produce a higher number?如何比较代码中的列表项值,然后将它们加在一起以找出最有可能产生更高数字的项?
【发布时间】:2022-09-28 07:01:09
【问题描述】:

我希望将 1 个列表与另一个列表进行比较。然后,我将能够继续将这些值加在一起,而不是得到一个对或错的答案。我对此很陌生,而且我了解字典,但是我很难找到指向正确方向的资源。

列表:

mle = {\"Weapon\" : \"fist\", \"Damage\" : 73, \"Fire Rate\" : 80, \"Range\" : 59, \"Accuracy\" : 72, \"Recoil\" : 79, \"Mobility\" : 54, \"Handling\" : 51}

然后我想在这里与这个列表进行比较:

brassKnucles = {\"Attachment\" : \"brass\", \"Damage\" : 3, \"Fire Rate\" : 0, \"Range\" : 0, \"Accuracy\" : 3, \"Recoil\" : -2\", \"Mobility\" : 5, \"Handling\" : 0}

我想在黄铜指环的伤害中添加一项,“mle”造成的伤害。

从概念上讲,我会这样做

mle[2] + brassknucles[2]

现在虽然这很好,但我还有一件事要做。我想添加第三个列表。

rings = {\"Attachment\" : \"rings\", \"Damage\" : 1, \"Fire Rate\" : 0, \"Range\" : 0, \"Accuracy\" : 5, \"Recoil\" : -6\", \"Mobility\" : 7, \"Handling\" : 0}

现在我需要一个 if 语句,而且很难弄清楚我将如何编写它,我想在添加到 mle 之前比较黄铜指节和戒指。

我知道如何写出我想做的事情,我只是很难把它放在屏幕上。

我试过这个

def damage():
global a1, a2, a3, a4, a5, br1, br2, smg1, smg2, smg3, sg1, lmg1, lmg2, lmg3, mr1, mr2, sr, mle
if damage = \"Damage\":
    return
# Compare the stats
while(0==0):
    if mle[2] == 73:
        Print(mle)
        if brassKnucles[2] > rings[2]
            print(mle[2] + brassKnucles[2]
        else
            print(\"weapon\" + mle[2] + \"Attachment\" + rings[2])
        break
        
return

我这样做是正确的还是我完全错了?

  • 我懂字典.对不起,但我不认为你这样做。您不能通过索引访问字典(除非您将键设为索引,否则将毫无意义,因为您可以使用列表代替)。您所描述的列表也是字典。列表和字典都是数据结构,但它们的工作方式和使用方式非常不同。您尝试比较两个字典并尝试在键匹配时聚合值。

标签: python python-3.x list dictionary


【解决方案1】:

这是你要找的东西吗?在带有字典的 python 中,您可以通过键 brassKnuclesDMG = brassKnucles["Damage"] 访问值

def damage():

    if damage == "Damage":
        return
    # Compare the stats
    while(True):
        if mle["Damage"] == 73:
            print(mle)
            if brassKnucles["Damage"] > rings["Damage"]:
                print(mle["Damage"] + brassKnucles["Damage"])
            else:
                print("weapon" + mle["Damage"] + "Attachment" + rings["Damage"])
            break

如果您有兴趣,本文可以帮助您了解关键(bu dum tss)概念https://www.w3schools.com/python/python_dictionaries.asp

【讨论】:

    【解决方案2】:

    我不确定 else 语句试图做什么,但基本上如果黄铜核(我称之为 bs)受到的伤害比戒指多,那么你将 mle 的伤害值添加到 bs。这样做的代码是:

    if bs["Damage"] > rings["Damage"]:
        print(mle["Damage"] + bs["Damage"])
    

    如果上面的陈述是错误的,我猜你的 else 陈述会增加对戒指的伤害。如果是这样,请参阅下面的代码:

    else: print(mle["Damage"] + rings["Damage"])
    

    【讨论】:

      【解决方案3】:

      编程的第一次尝试很棘手,所以耐心等待,我们也许可以从熟悉访问字典的基本操作开始:

      mle = {"Weapon" : "fist", "Damage" : 73, "Fire Rate" : 80, "Range" : 59, "Accuracy" : 72, "Recoil" : 79, "Mobility" : 54, "Handling" : 51}
      brassKnucles = {"Attachment" : "brass", "Damage" : 3, "Fire Rate" : 0, "Range" : 0, "Accuracy" : 3, "Recoil" : -2, "Mobility" : 5, "Handling" : 0}
      rings = {"Attachment" : "rings", "Damage" : 1, "Fire Rate" : 0, "Range" : 0, "Accuracy" : 5, "Recoil" : -6, "Mobility" : 7, "Handling" : 0}
      
      print(mle["Damage"])
      print(mle["Damage"]+brassKnucles["Damage"])
      print(mle["Damage"]+brassKnucles["Damage"]+rings["Damage"])
      

      输出:

      73
      76
      77
      

      迭代字典

      for k,v in mle.items():
          print(k,v)
      

      输出:

      Weapon fist
      Damage 73
      Fire Rate 80
      Range 59
      Accuracy 72
      Recoil 79
      Mobility 54
      Handling 51
      

      或者

      for k in mle:
          print(k,mle[k])
      

      输出:

      Weapon fist
      Damage 73
      Fire Rate 80
      Range 59
      Accuracy 72
      Recoil 79
      Mobility 54
      Handling 51
      

      然后再次尝试编写您要处理的内容;)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-09
        • 2021-09-28
        • 2022-02-08
        相关资源
        最近更新 更多