【问题标题】:Python 3: lists and dictionary. How can I create a dictionary that tells me what list each item is from?Python 3:列表和字典。如何创建一个字典来告诉我每个项目来自哪个列表?
【发布时间】:2020-02-06 07:35:10
【问题描述】:

鉴于这两个列表:

lst_1=['Apple', 'pie', 'is', 'the', 'most', 'delicious'] #list 1
lst_2=['It', 'is', 'Americas', 'best'] #list 2

如何将它们放入字典中,以便它告诉我它来自哪个列表(1 或 2),如下所示:

d['Apple'] = [1] #"Apple "is in list 1
d['pie']=[1]     #"pie" is in list 1
d['is']=[1,2] #"is" is in list 1 and 2
d['It']=[2]   # "It" is in list 2

【问题讨论】:

    标签: python python-3.x list function dictionary


    【解决方案1】:
    lst_1=['Apple', 'pie', 'is', 'the', 'most', 'delicious'] #list 1
    lst_2=['It', 'is', 'Americas', 'best'] #list 2
    
    d = {}
    
    for val in lst_1:
        d[val] = [1]
    
    for val in lst_2:
        if val not in d:
            d[val] = [2]
        else:
            d[val] = [1,2]
    
    
    print(d)
    

    【讨论】:

      【解决方案2】:
      lst_1=['Apple', 'pie', 'is', 'the', 'most', 'delicious'] #list 1
      lst_2=['It', 'is', 'Americas', 'best'] #list 2
      
      d = {}
      for index, i in enumerate([lst_1, lst_2]):
          for j in i:
              d.setdefault(j, set([]))
              d[j].add(index+1)
      
      print(d)
      

      【讨论】:

        【解决方案3】:

        使用defaultdict

        from collections  import defaultdict
        lst_1=['Apple', 'pie', 'is', 'the', 'most', 'delicious'] #list 1
        lst_2=['It', 'is', 'Americas', 'best'] #list 2
        d = defaultdict(list)
        for i in lst_1:
            d[i].append(1)
        for i in lst_2:
            d[i].append(2)
        

        dict(d)给了

        {'Apple': [1], 'pie': [1], 'is': [1, 2], 'the': [1], 'most': [1], 'delicious': [1], 'It': [2], 'Americas': [2], 'best': [2]}
        

        【讨论】:

          【解决方案4】:

          这个呢:

          myDictionary= {
            "list1" : {
              "1" : "Apple",
              "2" : "pie",
              "3" : "is",
              "4" : "most",
              "5" : "delicious."
            },
            "list2" : {
              "1" : "It",
              "2" : "is",
              "3" : "America's",
              "4" : "best,"
            }
          }
          

          我希望它有效。

          【讨论】:

            【解决方案5】:

            您可以使用 dict 推导来形成 dict 对象:

            d={i : [1] for i in lst_1}
            d.update({i: d.get(i, []) + [2] for i in lst_2})
            

            【讨论】:

              【解决方案6】:

              你可以这样做:

              lst_1=['Apple', 'pie', 'is', 'the', 'most', 'delicious']
              lst_2=['It', 'is', 'Americas', 'best']
              
              dic={}
              joined_list= lst_1 + lst_2
              for e in joined_list:
                  if e in lst_1 and e in lst_2:
                      dic[e] = [1, 2]
                  elif e in lst_1:
                      dic[e] = [1]
                  elif e in lst_2:
                      dic[e] = [2]
              print(dic)
              

              【讨论】:

                【解决方案7】:

                您可以尝试以下方法:

                d= {}
                lst = set(lst_1 + lst_2)
                for i in lst:
                    if i in lst_1 and i in lst_2:
                        d[i] = [1,2]
                    elif i in lst_2:
                        d[i] = [2]
                    elif i in lst_1:
                        d[i] = [1]
                print(d)
                

                【讨论】:

                • @Lulabelle95 看看这个
                猜你喜欢
                • 1970-01-01
                • 2018-09-18
                • 1970-01-01
                • 2021-07-12
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-01-08
                相关资源
                最近更新 更多