# map, list, stack, queue, set

Post on 30-Dec-2015

39 views

Category:

## Documents

Embed Size (px)

DESCRIPTION

Map, List, Stack, Queue, Set. Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by-nc-sa/3.0. Stack. Information found here: - PowerPoint PPT Presentation

TRANSCRIPT

• Map, List, Stack, Queue, Set Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides.Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by-nc-sa/3.0

*

StackInformation found here:http://docs.python.org/py3k/library/stdtypes.htmlReally easy! Use lists to simulate a stack.append(value) in order to push onto the stack.pop() in order to pop off the stack.Check this example out!

*

QueueReally easy too!!!!Use lists to simulate a queue.insert(0, value) in order to add to the queue..pop() in order to remove off from the queue.Why does this work? What do insert and pop really do?

*

QueueOk this is dumb and inefficient.Using lists, we get O(N), but..from collections import dequequeue = deque([blah, blah, blah])Use append() to add to the queueUse popleft() to remove from the queueThis works in O(1)! Sweeeeeeeeet.

*

DictionaryEquivalent to Javas Map. Stores keys and values.Empty dictionary is defined by:m = {}Pre-populated dictionary: (Map)(mapping names to SCII division)m2 = { jordan : master, roy : bronze, marty : bronze }Add to dictionary:m2[ IMMVP ] = masterRetrieve from dictionarym2[jordan] # masterExample!!

*

DictionaryHow to get keySet()map.keys()Uh oh. We get something called dict_keysWe should cast to a list.list(map.keys())How to check if a key is contained in a dictionarymarty in m # returns true bc marty is in dictionarybronze in m # returns false bc bronze is not a keyHow to deletedel m[marty]Get values with: m.values()Note that you get the dict_values. So cast to a list.Example!!

*

Sets = { GLaDOS, Cloud, Aeris, Shepard, Cloud, Aeris }# {GLaDOS, Cloud, Aeris, Shepard }Removed duplicatess2 = set(hello I am soooo happy!!!)Creates a set out of all the charactersThe stuff in the blue must be iterable.i.e. lists, tuples, etcCheck for contains is same as dictionary.GLaDOS in s

*

SetTons of extra functionalityCheck for subsets, see if set A is contained in set BGo here to see more: (under 5.7)http://docs.python.org/py3k/library/stdtypes.html#set.issubset

*

List Comprehensions [expression for element in list]

Applies the expression to each element in the listYou can have 0 or more for or if statementsIf the expression evaluates to a tuple it must be in parenthesis

*

List Comprehensions

1234567891011121314>>> vec = [2, 4, 6]>>> [3*x for x in vec] [6, 12, 18]

>>> [3*x for x in vec if x > 3] [12, 18]

>>> [3*x for x in vec if x < 2] []

>>> [[x,x**2] for x in range(10)] [[0, 0], [1, 1], [2, 4], [3, 9]]

>>> [x, x**2 for x in vec] # error - parens required for tuples

*

List ComprehensionsYou can do most things that you can do with map, filter and reduce more nicely with list comprehensions

How can we find out how many times a appears in the list named email?

123456>>> email = ['once', 'upon', 'a', 'time', 'in', 'a', 'far', 'away']

>>> len( [1 for x in email if x == 'a'] )

>>> 2

*

Example of MapISA hierarchy

*>>> a = []>>> a.append(1)>>> a.append(2)>>> a.append(3)>>> a.append(5)>>> a[1, 2, 3, 5]>>> a.pop()5>>> a[1, 2, 3]>>> a.pop()3>>> a[1, 2]*>>> a = []>>> a.insert(0,2)>>> a.insert(0,3)>>> a.insert(0,4)>>> a[4, 3, 2]>>> a.pop()2>>> a[4, 3]>>> a.insert(0, a.pop())>>> a*>>> from collections import deque>>> my_list = ["Jordan", "Zeratul", "Kerrigan", "Raynor", "Artosis"]>>> my_list['Jordan', 'Zeratul', 'Kerrigan', 'Raynor', 'Artosis']>>> q = deque(my_list)>>> qdeque(['Jordan', 'Zeratul', 'Kerrigan', 'Raynor', 'Artosis'])>>> q.append("Tasteless")>>> qdeque(['Jordan', 'Zeratul', 'Kerrigan', 'Raynor', 'Artosis', 'Tasteless'])>>> q.append("IMMVP" )>>> qdeque(['Jordan', 'Zeratul', 'Kerrigan', 'Raynor', 'Artosis', 'Tasteless', 'IMMVP'])>>> q.popleft()'Jordan'>>> q.popleft()'Zeratul'**>>> m{'hi': 3, 'hello': 32}>>> m = {'jordan' : 'master', 'roy' : 'bronze', 'marty' : 'bronze' }>>> m{'marty': 'bronze', 'roy': 'bronze', 'jordan': 'master'}>>> m.keys()dict_keys(['marty', 'roy', 'jordan'])>>> a = list(m.keys())>>> a['marty', 'roy', 'jordan']

>>> 'marty' in mTrue>>> 'roy' in mTrue>>> 'oy' in mFalse*>>> s = {'a', 'b', 'c', 'a', 'b', 'd'}>>> s{'a', 'c', 'b', 'd'}>>> 'b' in sTrue>>> a = set('hello world i am soooooo happy')>>> a{'a', ' ', 'e', 'd', 'i', 'h', 'm', 'l', 'o', 'p', 's', 'r', 'w', 'y'}>>> *****Lets explain this plz.*If there is time.

Recommended