【问题标题】:How do I make 2 lists correspond to each other? [duplicate]如何使 2 个列表相互对应? [复制]
【发布时间】:2020-09-18 18:48:31
【问题描述】:

我这里有 2 个列表:

list1 = [happy, sad, grumpy, mad]
list2 = [2, 5, 6, 9]

我想让数字分配给情绪? (快乐等于 2,悲伤等于 5,等等)。理想情况下,我想这样做,以便您可以比较 list1 中的项目,例如:

if happy > sad:
    print ("you are happy")

我想让这段代码尽可能高效,所以我不想分别为 list1 中的每个变量分配一个数字。

【问题讨论】:

  • 您的list1 中是否有变量happy、sad、grumpy、mad,或者您的意思是“happy”、“sad”、“grumpy”、“mad”?

标签: python python-3.x list


【解决方案1】:

您可以将列表一起zip 并从中创建dict

list1 = ["happy", "sad", "grumpy", "mad"]
list2 = [2, 5, 6, 9]

moods = dict(zip(list1, list2))
# This will create a dictionary like this
# {'happy': 2, 'sad': 5, 'grumpy': 6, 'mad': 9}


if moods["happy"] > moods["sad"]:
    print("You are happy")
else:
    print("You are sad")

输出是:

You are sad

编辑:

如果您不在乎其他值是什么,另一种选择是直接选择一种心情(受Laurent B. 's answer 启发):

list1 = ["happy", "sad", "grumpy", "mad"]
list2 = [2, 5, 6, 9]

mood = list1[list2.index(max(list2))]
print("You are", mood)

输出:

You are mad

【讨论】:

    【解决方案2】:

    一个有趣的方法是创建一个这样的字典:

    dico = {'happy':2, 'sad':5, 'grumpy':6, 'mad':9}
    inv_dico = {v:k for k,v in dico.items()}
    
    mood = inv_dico[max(dico['happy'], dico['sad'])]
    
    print("you are : ", mood)
    # you are : sad
    

    注意:你可以使用 min 而不是 max 来产生相反的效果

    【讨论】:

    • 比您的“最佳方式”更好的方式Asocia's answer。 :-)
    • 我没有说我的方法是最好的,我说最好的方法是创建一个字典
    • 我其实很喜欢这个答案,因为它可以轻松修改以涵盖更多情况。试着把它改成这样:mood = inv_dico[max(inv_dico)] 它会给你输出:“你疯了”,这很酷:)
    • 非常感谢,但我知道我疯了 ;-)
    • “我说最好的方法是创建一个字典”——你确定你能识别出最好的方法吗?
    【解决方案3】:

    您也可以使用index()。然而,字典版本对于典型的键值情况来说更好更干净。

    list1 = ['happy', 'sad', 'grumpy', 'mad']
    list2 = [2, 5, 6, 9]
    
    idx = list1.index
    
    if list2[idx('happy')] < list2[idx('sad')]:
        print('sad')
    else:
        print('happy')
    

    【讨论】:

      【解决方案4】:

      您可以使用标准的enum 模块来完成它:

      from enum import IntEnum
      
      class Mood(IntEnum):
          happy = 2
          sad = 5
          grumpy = 6
          mad = 9
      
      if Mood.happy > Mood.sad:         # Nonsense; do you mean "if yourmood < Mood.sad:"?
          print ("you are happy")
      

      【讨论】:

        【解决方案5】:

        如果不想使用字典,可以使用exec函数。

        list1 = ['happy', 'sad', 'grumpy', 'mad']
        list2 = [2, 5, 6, 9]
        
        for L,i in zip(list1,list2):
          exec('{}={}'.format(L,i))
        
        if happy > sad:
          print ("you are happy")
        

        请注意,您必须使用字符串来命名变量(不能有未分配变量的列表)。

        【讨论】:

          【解决方案6】:

          你可以只使用简单的 dict 进行数据映射

          emotions = {'happy': 2, 'sad': 5,  'grumpy': 6, 'mad': 9}
          
          
          if emotions['happy'] > emotions['sad']:
              print("you are happy")
          else:
              print("you are sad")
          
          

          【讨论】:

            【解决方案7】:

            您可以在 python 中使用字典。一种表示键值对的简单方法。 Ex: did = {'jack': 4098, 'sape': 4139} 点击了解更多how to use dictionary

            【讨论】:

              猜你喜欢
              • 2012-07-28
              • 1970-01-01
              • 1970-01-01
              • 2016-10-25
              • 1970-01-01
              • 2021-02-19
              • 2011-07-20
              • 1970-01-01
              • 2016-05-14
              相关资源
              最近更新 更多