COSC 1306 COMPUTER SCIENCE AND PROGRAMMING - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

COSC 1306 COMPUTER SCIENCE AND PROGRAMMING

Description:

COSC 1306 COMPUTER SCIENCE AND PROGRAMMING Jehan-Fran ois P ris jfparis_at_uh.edu Boolean operations on sets (IV) Symmetric difference of two sets Contains all ... – PowerPoint PPT presentation

Number of Views:87
Avg rating:3.0/5.0
Slides: 42
Provided by: Jehan46
Category:

less

Transcript and Presenter's Notes

Title: COSC 1306 COMPUTER SCIENCE AND PROGRAMMING


1
COSC 1306COMPUTER SCIENCE AND PROGRAMMING
  • Jehan-François Pâris
  • jfparis_at_uh.edu

2
CHAPTER VIIDICTIONARIES, TUPLES AND SETS
3
DICTIONARIES
VERY USEFUL
4
Dictionaries (I)
  • Store pairs of entries called items 'CS'
    '743-713-3350', 'UHPD' '713-743-3333'
  • Each pair of entries contains
  • A key
  • A value
  • Key and values are separated by a colon
  • Pairs of entries are separated by commas
  • Dictionary is enclosed within curly braces

5
Usage
  • Keys must be unique within a dictionary
  • No duplicates
  • If we have age 'Alice' 25, 'Bob'
    28then age'Alice' is 25and ageBob' is
    28

6
Dictionaries are mutable
  • gtgtgt age 'Alice' 25, 'Bob' 28
  • gtgtgt saved age
  • gtgtgt age'Bob' 29
  • gtgtgt age'Bob' 29, 'Alice' 25
  • gtgtgt saved'Bob' 29, 'Alice' 25

7
Keys must be unique
  • gtgtgt age 'Alice' 25, 'Bob' 28, 'Alice'
    26
  • gtgtgt age'Bob' 28, 'Alice' 26

8
Displaying contents
  • gtgtgt age 'Alice' 25, 'Carol' 'twenty-two'
  • gtgtgt age.items()dict_items( ('Alice', 25),
    ('Carol', 'twenty-two'))
  • gtgtgt age.keys()dict_keys( 'Alice', 'Carol')
  • age.values()dict_values(25, 'twenty-two')

9
Updating directories
  • gtgtgt age 'Alice' 26 , 'Carol' 22
  • gtgtgt age.update('Bob' 29)
  • gtgtgt age'Bob' 29, 'Carol' 22, 'Alice' 26
  • gtgtgt age.update('Carol' 23)
  • gtgtgt age'Bob' 29, 'Carol' 23, 'Alice' 26

10
Returning a value
  • gtgtgt age 'Bob' 29, 'Carol' 23, 'Alice' 26
  • gtgtgt age.get('Bob')29
  • gtgtgt age'Bob'29

11
Removing a specific item (I)
  • gtgtgt a 'Alice' 26, 'Carol' 'twenty-two'
  • gtgtgt a'Carol' 'twenty-two', 'Alice' 26
  • gtgtgt a.pop('Carol)'twenty-two'
  • gtgtgt a'Alice' 26

12
Removing a specific item (II)
  • gtgtgt a.pop('Alice')26
  • gtgtgt a
  • gtgtgt

13
Remove a random item
  • gtgtgt age 'Bob' 29, 'Carol' 23, 'Alice' 26
  • gtgtgt age.popitem()('Bob', 29)
  • gtgtgt age
  • 'Carol' 23, 'Alice' 26
  • gtgtgt age.popitem()('Carol', 23)
  • gtgtgt age'Alice' 26

14
TUPLES
NOT COVERED
15
Tuples
  • Same as lists but
  • Immutable
  • Enclosed in parentheses
  • A tuple with a single element must have a comma
    inside the parentheses
  • a (11,)

16
Examples
  • gtgtgt mytuple (11, 22, 33)
  • gtgtgt mytuple011
  • gtgtgt mytuple-133
  • gtgtgt mytuple01(11,)
  • The comma is required!

17
Why?
  • No confusion possible between 11 and 11
  • (11) is a perfectly acceptable expression
  • (11) without the comma is the integer 11
  • (11, ) with the comma is a list containing the
    integer 11
  • Sole dirty trick played on us by tuples!

18
Tuples are immutable
  • gtgtgt mytuple (11, 22, 33)
  • gtgtgt saved mytuple
  • gtgtgt mytuple (44,)
  • gtgtgt mytuple(11, 22, 33, 44)
  • gtgtgt saved(11, 22, 33)

19
Things that do not work
  • mytuple 55Traceback (most recent call
    last)ZTypeError can only concatenate tuple
    (not "int") to tuple
  • Can understand that!

20
Sorting tuples
  • gtgtgt atuple (33, 22, 11)
  • gtgtgt atuple.sort()Traceback (most recent call
    last)AttributeError'tuple' object has no
    attribute 'sort'
  • gtgtgt atuple sorted(atuple)
  • gtgtgt atuple11, 22, 33

Tuples are immutable!
sorted( ) returns a list!
21
Most other things work!
  • gtgtgt atuple (11, 22, 33)
  • gtgtgt len(atuple)3
  • gtgtgt 44 in atupleFalse
  • gtgtgt i for i for i in atuple11, 22, 33

22
The reverse does not work
  • gtgtgt alist 11, 22, 33
  • gtgtgt (i for i in alist)ltgenerator object
    ltgenexprgt at 0x02855DA0gt
  • Does not work!

23
Converting sequences into tuples
  • gtgtgt alist 11, 22, 33
  • gtgtgt atuple tuple(alist)
  • gtgtgt atuple(11, 22, 33)
  • gtgtgt newtuple tuple('Hello World!')
  • gtgtgt newtuple('H', 'e', 'l', 'l', 'o', ' ', 'W',
    'o', 'r', 'l', 'd', '!')

24
SETS
NOT COVERED A SPECIALIZED TOOL
25
Sets
  • Indentified by curly braces
  • 'Alice', 'Bob', 'Carol'
  • 'Dean' is a singleton
  • Can only contain unique elements
  • Duplicates are eliminated
  • Immutable like tuples and strings

26
Sets do not contain duplicates
  • gtgtgt cset 11, 11, 22
  • gtgtgt cset11, 22

27
Sets are immutable
  • gtgtgt aset 11, 22, 33
  • gtgtgt bset aset
  • gtgtgt aset aset 55
  • gtgtgt aset33, 11, 22, 55
  • gtgtgt bset33, 11, 22

Union of two sets
28
Sets have no order
  • gtgtgt 1, 2, 3, 4, 5, 6, 71, 2, 3, 4, 5, 6, 7
  • gtgtgt 11, 22, 3333, 11, 22

29
Sets do not support indexing
  • gtgtgt myset 'Apples', 'Bananas', 'Oranges'
  • gtgtgt myset'Bananas', 'Oranges', 'Apples'
  • gtgtgt myset0Traceback (most recent call last)
    File "ltpyshell2gt", line 1, in ltmodulegt
    myset0TypeError 'set' object does not support
    indexing

30
Examples
  • gtgtgt alist 11, 22, 33, 22, 44
  • gtgtgt aset set(alist)
  • gtgtgt aset33, 11, 44, 22
  • gtgtgt aset aset 55SyntaxError invalid syntax

31
Boolean operations on sets (I)
  • Union of two sets
  • Contains all elements that are in set A or in set
    B

A
B
32
Boolean operations on sets (II)
  • Intersection of two sets
  • Contains all elements that are in both sets A and
    B

A
B
33
Boolean operations on sets (III)
  • Difference of two sets
  • Contains all elements that are in A but not in B

A
B
34
Boolean operations on sets (IV)
  • Symmetric difference of two sets
  • Contains all elements that are either
  • in set A but not in set B or
  • in set B but not in set A

A
B
35
Boolean operations on sets (V)
  • gtgtgt aset 11, 22, 33
  • gtgtgt bset 12, 23, 33
  • Union of two sets
  • gtgtgt aset bset 33, 22, 23, 11, 12
  • Intersection of two sets
  • gtgtgt aset bset33

36
Boolean operations on sets (VI)
  • gtgtgt aset 11, 22, 33
  • gtgtgt bset 12, 23, 33
  • Difference
  • gtgtgt aset bset11, 22
  • Symmetric difference
  • gtgtgt aset bset11, 12, 22, 23

37
Summary
  • Strings, lists, tuples, sets and dictionaries all
    deal with aggregates
  • Two big differences
  • Lists and dictionaries are mutable
  • Unlike strings, tuples and sets
  • Strings, lists and tuples are ordered
  • Unlike sets and dictionaries

38
Mutable aggregates
  • Can modify individual items
  • x 11, 22, 33x0 44will work
  • Cannot save current value
  • x 11,22, 33y xwill not work

39
Immutable aggregates
  • Cannot modify individual items
  • s 'hello!'s0 'H'is an ERROR
  • Can save current value
  • s 'hello!'t swill work

40
Ordered aggregates
  • Entities in the collection can be accessed
    through a numerical index
  • s 'Hello!'s0
  • x 'Alice', 'Bob', 'Carol'x-1
  • t (11, 22)t1

41
Other aggregates
  • Cannot index sets
  • myset 'Apples', 'Bananas', 'Oranges'
    myset0 is WRONG
  • Can only index dictionaries through their keys
  • age 'Bob' 29, 'Carol' 23, 'Alice'
    26age'Alice' worksage0 is WRONG
Write a Comment
User Comments (0)
About PowerShow.com