Dictionaries, File operations

Slides:



Advertisements
Similar presentations
Dictionaries: Keeping track of pairs
Advertisements

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.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 16 Dictionaries 5/10/09 Python Mini-Course: Lesson 16 1.
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.
Python Dictionary.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
Python Programming Chapter 10: Dictionaries Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
Sequences Jordi Cortadella Department of Computer Science.
Built-in Data Structures in Python An Introduction.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Computing Science 1P Large Group Tutorial: Lab Exam & Class Test Simon Gay Department of Computing Science University of Glasgow 2006/07.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
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.
Chapter 10 Loops: while and for CSC1310 Fall 2009.
Dictionaries Intro to Computer Science CS 1510 Dr. Sarah Diesburg.
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
Lakshit Dhanda. Output Formatting Python has ways to convert any value to a string. 2 Methods repr() – meant to generate representations of values read.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Dictionaries. The compound types you have learned about - - strings, lists, and tuples – use integers as indices. If you try to use any other type as.
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.
MapReduce, Dictionaries, List Comprehensions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
More Python Data Structures  Classes ◦ Should have learned in Simpson’s OOP ◦ If not, read chapters in Downey’s Think Python: Think like a Computer Scientist.
String and Lists Dr. José M. Reyes Álamo.
CSc 110, Autumn 2016 Lecture 26: Sets and Dictionaries
10 - Python Dictionary John R. Woodward.
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Algorithmic complexity: Speed of algorithms
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
Topics Introduction to Repetition Structures
Lecture – 2 on Data structures
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Introduction to Repetition Structures
CSc 110, Spring 2018 Lecture 32: Sets and Dictionaries
CSc 110, Autumn 2017 Lecture 30: Sets and Dictionaries
Introduction to Python
LING 388: Computers and Language
CISC101 Reminders Quiz 2 this week.
CMSC201 Computer Science I for Majors Lecture 20 – Dictionaries
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
Introduction to Python
CSc 110, Spring 2018 Lecture 33: Dictionaries
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
Python Data Structures
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Introduction to Dictionaries
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Algorithmic complexity: Speed of algorithms
Topics Sequences Introduction to Lists List Slicing
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Dictionaries Dictionary: object that stores a collection of data
Using the Target Variable Inside the Loop
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Quiz #2: Functions Wednesday, February 25.
CHAPTER 4: Lists, Tuples and Dictionaries
Algorithmic complexity: Speed of algorithms
Topics Introduction to Repetition Structures
Topics Sequences Introduction to Lists List Slicing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Python
CMSC201 Computer Science I for Majors Lecture 19 – Dictionaries
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Presentation transcript:

Dictionaries, File operations IIITD Dictionaries, File operations

Dictionary A dictionary is like a list, but more general. In a list, the positions (a.k.a. indices) have to be integers; in a dictionary the indices can be (almost) any type  think of a dictionary as a mapping between a set of indices (which are called keys) and a set of values. Each key maps to a value. The association of a key and a value is called a key-value pair or sometimes an item. The function dict creates a new dictionary with no items. Because dict is the name of a built-in function, you should avoid using it as a variable name

Contd.. >>> eng2sp = dict() >>> print eng2sp {} The squiggly-brackets, {}, represent an empty dictionary. To add items to the dictionary, you can use square brackets: >>> eng2sp['one'] = 'uno'

Contd.. This line creates an item that maps from the key 'one' to the value 'uno'. If we print the dictionary again, we see a key-value pair with a colon between the key and value: >>> print eng2sp {'one': 'uno'} This output format is also an input format. For example, you can create a new dictionary with three items:

>>> eng2sp = {'one': 'uno', 'two': 'dos', 'three': 'tres'} >>> print eng2sp {'one': 'uno', 'three': 'tres', 'two': 'dos'} The order of the key-value pairs is not the same. In fact, if you type the same example on your computer, you might get a different result. In general, the order of items in a dictionary is unpredictable.

But that's not a problem because the elements of a dictionary are never indexed with integer indices. >>> print eng2sp['two'] 'dos‘ The len function works on dictionaries; it returns the number of key-value pairs:

In operator The in operator works on dictionaries; it tells you whether something appears as a key in the dictionary (appearing as a value is not good enough). To see whether something appears as a value in a dictionary, you can use the method values, which returns the values as a list, and then use the in operator:

>>> vals = eng2sp.values() >>> 'uno' in vals True The in operator uses different algorithms for lists and dictionaries. For lists, it uses a linear search algorithm. As the list gets longer, the search time gets longer in direct proportion to the length of the list. For dictionaries, Python uses an algorithm called a hash table that has a remarkable property; the in operator takes about the same amount of time no matter how many items there are in a dictionary

As a set of counters: http://www.pythonlearn.com/html- 007/cfbook010.html

Take Away Assignment No Deadline and Grading-It’s for your learning!!. Next Class-Classes and objects Please read the topics we have done till now..