how to ignore case while doing most_common in python's collections.counter?

  • Last Update :
  • Techknowledgy :

How about mapping it to str.lower?

>>> Counter(map(str.lower, names)).most_common(3)[('juicy', 2), ('aish', 2), ('ash', 2)]

Suggestion : 2

How to ignore case while doing most_common in Python's collections.Counter?

>>> Counter(map(str.lower, names)).most_common(3)[('juicy', 2), ('aish', 2), ('ash', 2)]

Suggestion : 3

The Counter class itself is a dictionary subclass with no restrictions on its keys and values. The values are intended to be numbers representing counts, but you could store anything in the value field.,dict subclass that calls a factory function to supply missing values,Counter objects support additional methods beyond those available for all dictionaries:,A real dictionary used to store the contents of the UserDict class.

>>> baseline = {
      'music': 'bach',
      'art': 'rembrandt'
   } >>>
   adjustments = {
      'art': 'van gogh',
      'opera': 'carmen'
   } >>>
   list(ChainMap(adjustments, baseline))['music', 'art', 'opera']
>>> combined = baseline.copy() >>>
   combined.update(adjustments) >>>
   list(combined)['music', 'art', 'opera']
import builtins
pylookup = ChainMap(locals(), globals(), vars(builtins))
import os, argparse

defaults = {
   'color': 'red',
   'user': 'guest'
}

parser = argparse.ArgumentParser()
parser.add_argument('-u', '--user')
parser.add_argument('-c', '--color')
namespace = parser.parse_args()
command_line_args = {
   k: v
   for k,
   v in vars(namespace).items() if v is not None
}

combined = ChainMap(command_line_args, os.environ, defaults)
print(combined['color'])
print(combined['user'])
c = ChainMap() # Create root context
d = c.new_child() # Create nested child context
e = c.new_child() # Child of c, independent from d
e.maps[0] # Current context dictionary--like Python 's locals()
e.maps[-1] # Root context--like Python 's globals()
e.parents # Enclosing context chain--like Python 's nonlocals

d['x'] = 1 # Set value in current context
d['x'] # Get first key in the chain of contexts
del d['x'] # Delete from current context
list(d) # All nested values
k in d # Check all nested values
len(d) # Number of nested values
d.items() # All nested items
dict(d) # Flatten into a regular dictionary
class DeepChainMap(ChainMap):
   'Variant of ChainMap that allows direct updates to inner scopes'

def __setitem__(self, key, value):
   for mapping in self.maps:
   if key in mapping:
   mapping[key] = value
return
self.maps[0][key] = value

def __delitem__(self, key):
   for mapping in self.maps:
   if key in mapping:
   del mapping[key]
return
raise KeyError(key)

   >>>
   d = DeepChainMap({
      'zebra': 'black'
   }, {
      'elephant': 'blue'
   }, {
      'lion': 'yellow'
   }) >>>
   d['lion'] = 'orange'
# update an existing key two levels down
   >>>
   d['snake'] = 'red'
# new keys get added to the topmost dict
   >>>
   del d['elephant'] # remove an existing key one level down >>>
   d # display result
DeepChainMap({
   'zebra': 'black',
   'snake': 'red'
}, {}, {
   'lion': 'orange'
})