<jupyter_start><jupyter_text>First day:  List comprehensions and generators  List comprehensions<jupyter_code>from collections import Counter
import calendar
import itertools
import random
import re
import string

import requests
names = 'pybites <NAME> <NAME>'.split()
names
for name in names:
    print(name.title())
first_half_alphabet = list(string.ascii_lowercase)[:13]
first_half_alphabet
new_names = []
for name in names:
    if name[0] in first_half_alphabet:
        new_names.append(name.title())
        
new_names
new_names2 = [n.title() for n in names if n[0] in first_half_alphabet]
assert new_names == new_names2
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36', "Upgrade-Insecure-Requests": "1","DNT": "1","Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Language": "en-US,en;q=0.5","Accept-Encoding": "gzip, deflate"}
resp = requests.get('https://projects.bobbelderbos.com/pcc/harry.txt', headers=headers)
words = resp.text.lower().split()
words[:5]
counter = Counter(words)
counter.most_common(5)
'-' in words
words = [re.sub(r'\W+', r'', word) for word in words]
words[:5]
'-' in words
'the' in words
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36', "Upgrade-Insecure-Requests": "1","DNT": "1","Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Language": "en-US,en;q=0.5","Accept-Encoding": "gzip, deflate"}
resp = requests.get('https://projects.bobbelderbos.com/pcc/stopwords.txt', headers=headers)
stopwords = resp.text.lower().split()
stopwords[:5]
len(stopwords)
words = [word for word in words if word.strip() and word not in stopwords]
'the' in words
counter = Counter(words)
counter.most_common(5)<jupyter_output><empty_output><jupyter_text>Generators<jupyter_code>def num_gen():
    for i in range(5):
        yield i
gen = num_gen()
next(gen)
for i in gen:
    print(i)
next(gen)
options = 'red yellow blue white black green purple'.split()
options
def create_select_options(options=options):
    select_list = []
    
    for option in options:
        select_list.append(f"<option value={option}>{option.title()}</option>")
        
    return select_list
from pprint import pprint as pp
pp(create_select_options())
def create_select_options_gen(options=options):
    for option in options:
        yield f"<option value={option}>{option.title()}</option>"
create_select_options_gen()
list(create_select_options_gen())
# Performance of list vs generator

# list
def leap_years_lst(n=1000000):
    leap_years = []
    for year in range(1, n+1):
        if calendar.isleap(year):
            leap_years.append(year)
    return leap_years

# generator
def leap_years_gen(n=1000000):
    for year in range(1, n+1):
        if calendar.isleap(year):
            yield year
%timeit -n1 leap_years_lst()
%timeit -n1 leap_years_gen()<jupyter_output>The slowest run took 4.98 times longer than the fastest. This could mean that an intermediate result is being cached.
2.03 µs ± 1.66 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
{'fraction_non_alphanumeric': 0.14401749453295845, 'fraction_numerical': 0.03405185879412684, 'mean_word_length': 4.7797833935018055, 'pattern_counts': {'":': 10, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '346974', 'license': 'MIT', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1112, 'n_words': 278, 'path': 'talkpython/100-days-of-code-in-python/my_progress/day_016_list_comprehensions_and_generators.ipynb', 'repo_name': 'djrgit/coursework', 'size': '3218'}