Intro to Computer Science CS 1510 Dr. Sarah Diesburg

Slides:



Advertisements
Similar presentations
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Advertisements

Dictionaries Last half of Chapter 5. Dictionary A sequence of key-value pairs – Key is usually a string or integer – Value can be any python object There.
COMPSCI 101 Principles of Programming Lecture 24 – Using dictionaries to manage a small file of information.
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Lists and Tuples Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Built-in Data Structures in Python An Introduction.
Chapter 9 Dictionaries and Sets.
Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Python Data Structures CMSC 201. Built in Types Today we will be talking about some other built in types in python! Tuples Sets Dictionaries.
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.
Introduction to Computing Using Python Dictionaries: Keeping track of pairs  Class dict  Class tuple.
Sets Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
7. Lists 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Dictionaries Intro to Computer Science CS 1510 Dr. Sarah Diesburg.
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.
Dictionaries Alexandra Stefan CSE1310 – University of Texas at Arlington.
String and Lists Dr. José M. Reyes Álamo.
COMPSCI 107 Computer Science Fundamentals
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
When to use Tuples instead of Lists
Announcements Project 4 due Wed., Nov 7
Dictionaries, File operations
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 8 Namespaces and Memory Realities
Dictionaries GENOME 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
COSC 1323 – Computer Science Concepts I
Lists – Indexing and Sorting
CHAPTER THREE Sequences.
Types, Truth, and Expressions (Part 2)
String Encodings and Penny Math
CS190/295 Programming in Python for Life Sciences: Lecture 6
6. Lists Let's Learn Python and Pygame
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Types, Truth, and Expressions (Part 2)
Chapter 8 Namespaces and Memory Realities
Intro to Computer Science CS1510 Dr. Sarah Diesburg
String and Lists Dr. José M. Reyes Álamo.
Introduction to Dictionaries
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Datatypes in Python (Part II)
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lists – Indexing and Sorting
Python Primer 1: Types and Operators
Introduction to Strings
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Dictionaries: Keeping track of pairs
How to use hash tables to solve olympiad problems
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Hash Tables By JJ Shepherd.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 8 Namespaces and Memory Realities
Intro to Computer Science CS1510 Dr. Sarah Diesburg
String Encodings and Penny Math
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Types, Truth, and Expressions
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
Introduction to Dictionaries
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CMSC201 Computer Science I for Majors Lecture 19 – Dictionaries
Dictionary.
Introduction to Computer Science
Presentation transcript:

Intro to Computer Science CS 1510 Dr. Sarah Diesburg Dictionaries Intro to Computer Science CS 1510 Dr. Sarah Diesburg

Questions New Assignment – PA10

More Data Structures We have seen the string and list data structures and their uses. In particular, the dictionary is an important, very useful part of Python as well as generally useful to solve many problems.

What is a Dictionary? In data structure terms, a dictionary is better termed an associative array or associative list or a map. You can think if it as a list of pairs, where the first element of the pair, the key, is used to retrieve the second element, the value. Thus we map a key to a value.

Key Value Pairs The key acts as a “lookup” to find the associated value. Just like a dictionary, you look up a word by its spelling to find the associated definition. A dictionary can be searched to locate the value associated with a key.

Python Dictionary Use the { } marker to create a dictionary Use the : marker to indicate key:value pairs: contacts= {‘bill’: ‘353-1234’, ‘rich’: ‘269-1234’, ‘jane’: ‘352-1234’} print (contacts) {‘jane’: ‘352-1234’, ‘bill’: ‘353-1234’, ‘rich’: ‘369-1234’}

Keys and Values Key must be immutable: Value can be anything. strings, integers, tuples are fine lists are NOT Value can be anything.

Collections but not a Sequence Dictionaries are collections, but they are not sequences like lists, strings or tuples: there is no order to the elements of a dictionary in fact, the order (for example, when printed) might change as elements are added or deleted. So how to access dictionary elements?

Access Dictionary Elements Access requires [ ], but the key is the index! myDict={} an empty dictionary myDict[‘bill’]=25 added the pair ‘bill’:25 print(myDict[‘bill’]) prints 25

Dictionaries are Mutable Like lists, dictionaries are a mutable data structure: you can change the object via various operations, such as index assignment myDict = {‘bill’:3, ‘rich’:10} print (myDict[‘bill’] ) # prints 3 myDict[‘bill’] = 100 print (myDict[‘bill’]) # prints 100

Again, Common Operators Like others, dictionaries respond to these: len(myDict) number of key:value pairs in the dictionary element in myDict boolean, is element a key in the dictionary for key in myDict: iterates through the keys of a dictionary

Lots of Methods myDict.items() – all the key/value pairs myDict.keys() – all the keys myDict.values() – all the values key in myDict does the key exist in the dictionary myDict.clear() – empty the dictionary myDict.update(yourDict) – for each key in yourDict, updates myDict with that key/value pair

Dictionaries are Iterable for key in myDict: print(key) prints all the keys for key,value in myDict.items(): print(key,value) prints all the key/value pairs for value in myDict.values(): print(value) prints all the values

Doing something with this Write a function called letterCount that: takes in a string as a parameter prints a table of the letters of the alphabet (in alphabetical order ) together with the number of times each letter occurs. Case should be ignored.