LING 388 Computers and Language Lecture 7 Administrivia








![Python List as a Stack • Suppose we have list = ['a', 'c', 'b'] Python List as a Stack • Suppose we have list = ['a', 'c', 'b']](https://slidetodoc.com/presentation_image_h2/a8b2a02f5e2e9531dd72cb41ea6d86fb/image-9.jpg)

![Python List as a Queue • Queuing operations are: • list. append() • list[0] Python List as a Queue • Queuing operations are: • list. append() • list[0]](https://slidetodoc.com/presentation_image_h2/a8b2a02f5e2e9531dd72cb41ea6d86fb/image-11.jpg)


- Slides: 13

LING 388: Computers and Language Lecture 7

Administrivia • Last time: Strings • Terminal log for lecture 6 available • This time: Lists • Quick Homework 5

Python • https: //docs. python. org/3/tutorial/introduction. html • Numbers • Strings • Lists • Dictionaries

Python Lists Recall strings from last time? Some similarities: indexing, slicing Think of those as lists too

Python Lists vs. Sets • in – membership • not in – not a member of • set(List) - produces a set, no duplicates permitted • set is faster than list for lookup (hashtable)

Python Lists

Python Lists https: //visualgo. net/en/list? slide=4 • Lists as stacks • Lists as queues • List Comprehensions (advanced topic) https: //www. appcoda. com/ios-concurrency/

Python List as a Stack • Note that. pop() removes from the right end and. append() adds to the right end. • How to save the element of a list popped off the stack? Use a variable, e. g. x: • x = list. pop() • Stacking operations are: • list. append() • list. pop()
![Python List as a Stack Suppose we have list a c b Python List as a Stack • Suppose we have list = ['a', 'c', 'b']](https://slidetodoc.com/presentation_image_h2/a8b2a02f5e2e9531dd72cb41ea6d86fb/image-9.jpg)
Python List as a Stack • Suppose we have list = ['a', 'c', 'b'] • How do we flip the order of b and c using stack operations? • Answer: >>> x 1 = list. pop() >>> x 2 = list. pop() >>> x 1 = list. append(x 1) >>> x 1 = list. append(x 2)

Python List as a Queue EXAMPLE: • Method append() to add to right end of the queue • list[0] gives us the head, i. e. left end, of the queue • Note: x = list[0] saves the head of the queue into variable x • list = list[1: ] deletes the head of the queue from the queue • Also can use del list[0]
![Python List as a Queue Queuing operations are list append list0 Python List as a Queue • Queuing operations are: • list. append() • list[0]](https://slidetodoc.com/presentation_image_h2/a8b2a02f5e2e9531dd72cb41ea6d86fb/image-11.jpg)
Python List as a Queue • Queuing operations are: • list. append() • list[0] • del list[0]

Quick Homework 5 • Suppose we define a variable list as follows: • list = [4, 2, 3, 1] • Use only the operations demonstrated in class. • Q 1: Show a shortest sequence of stacking operations that sorts list into a list in ascending order, i. e. you should end up with: • list = [1, 2, 3, 4] • Q 2: How many stacking operations are needed? • Q 3: Show a shortest sequence of queuing operations that sorts list into a list in ascending order, i. e. you should end up with: • list = [1, 2, 3, 4] • Q 4: How many queuing operations are needed?

Python List as a Queue FYI: • A faster implementation according to section 5. 1. 2 • Don't use for your homework