Data Structures in Python - PowerPoint PPT Presentation

About This Presentation
Title:

Data Structures in Python

Description:

Data Structures in Python By: Christopher Todd – PowerPoint PPT presentation

Number of Views:81
Avg rating:3.0/5.0
Slides: 18
Provided by: Christo594
Category:
Tags: data | dict | python | structures

less

Transcript and Presenter's Notes

Title: Data Structures in Python


1
Data Structures in Python
  • By Christopher Todd

2
Lists in Python
  • A list is a group of comma-separated values
    between square brackets.
  • The values can be of multiple types such as
    strings, ints, or other lists.
  • Lists start at the indices 0
  • Defining a list
  • A hello, 5, test, 8
  • B 0, 1, test2, A

3
Methods of a List
  • len()
  • Gives the size of the list
  • append()
  • Adds an item to the end of the list
  • extend(L)
  • Extend the list by appending all the items
  • remove(x)
  • Remove the first item from the list whose value
    is x. It is an error if there is no such item.
  • pop(i)
  • Remove the item at the given position in the list

4
Methods of a List
  • index(x)
  • Return the index of the first item whose value is
    equal to x
  • count(x)
  • Return the number of times x is in the list
  • sort()
  • Sort the items of the list
  • reverse()
  • Reverse the items of the list

5
Lists as Stacks
  • List methods make it easy to use lists as stacks
  • The Stacks are formed with the last element being
    added is the first element retrieved
  • (last-in, first-out)
  • Use append() to add to the top of the stack
  • Use pop() to retrieve from the top of the stack

6
Lists as Queues
  • Unlike sets, lists are not as efficient for
    forming Queues
  • Appending and popping from the front of the list
    is slower than doing the same from the back
  • The Queues are formed with the first element
    being added is the first element retrieved
  • (first-in, first-out)
  • Use collections.deque to implement a queue
  • This allows for faster access to both sides of
    the list

7
Tools when using Lists
  • filter(function, sequence)
  • Returns a sequence consisting of items from the
    sequence that returns a true value for
    function(item)
  • Outputs either a string, tuple, or list
  • If sequence is a string or tuple, it returns data
    of the same type
  • If sequence is anything else it returns a list
  • map(function, sequence)
  • Calls function(item) for each of the sequences
    items
  • Returns a list of return values from the
    function(item) call
  • reduce(function, sequence)
  • Returns a single value constructed by calling the
    first two items of a sequence, then the result
    and the next item until the sequence is spent

8
List Comprehensions
  • List comprehensions provide a concise way to
    create lists without resorting to the use of the
    aforementioned methods.
  • Each list comprehension consists of an expression
    followed by a for clause, then 0 or more for or
    if clauses.
  • The result will be a list evaluated from the
    expression of the for and if clauses.
  • If the resulting list is to be converted into a
    Tuple, it must be parenthesized.
  • List comprehensions are much more flexible that
    map() and can be applied to complex expressions
    and nested functions

9
Tools when using Lists
  • filter(function, sequence)
  • Returns a sequence consisting of items from the
    sequence that returns a true value for
    function(item)
  • Outputs either a string, tuple, or list
  • If sequence is a string or tuple, it returns data
    of the same type
  • If sequence is anything else it returns a list
  • map(function, sequence)
  • Calls function(item) for each of the sequences
    items
  • Returns a list of return values from the
    function(item) call
  • reduce(function, sequence)
  • Returns a single value constructed by calling the
    first two items of a sequence, then the result
    and the next item until the sequence is spent

10
The del Statement
  • The del statement is a way to remove an item from
    a list given its index, instead of its value
  • The del statement can be used to remove slices
    from a list or clear an entire list
  • del can also be used to delete variables
  • del a
  • If you reference the variable a after this, the
    program returns an error
  • Unlike pop(), del does not return a value

11
Tuples
  • Tuples ordered list separated by commas
  • Usually used in databases and formatting
  • Some information about Tuples
  • output Tuples are always enclosed in parentheses
  • Tuples are immutable
  • Defining Tuples
  • Empty Tuples
  • Empty ()
  • Tuples with one value
  • One hello,

12
Sets
  • Sets Unordered list that contain varying
    elements
  • Usually used in membership testing and
    eliminating duplicate information
  • Some information about sets
  • Cannot contain duplicate information
  • Set objects support mathematical operation

13
Dictionaries
  • Dictionaries Unordered associative arrays
  • Dictionaries indexed by keys which can be any
    immutable type such as int, string, or tuple
  • You cannot use lists as keys
  • Dictionarys keys must be unique
  • Dictionaries are unordered sets with a keyvalue
    relationship

14
Dictionaries Cont.
  • Defining Dictionaries
  • Use curly brackets
  • Separate keys and values by
  • A) dictest first 1st, second 2nd
  • B) dict(first1st, second2nd)
  • A and B create the same Dictionary
  • key()
  • Returns all of the keys in the Dictionary

15
Looping Techniques
  • iteritems()
  • Used for printing the keys and values of a
    dictionary while looping
  • enumerate()
  • Used for printing the index and value of a list
    while looping
  • zip()
  • To loop over two or more lists at the same time
  • reverse()
  • To loop over a list in reverse
  • sorted()
  • To loop over a list in sorted order

16
Conditions
  • The conditions used in while and if statements
    can contain any operators, not just comparisons.
  • Comparison operators have a lower priority than
    mathematical operators
  • Comparisons can be chained
  • a lt b lt c
  • Checks to see if a is less than b, than if b is
    less than c.

17
Conditions Cont.
  • In not in check to see if values exist in a
    list or sequence
  • Is is not check to see if two values are equal
  • AND, OR, and NOT can all be used to combine
    multiple comparisons
  • Parenthesis can be used for grouping
  • The operators are tested left to right
Write a Comment
User Comments (0)
About PowerShow.com