Lecture 9 - ECE 364 Software Tools Laboratory Lecture 9...

Info icon This preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
3/27/11 1 ECE 364: Software Tools Laboratory Lecture 9 March 28, 2011 Lecturer: Michael Goldfarb 1 Lecture 9 §੿ Dictionaries §੿ Classes §੿ Lecture examples can be found in §੿ ~ee364/examples/LEC-09 2
Image of page 1

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

View Full Document Right Arrow Icon
3/27/11 2 Dictionaries §੿ A Python dictionary is an associative container §੿ A key is associated with a value §੿ Unlike lists, dictionaries are NOT ordered §੿ A key can be any Python type §੿ Integers, strings, tuples, lists, etc. §੿ Not limited to built-in types §੿ A value can also be any Python type §੿ Just like lists and tuples 3 Creating a Dictionary §੿ Dictionaries use {} and have comma separated values consisting of key:value pairs §੿ Example A = { "Foobar" : 100, 2 : “Big Deal”, (1, 2, 34) : [[1, 2], ”Baz”]} 4
Image of page 2
3/27/11 3 Accessing Items §੿ Dictionary values are accessed using square brackets and indexing with a key A[Key] # Gets the value associated with Key §੿ If a key:value pair is not present a KeyError exception is raised A[Key] # Raises KeyError if not found 5 Adding Items §੿ Add a value to a dictionary: A[Key] = Value §੿ Merge two dictionaries: >>> A = {1:20, -5:7, 8.2:31} >>> B = {1:'foobar', 9:0} >>> A.update(B) >>> print A {1: 'foobar', -5: 7, 9: 0, 8.2: 31} 6
Image of page 3

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

View Full Document Right Arrow Icon
3/27/11 4 Removing Items §੿ Delete a value from a dictionary: del A[Key] §੿ Pop a value from a dictionary A.pop(Key) §੿ Pop a key/value pair from a dictionary key, val = A.popitem() 7 Checking for Items §੿ The following methods are equivalent: Key in A A.has_key(Key) §੿ Each returns True if A contains the key Key §੿ To negate the test: Key not in A not A.has_key(Key) 8
Image of page 4
3/27/11 5 Keys §੿ To obtain a list of the keys in a dictionary, the built-in method keys() may be used: >>> A = { "Foobar" : 100, ... 2 : "Big Deal", ... (1, 2, 34) : [[1, 2], "Yuk"]} >>> print A.keys() [(1, 2, 34), 2, 'Foobar'] 9 Values §੿ To obtain a list of the values in a dictionary, the built-in method values() may be used: >>> A = { "Foobar" : 100, ... 2 : "Big Deal", ... (1, 2, 34) : [[1, 2], "Yuk"]} >>> print A.values() [[[1, 2], 'Yuk'], 100, 'Big Deal'] 10
Image of page 5

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

View Full Document Right Arrow Icon
3/27/11 6 Items §੿ A list of key:value pairs can be obtained using the items() member function §੿ Returns a list of tuples of the form (key, value) §੿ Example >>> print A.items() [((1, 2, 34), [[1, 2], ’Baz’]), ('Foobar', 100), (2, 'Big Deal')] 11 Iterating §੿ In a for loop, a dictionary returns its keys: for key in Dictionary: ...
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern