<commit_before>from collections import defaultdict

# Example 1: using int as parameter.
mississippi = 'mississippi'
d = defaultdict(int)
for key in mississippi:
    d[key] += 1
d.items()

# Example 2: Using list as parameter.
colors = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
d = defaultdict(list)
for key, value in colors:
    d[key].append(value)
d.items()

# defaultdict means that if an key is not found in the dictionary that instead of a KeyError being thrown a new value is created.
# The type of this new pair is given by the argument of defaultdict.

# somedict = {}
# print(somedict[3]) # KeyError

someddict = defaultdict(int)
print(someddict[3]) # print int(), thus 0

# The standard dictionary includes the method setdefault() for retrieving a value and establishing a default if the value does not exist.
# By contrast, defaultdict lets the caller specify the default(value to be returned) up front when the container is initialized.

# Callable as its first argument(mandatory)
d_int = defaultdict(int)
d_list = defaultdict(list)
def foo():
    return 'default value'
d_foo = defaultdict(foo)
d_int
d_list
d_foo

# **kwargs as its second argument(optional)
d_int = defaultdict(int, a=10, b=12, c=13)
d_int

kwargs = {'a':10,'b':12,'c':13}
d_int = defaultdict(int, **kwargs)
d_int

# How does it works
# As is a child class of standard dictionary, it can perform all the same functions.
# But in case of passing an unknown key it returns the default value instead of error. For ex:

d_int['a']
d_int['d']
d_int

# In case you want to change default value overwrite default_factory:
d_int.default_factory = lambda: 1
d_int['e']
d_int

def foo():
    return 2
d_int.default_factory = foo
d_int['f']
d_int

from collections import defaultdict
ice_cream = defaultdict(lambda: 'Vanilla')
ice_cream = defaultdict(lambda: 'Vanilla')
ice_cream['Sarah'] = 'Chunky Monkey'
ice_cream['Abdul'] = 'Butter Pecan'
print ice_cream['Sarah']
print ice_cream['Joe']<commit_msg>Adding fix for collectiondict with lambda initialization<commit_after>from collections import defaultdict

# Example 1: using int as parameter.
mississippi = 'mississippi'
d = defaultdict(int)
for key in mississippi:
    d[key] += 1
print 'Example 1:'
print d.items()
print

# Example 2: Using list as parameter.
colors = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
d = defaultdict(list)
for key, value in colors:
    d[key].append(value)
print 'Example 2:'
print d.items()
print

# defaultdict means that if an key is not found in the dictionary that instead of a KeyError being thrown a new value is created.
# The type of this new pair is given by the argument of defaultdict.
someddict = defaultdict(int)
print(someddict[3]) # print int(), thus 0
print

# The standard dictionary includes the method setdefault() for retrieving a value and establishing a default if the value does not exist.
# By contrast, defaultdict lets the caller specify the default(value to be returned) up front when the container is initialized.
# Notes:
# Callable as its first argument(mandatory)
def func():
    return 'default value'

dic_int = defaultdict(int)
dic_list = defaultdict(list)
dic_foo = defaultdict(func)
print dic_int
print dic_list
print dic_foo
print

# Initialize dictionary with ints, second argument is optional.
dic_int = defaultdict(int, a=10, b=12, c=13)
print dic_int
print

# Initialize dictionary with int, and **kwargs as its second argument(optional)
kwargs = {'a':10,'b':12,'c':13}
dic_int = defaultdict(int, **kwargs)
print dic_int
print

# How does it works
# As is a child class of standard dictionary, it can perform all the same functions.
# But in case of passing an unknown key it returns the default value instead of error. For ex:
print dic_int['a']
print dic_int['d']
print dic_int
print

# In case you want to change default value overwrite default_factory:
dic_int.default_factory = lambda: 1
dic_int['e']
dic_int

def foo():
    return 2

dic_int.default_factory = foo
dic_int['f']
dic_int

# Another example, lambda with default value as string.
ice_cream = defaultdict(lambda: 'Vanilla')
ice_cream['Sarah'] = 'Chunky Monkey'
ice_cream['Abdul'] = 'Butter Pecan'
print ice_cream['Sarah']
print ice_cream['Joe']
{'fraction_non_alphanumeric': 0.10302745834311194, 'fraction_numerical': 0.011969021356489086, 'mean_word_length': 5.079885877318117, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1658586', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1328, 'n_words': 546}