【问题标题】:python create dict using list of strings with length of strings as valuespython使用以字符串长度为值的字符串列表创建dict
【发布时间】:2017-03-16 02:08:41
【问题描述】:

我确信这是可以做到的,但到目前为止我还没有成功:

我有一个字符串列表。我想创建一个字典,其中所述字符串的长度(可以表示为范围)作为键,字符串本身作为值。

示例: 这是我的清单:['foo','bar','help','this','guy']

我想最终得到这样的字典: {3:['foo','bar','guy], 4:['this','help']}

【问题讨论】:

标签: python list dictionary


【解决方案1】:

使用defaultdict,这样您就不必检查是否为新密钥创建列表:

from collections import defaultdict

x = ['foo','bar','help','this','guy']

len_dict = defaultdict(list)

for word in x:
    len_dict[len(word)].append(word)

len_dict
#
# Out[5]: defaultdict(list, {3: ['foo', 'bar', 'guy'], 4: ['help', 'this']})

【讨论】:

    【解决方案2】:

    您可以将字典用作setdefault 的容器:

    lst = ['foo','bar','help','this','guy'] 
    
    result = {}   
    for w in lst:
        result.setdefault(len(w), []).append(w)
    
    result
    # {3: ['foo', 'bar', 'guy'], 4: ['help', 'this']}
    

    【讨论】:

      【解决方案3】:

      你可以这样做:

      d={}
      lst=['foo','bar','help','this','guy']
      for i in lst:
          if len(i) in d:
              d[len(i)].append(i)
          else:
              d[len(i)]=[i]
      

      【讨论】:

        【解决方案4】:

        这个解决方案是pythonic、优雅和快速的:(由著名的Raymond Hettinger 在他的众多conferences 之一中)。

        dict.setdefault 是字典方法,如果在 dict 中找不到键,则初始化键值,并为 提供的键执行dict.get

        l = ['foo','bar','help','this','guy']
        d = {}
        for e in l:
            key = len(e)
            d.setdefault(key, []).append(name)
        print(d)
        

        输出:

        {3: ['foo', 'bar', 'guy'], 4: ['help', 'this']}
        

        此解决方案是上述解决方案的现代方式:

        来自collectiondefaultdictdict 的子类,它自动将值初始化为不在defaultdict 中的任何给定键。

        from collections import defaultdict
        l = ['foo','bar','help','this','guy']
        d = defaultdict(list)
        for e in l:
            key = len(e)
            d[key].append(e)
        print(d)
        

        输出:

        defaultdict(<class 'list'>, {3: ['foo', 'bar', 'guy'], 4: ['help', 'this']})
        

        【讨论】:

          【解决方案5】:

          与上面所说的类似,但是使用dict类的get方法:

          the_list=['foo','bar','help','this','guy']
          d = {}
          for word in the_list:
            key = len(word)
            d[key] = d.get(key, []) + [word]
          print(d)
          # {3: ['foo', 'bar', 'guy'], 4: ['help', 'this']}
          

          【讨论】:

            【解决方案6】:

            另一种方法:

            from collections import defaultdict
            given_list=['foo','bar','help','this','guy']
            len_words=[len(i) for i in given_list]
            d=defaultdict(list)
            for i,j in list(zip(len_words,given_list)):
                d[i].append(j)
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-06-25
              • 1970-01-01
              • 2018-07-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多