Sagebrush · Examples · Notebooks

Text & data

Regular expressions, JSON, counting and sorting words.

notebook · Python · intro · by Sagebrush · MIT license

dataprogramming

Download (ZIP)Source on GitHub

Preview

import re, json
text = 'Euler 1707, Gauss 1777, Riemann 1826, Noether 1882'
people = {name: int(year) for name, year in re.findall(r'(\w+) (\d{4})', text)}
print(json.dumps(people, indent=2))
{
  "Euler": 1707,
  "Gauss": 1777,
  "Riemann": 1826,
  "Noether": 1882
}
from collections import Counter
Counter('the quick brown fox jumps over the lazy dog'.replace(' ', '')).most_common(5)
[('o', 4), ('e', 3), ('t', 2), ('h', 2), ('u', 2)]
words = 'sagebrush grows in the high desert'.split()
sorted(words, key=lambda w: (-len(w), w))
['sagebrush', 'desert', 'grows', 'high', 'the', 'in']

Files

Open the ZIP's notebook in Sagebrush (File, Open) to run and change it.