{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture-11

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

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

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 ?

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

View Full Document
2 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 ?
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online