【问题标题】:Create a dictionary from two lists in python matching the number of letters in each word [duplicate]从python中的两个列表创建一个字典,匹配每个单词中的字母数[重复]
【发布时间】:2016-07-01 23:07:45
【问题描述】:

我有两个列表

a= ['and', 'people', 'them', 'become', 'treat', 'is', 'they', 'see', 'the', 'way', 'you', 'what']
b= [3, 6, 4, 6, 5, 2, 4, 3, 3, 3, 3, 4]

我需要一个字典,其中键是b 中的数字,值是a 中的单词。但是,键的值必须是唯一的。所以,输出是这样的:

c= {2 : 'is', 3 : ['and', 'see', 'the', 'way', 'you'], 4: ['them', 'they', 'what'], 5 : 'treat', 6 : 'become'}

我试过了:

mydict = {key:value for key, value in zip(b, a)}
print (mydict)

这是输出:

{2: 'is', 3: 'and', 4: 'them', 5: 'treat', 6: 'become'}

【问题讨论】:

  • 总是使用列表作为值不是更好吗?然后你可以使用mydict.setdefault(key, []).append(value)collections.defaultdict(list)。否则你会遇到三种情况:键不存在,键存在字符串值,键存在列表值。
  • 你需要什么输出?
  • 我需要这个输出: c= {2 : 'is', 3 : ['and', 'see', 'the', 'way', 'you'], 4: ['他们','他们','什么'],5:'治疗',6:'成为'}
  • @DanielTiezzi,为什么不5: ['treat']
  • 因此,在分配给匹配键的值列表中具有相同长度的单词

标签: python dictionary


【解决方案1】:
dict={}
for i in xrange(len(b)):
    if b[i] not in dict:
        dict[b[i]]=[a[i]]
    else:
        dict[b[i]].append(a[i])
print dict

你会得到你需要的结果。

{2: ['is'], 3: ['and', 'see', 'the', 'way', 'you'], 4: ['them', 'they', 'what'],5: ['treat'], 6: ['people', 'become']}

【讨论】:

    【解决方案2】:

    您可以使用defaultdict 类,它的行为类似于dict,但可以为缺少的键设置默认起始值​​。试试这样:

        >>> a= ['and', 'people', 'them', 'become', 'treat', 'is', 'they', 'see', 'the', 'way', 'you', 'what']
        >>> from collections import defaultdict
        >>> c=defaultdict(list)
        >>> for x in a: c[len(x)].append(x)
        >>> print(c)
        defaultdict(<class 'list'>, {2: ['is'], 3: ['and', 'see', 'the', 'way', 'you'], 4: ['them', 'they', 'what'], 5: ['treat'], 6: ['people', 'become']})
    

    那你可以像往常一样使用它dict:

        >>> for k,v in c.items():
        >>>        print(k, ' : ', v)
    
        2  :  ['is']
        3  :  ['and', 'see', 'the', 'way', 'you']
        4  :  ['them', 'they', 'what']
        5  :  ['treat']
        6  :  ['people', 'become']
    

    【讨论】:

      【解决方案3】:

      正如我在my comment 中指出的,这意味着要处理三种情况:

      1. 密钥尚不存在
      2. 存在键,值为字符串
      3. 键存在,值是列表

      因此,要精确地做你想做的事:

      mydict = {}
      for key, value in zip(map(len, a), a):
          # 1. 
          if key not in mydict: 
              mydict[key] = value
          # 2. 
          elif isinstance(mydict[key], str):
              mydict[key] = [mydict[key], value]
          # 3. 
          else:
              mydict[key].append(value)
      

      请注意使用maplen 来即时获取长度。

      但是,正如您所看到的,这是相当复杂的,现在您有一个具有异构值类型的字典,这进一步使额外的处理复杂化。相反,我建议始终使用列表作为值,即使只使用单个字符串:

      mydict = {}
      for key, value in zip(map(len, a), a):
          if key not in mydict:
              mydict[key] = []
          mydict[key].append(value)
      

      这显然更简洁,并且可能会简化您的任何其他步骤。您可以使用为此类事情设计的dict.setdefault 方法或collections.defaultdict 对象进一步简化此代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-11
        • 1970-01-01
        相关资源
        最近更新 更多