Python:: Easy lab

profileLLLLEE
hint1.pdf

Python tidbits for CSE 5120 March Programming Assignment

Kerstin Voigt, March 2021

Work in Python Shell to test out code snippets

Program loaded

dtab a copy of list DATATABLE

DATATABLE is a list of Python dictionaries

Each line a dictionary that lists KEY : VALUE pairs

Let’s all each line an INSTANCE

K. Voigt
Highlight
K. Voigt
Highlight
K. Voigt
Highlight
K. Voigt
Highlight
K. Voigt
Highlight
K. Voigt
Highlight

The third instance of datatable dtab

Iterating over all ATTRIBUTES of the instance, Here: just to print

Iterating over all VALUES of the instance, Here: just to print

Iterating over all ITEMS (key:val) of the instance …

K. Voigt
Highlight
K. Voigt
Highlight
K. Voigt
Highlight
K. Voigt
Highlight
K. Voigt
Highlight
K. Voigt
Highlight
K. Voigt
Highlight

A Python dictionary is equivalent to a C++ Map data structure;

Use [ ] operator on key to obtain value associated with key

A RULE is represented by plain LIST of attributes; all attribs are are to have value 1 in an instance in order for rule to “cover” the instance (= rule is applicable to instance)

This function definition should go into your .py file …. But for quick prototyping, you CAN define a quick fct within the Pyton shell … benefit of interpreter!

K. Voigt
Highlight
K. Voigt
Highlight
K. Voigt
Highlight
K. Voigt
Highlight
K. Voigt
Highlight

Again: put fct def into .py file; in defined in shell just for demo

DEC is global; here DEC = ‘ok’

Neither of these 4 rules covers any neg instance, i.e., inst wit ‘ok’ : 0

These 2 rules cover one or more neg instances

… find them in listing here …

Empty rule [ ] covers all instances be default; thus True

K. Voigt
Highlight
K. Voigt
Highlight
K. Voigt
Highlight

Left to define …

def num_covers(dtab, rule):

… your code …

• Identifies and counts all instances in dtab that are covered by the rule

• Special case: if rule is empty, return the length of dtab bec “all” instances are coverd by rule [ ]

def num_covers_pos(dtab, rule):

… your code …

• Identifies and counts all POSITIVE instances (those with ‘ok’:1) that are covered by the rule

• Special case: if rule is empty, return the number of all positive instances in dtab; they are all covered by rule [ ]

K. Voigt
Highlight
K. Voigt
Highlight
K. Voigt
Highlight
  • Python tidbits for CSE 5120 March Programming Assignment�
  • Slide Number 2
  • Slide Number 3
  • Slide Number 4
  • Slide Number 5
  • Left to define …