CSE 231 Lab 9.

Slides:



Advertisements
Similar presentations
Computer Science 112 Fundamentals of Programming II Overview of Collections.
Advertisements

Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Dictionaries: Keeping track of pairs
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
Genome Sciences 373 Genome Informatics Quiz Section 2 April 7, 2015.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 14 Tuples, Sets, and Dictionaries 1.
Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming.
Built-in Data Structures in Python An Introduction.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
1 Joe Meehean.  List of names  Set of names  Map names as keys phone #’s as values Phil Bill Will Phil Bill Will Phil Bill Will Phil: Bill:
14. DICTIONARIES AND SETS Rocky K. C. Chang 17 November 2014 (Based on from Charles Dierbach, Introduction to Computer Science Using Python and Punch and.
Sets Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
LECTURE 3 Python Basics Part 2. FUNCTIONAL PROGRAMMING TOOLS Last time, we covered function concepts in depth. We also mentioned that Python allows for.
Dictionaries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction to Python Aug 22, 2013 Hee-gook Jun.
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
Copyright © 2013 by John Wiley & Sons. All rights reserved. SETS AND DICTIONARIES CHAPTER Slides by James Tam Department of Computer Science, University.
1 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. for-each PROS easy to use access to ALL items one-by-one ability to change the state.
String and Lists Dr. José M. Reyes Álamo.
CSc 110, Autumn 2016 Lecture 26: Sets and Dictionaries
for-each PROS CONS easy to use access to ALL items one-by-one
Sets Set is an unordered list of items
Lecture 3 Python Basics Part 2.
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Python – May 18 Quiz Relatives of the list: Tuple Dictionary Set
CSCI 104 Abstract Data Types
CSc 110, Spring 2017 Lecture 29: Sets and Dictionaries
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
Containers and Lists CIS 40 – Introduction to Programming in Python
Announcements Project 4 due Wed., Nov 7
Data types: Complex types (Dictionaries)
CSc 110, Spring 2017 Lecture 28: Sets and Dictionaries
Ruth Anderson CSE 140 University of Washington
CSc 110, Spring 2018 Lecture 32: Sets and Dictionaries
CSc 110, Autumn 2017 Lecture 30: Sets and Dictionaries
CISC101 Reminders Quiz 2 this week.
Basic Python Collective variables (sequences) Lists (arrays)
CHAPTER THREE Sequences.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
String and Lists Dr. José M. Reyes Álamo.
Presented by Mirza Elahi 10/29/2018
Ruth Anderson UW CSE 160 Winter 2017
Datatypes in Python (Part II)
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
CS 1111 Introduction to Programming Fall 2018
Dictionaries Dictionary: object that stores a collection of data
Michael Ernst CSE 140 University of Washington
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Assignment 2 due today.
Ruth Anderson CSE 160 University of Washington
CSE 231 Lab 3.
Ruth Anderson UW CSE 160 Spring 2018
CSE 231 Lab 6.
CSE 231 Lab 8.
Introduction to Strings
CMSC201 Computer Science I for Majors Lecture 19 – Dictionaries
Sets CS3240, L. grewe.
Sample lecture slides.
Dictionary.
Introduction to PYTHON
Introduction to Computer Science
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

CSE 231 Lab 9

Nested Dictionaires sets Topics to cover Nested Dictionaires sets

What is Nested Dictionary? A dictionary inside a dictionary Creating a nested dictionary: people = {1: {'name': ‘Zach', 'age': '7', 'sex': 'Male'}, 2: {'name': ‘Imen', 'age': ‘40', 'sex': 'Female'}} print(people[1]['name']) print(people[1]['age']) print(people[1]['sex']) Zach 7 Male

What is Nested Dictionary? Modifying a nested dictionary: people[2]['married'] = 'Yes‘ print(people[2]) del people[2]['married'] {'name': 'Imen', 'age': '40', 'sex': 'Female', 'married': 'Yes'} {'name': 'Imen', 'age': '40', 'sex': 'Female'}

What is Nested Dictionary? Person ID: 1 name: Zach age: 7 sex: Male Person ID: 2 name: Imen age: 40 sex: Female Iterating through a nested dictionary: for id, info in people.items(): print("\nPerson ID:", id) for key in info: print(key + ':', info[key])

Sets Think of mathematics. All items in a set are unique (no duplicates). Unordered Collection of unique objects Last data structure Use { } just like dictionaries But not key:value pairs Example: { 1, 2, 3 } The set doesn’t maintain elements in any particular order.

{'b', 'd', 'a', 'c'} {'r', 'd'} Create and update a Set Creating a set S = set() # S = { } creates a dictionary! S = { 20, 5, 10 } S = set(“abcabbcd”) # Works with any iterable, ignores duplicates set comprehensions are also supported: Print(S) {'b', 'd', 'a', 'c'} S = {x for x in 'abracadabra' if x not in 'abc'} Print(S) {'r', 'd'}

True #A and B are equal sets Create and update a Set Creating a set A = {1, 2, 3} B = { 3, 2, 3, 1} True #A and B are equal sets Print(A == B) The order of elements is unimportant and duplicates are removed

Create and update a Set Adding/discarding an object S.add(100) # adds the object, ignores duplicates S.discard(100) # discards the object if it exists S.remove(100) # removes the object from set, but raises KeyError if the object doesn’t exist! Print(S) {'b', 100, 'd', 'a', 'c'} Print(S) {'b', 'd', 'a', 'c'} Print(S) KeyError: 100

Patterns Ints: Strings: Lists: x = 0 # initialize at zero x += 1 # increment the counter Strings: s = ‘’ # initialize with empty string s += ch # concatenate characters Lists: L = [] # initialize with empty list L.append(value) # append values to the list

Patterns Dictionaries: D = { } # initialize with empty dictionary S = “aabacdbacd” # we have a string, we want to count all the characters for ch in S: if ch in D: # check to see if the key exists in the dictionary D[ch] += 1 # increment the value if it exists else: D[ch] = 1 # set the value to 1 if it doesn’t exist

Patterns Sets: S = set() # initialize with empty set We have a dictionary file and we want to create a set of all its words S = set() # initialize with empty set fp = open(“dictionary.txt”) for line in fp: set.add(line.strip()) # add the word to the set, it ignores the duplicates

Useful Function, Methods & Operators len(S) returns the size of the set check whether an element belongs to a set using the keyword in Union: set_a | set_b set_a.union(set_b) Intersection: set_a & set_b set_a.intersection(set_b)

Useful Function, Methods & Operators Symmetric_Difference: the set of elements in precisely one of set_a or set_b Set_a ^ set_b Difference: the set of elements in set_a but not set_b set_a - set_b set_a.difference(set_b)

Sets examples A = {1, 2, 3, 4, 5} B = {4, 5, 6, 7, 8} print(A | B) print(A & B) print(A - B) print(B - A) print(A ^ B) {1, 2, 3, 4, 5, 6, 7, 8} {4, 5} {1, 2, 3} {8, 6, 7} {1, 2, 3, 6, 7, 8}