Built-in data structures¶
Learning objectives
- .
Built-in data structures¶
Problem¶
Are there classes that can help me solve problems more elegantly?
Python classes¶
list
: heterogeneous containertuple
: immutablelist
set
: setsdict
: dictionary- Regular expressions: text patterns
From Python 'Data Structures' documentation
set
¶
Sorted collection of unique elements.
- No need to check for elements existing twice
dict
¶
A dictionary:
periodic_table = dict({1: "Hydrogen", 2: "Helium", 3: "Lithium"})
periodic_table[2] = "helium"
assert periodic_table[2] == "helium"
- Commonly uses as a look-up table
- A look-up table can store the results of earlier calculations
Regular expressions¶
A state-machine for a pattern in text
import re
dna_regex = re.compile("^[ACGT]*$")
assert dna_regex.match("")
assert dna_regex.match("A")
assert dna_regex.match("CA")
assert dna_regex.match("GCA")
assert dna_regex.match("TGCA")
assert dna_regex.match("TGCAAAAAA")
assert not dna_regex.match("nonsense")