introduction to the python programming language · introduction to the python programming language...

8
Introduction to the Python programming language Laszlo SZATHMARY University of Debrecen Faculty of Informatics 2017-2018, 1st semester Lab #5 set dictionary (last update: 2017-08-03 [yyyy-mm-dd])

Upload: vuminh

Post on 29-Jun-2018

254 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to the Python programming language · Introduction to the Python programming language Laszlo SZATHMARY University of Debrecen Faculty of Informatics 2017-2018, 1st semester

Introduction to the Python programming language

Laszlo SZATHMARY University of Debrecen Faculty of Informatics

2017-2018, 1st semester

Lab #5 • set • dictionary

(last update: 2017-08-03 [yyyy-mm-dd])

Page 2: Introduction to the Python programming language · Introduction to the Python programming language Laszlo SZATHMARY University of Debrecen Faculty of Informatics 2017-2018, 1st semester

2

set https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset

Exercise: Consider the following elements: [5, 2, 3, 5, 1, 4, -200, 5, 1, 3, 2, 2, 5] . Remove the duplicates, i.e. one element can be present in the result at most once. The elements in the result should be sorted. (set01)

removing duplicates

Is an element in the set?

Page 3: Introduction to the Python programming language · Introduction to the Python programming language Laszlo SZATHMARY University of Debrecen Faculty of Informatics 2017-2018, 1st semester

3

list to set

empty set; extending a set

classic set operations

removing an element

Page 4: Introduction to the Python programming language · Introduction to the Python programming language Laszlo SZATHMARY University of Debrecen Faculty of Informatics 2017-2018, 1st semester

4

dictionary for storing key / value pairs

empty dictionary ( or: d = dict() )

Is an element with the given key in the dictionary?

Page 5: Introduction to the Python programming language · Introduction to the Python programming language Laszlo SZATHMARY University of Debrecen Faculty of Informatics 2017-2018, 1st semester

5

-- keys -- -- values --

dict

‘a’

‘b’

‘g’

‘alfa’

‘beta’

‘gamma’

Page 6: Introduction to the Python programming language · Introduction to the Python programming language Laszlo SZATHMARY University of Debrecen Faculty of Informatics 2017-2018, 1st semester

6

the order of the elements is arbitrary

iterators: d.keys() d.values() d.items()

using iterators in a loop

Iterator to list. The order is arbitrary. HW: dict1.py

Page 7: Introduction to the Python programming language · Introduction to the Python programming language Laszlo SZATHMARY University of Debrecen Faculty of Informatics 2017-2018, 1st semester

7

removing an element from a dictionary