lecture-11 - Key data structure Dictionaries Associative...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Key data structure: Dictionaries Associative arrays, Hash tables … A table storing a set of “keys”, And a “value” for each key. Any (immutable) object can be a key! • int,float,string,tuples… Very useful! Using Dictionaries Unsorted list of key,value pairs Empty Dictionary: {} Non-empty Dictionary: {k1:v1,k2:v2,…} Membership: is k in dict: k in d Lookup value of key: d[k] Set value of key: d[k]=v Dictionaries >>> d={} >>> d=dict(mexmenu) >>> d[“ceviche”] = 3.95 >>> d {…} >>> d[“burrito”] 3.50 >>> d.keys() >>> d.values() Dictionaries def freq(s): d={} for c in s: if c in d: d[c]+=1 else: d[c]=1 return d >>> d=plotfreq([1,1,3.0,”A”,3.0,”A”,”A”,1,2,3.0,1,”A”]) >>> d >>> d = plotfreq(“avrakedavra”) >>> d.keys() >>> d def plotfreq(s): d=freq(s) for k in d.keys(): print k, “*”*d[k] Next: What’s in a name ? More precisely: • How should programmer think of data • What does a variable “x” really mean ?
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
What’s in a name ? ML (or Functional Languages) • Name refers to a Value • Binding maps Names to Values • Environment list of bindings • Environment can be extended • Environment can’t be changed Data model in functional PL • Vars = names in phonebook • Evaluation = Most recent • Environment “frozen” in function value – behavior of function cannot be changed – easier reasoning Data model in OO langs • Variables “point to” objects • Objects = boxes with data inside X “pumpkin” Y 3.142 Z [1,2,3] Namespaces • Manage variable names in Python • Similar to, but different from Environments – Core PL concept, unifies many ideas • We will see very important differences Ok, but what IS a namespace ? A mapping from names to objects X “pumpkin” Y 3.142 Z [1,2,3] Namespaces vs. Environments Both are maps from variables to something Namespace Environment What’s the difference ? 1. Assignment
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

lecture-11 - Key data structure Dictionaries Associative...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online