【问题标题】:Adding a single character to add keys in Counter添加单个字符以在 Counter 中添加键
【发布时间】:2017-08-27 22:06:15
【问题描述】:

如果 Counter 对象的键类型为str,即:

我可以这样做:

>>> vocab_counter = Counter("the lazy fox jumps over the brown dog".split())

>>> vocab_counter  = Counter({k+u"\uE000":v for k,v in vocab_counter.items()})
>>> vocab_counter
Counter({'brown\ue000': 1,
         'dog\ue000': 1,
         'fox\ue000': 1,
         'jumps\ue000': 1,
         'lazy\ue000': 1,
         'over\ue000': 1,
         'the\ue000': 2})

将字符添加到所有键的快速和/或 Python 方法是什么?

上述方法是实现将字符附加到所有键的最终计数器的唯一方法吗?是否有其他方法可以实现相同的目标?

【问题讨论】:

  • 我认为您的做法似乎还不错...您期待什么样的改进?用的时间?代码行数?
  • 不是几行代码,就是想知道有没有更好的办法。多次遍历所有键并不是最优的。也许我必须将其转储到数据帧或 numpy 数组中,然后将其读回计数器。
  • 我不认为这会更好......那样会涉及创建拆分字符串列表,操作其中的每一个,然后计算它们。它目前的下降方式只涉及每个独特的单词 - 所以如果计算一本小说,它会更加干净和高效(无论如何我认为)我要做的唯一改变是在你的最后一步创建字典而不是计数器,因为您不再需要计算(我假设)
  • 嗨@alvas 不知道多次,但您必须至少迭代一次才能更新。检查我的答案。它可能会对你有所帮助。

标签: python string dictionary counter key-value


【解决方案1】:

更好的方法是在创建计数器对象之前添加该字符。您可以使用Counter 中的生成器表达式来做到这一点:

In [15]: vocab_counter = Counter(w + u"\uE000" for w in "the lazy fox jumps over the brown dog".split())

In [16]: vocab_counter
Out[16]: Counter({'the\ue000': 2, 'fox\ue000': 1, 'dog\ue000': 1, 'jumps\ue000': 1, 'lazy\ue000': 1, 'over\ue000': 1, 'brown\ue000': 1})

如果在创建计数器之前无法修改单词,您可以覆盖Counter 对象以添加特殊字符during setting the values for keys

【讨论】:

    【解决方案2】:

    我用的最短的方法是,

    vocab_counter = Counter("the lazy fox jumps over the brown dog".split()) 
    for key in vocab_counter.keys():
      vocab_counter[key+u"\uE000"] = vocab_counter.pop(key)
    

    【讨论】:

    • '\ue000' 应该在每个新键的末尾,而不是前面。
    • 啊!你可以认为这是错字:P
    • 现在已经解决了,我认为这是这里唯一没有错误的答案。我不认为这种方法比问题中的方法更好,但至少不会更糟。
    【解决方案3】:

    我能想到的唯一其他优化方法是使用Counter 的子类,在插入键时附加字符:

    from collections import Counter
    
    
    class CustomCounter(Counter):
        def __setitem__(self, key, value):
            if len(key) > 1 and not key.endswith(u"\uE000"):
                key += u"\uE000"
            super(CustomCounter, self).__setitem__(key, self.get(key, 0) + value)
    

    演示:

    >>> CustomCounter("the lazy fox jumps over the brown dog".split())
    CustomCounter({u'the\ue000': 2, u'fox\ue000': 1, u'brown\ue000': 1, u'jumps\ue000': 1, u'dog\ue000': 1, u'over\ue000': 1, u'lazy\ue000': 1})
    # With both args and kwargs 
    >>> CustomCounter("the lazy fox jumps over the brown dog".split(), **{'the': 1, 'fox': 3})
    CustomCounter({u'fox\ue000': 4, u'the\ue000': 3, u'brown\ue000': 1, u'jumps\ue000': 1, u'dog\ue000': 1, u'over\ue000': 1, u'lazy\ue000': 1})
    

    【讨论】:

    • 演示中的 CustomCounter 包含条目 u'the\ue000': 1,其值应为 2,因此此解决方案似乎存在错误。
    • @Felix 哎呀!固定。
    【解决方案4】:

    你可以通过字符串操作来做到这一点:

    text = 'the lazy fox jumps over the brown dog'
    Counter((text + ' ').replace(' ', '_abc ').strip().split())
    

    【讨论】:

      猜你喜欢
      • 2020-03-21
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 2016-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多