Data Collection Zelle - Open Michigan
Data Collection Zelle - Open Michigan
Zelle - Chapter 11
Charles Severance - www.dr-chuck.com
$ python
Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53)
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
>>> x = 2
>>> x = 4
>>> print x
4
What is a Collection?
• A collection is nice because we can put more than one value in them
and carry them all around in one convenient package.
• Dictionary
>>> print sum(grades) Add up the values in the list using the sum()
297 function.
• grades[0]
http://docs.python.org/lib/built-in-funcs.html
>>>print Ist
[3,12,14,18,21,33]
>>>for xval in Ist:
… print xval
…
3
12
14 Looping through Lists
18
21
33
>>>
z-343
List
Operations
z-343
Quick Peek: Object Oriented
<nerd-alert>
What “is” a List Anyways?
• A list is a special kind of variable >>> i = 2
>>> i = i + 1
• Regular variables - integer >>> x = [1, 2, 3]
>>> print x
• Contain some data
[1, 2, 3]
• Smart variables - string, list >>> x.reverse()
>>> print x
• Contain some data and capabilities [3, 2, 1]
Python
Dictionaries
tissue
calculator
perfume
money
candy
http://en.wikipedia.org/wiki/Associative_array
Dictionaries
• Dictionaries are Python’s most powerful data collection
z-369
Dictionary Literals (Constants)
• Dictionary literals use curly braces and have a list of key : value pairs
ccc[“csev”] = 20
if “csev” in ccc:
print “Yes”
else
print “No”
Dictionary
Counting >>> ccc = dict()
>>> print ccc.get("csev", 0)
0
• Since it is an error to >>> ccc["csev"] = ccc.get("csev",0) + 1
reference a key which is not >>> print ccc
in the dictionary {'csev': 1}
>>> print ccc.get("csev", 0)
• We can use the dictionary 1
>>> ccc["csev"] = ccc.get("csev",0) + 1
get() operation and supply a
default value if the key does >>> print ccc
{'csev': 2}
not exist to avoid the error
and get our count started.
dict.get(key, defaultvalue)
What get() effectively does...
• The get() method basically d = dict()
does an implicit if checking x = d.get(“fred”,0)
to see if the key exists in the
dictionary and if the key is
not there - return the
default value d = dict()
if “fred” in d:
• The main purpose of get() is x = d[“fred”]
to save typing this four line else:
pattern over and over x=0
Retrieving lists of Keys and Values
• You can get a list of keys, values or items (both) from a dictionary
• However hashing makes it so that dictionaries are not sorted and they
are not sortable
• Lists and sequences maintain their order and a list can be sorted - but
not a dictionary
http://en.wikipedia.org/wiki/Hash_function
Dictionaries are not Ordered
>>> lst = dict()
>>> dict = { "a" : 123, "b" : 400, "c" : 50 } >>> lst.append("one")
>>> print dict >>> lst.append("and")
{'a': 123, 'c': 50, 'b': 400} >>> lst.append("two")
>>> print lst
['one', 'and', 'two']
>>> lst.sort()
Dictionaries have no order and >>> print lst
cannot be sorted. Lists have ['and', 'one', 'two']
order and can be sorted. >>>
http://en.wikipedia.org/wiki/Hash_function
Summary: Two Collections
• List
• Dictionary