Oct-17-2019, 06:05 AM
(Oct-16-2019, 04:37 PM)Mark17 Wrote: I don't understand why. Seeing a simple example and the kind of traceback that can result might help me understand a bit more.
Simple example. Lets say we have class and want have ledger. Normal way would be to have dictionary but we can to it with separate variables:
john = 'student' mary = 'student' sonia = 'teacher'How could we find out how many students we have? How could we find out names of the students? etc, etc
On the other hand, with dictonaries it's simple:
>>> class_ledger = {'john': 'student', 'maria': 'student', 'sonia': 'teacher'} >>> len(class_ledger) # total number of people 3 >>> sum(1 for k, v in class_ledger.items() if v == 'student') # number of students 2 >>> print(*(k for k, v in class_ledger.items() if v == 'student'), sep='\n') # names of students john maria >>> 'maria' in class_ledger # is maria in a ledger True >>> class_ledger.update({'Diego': 'student'}) # add new person to class >>> class_ledger {'john': 'student', 'maria': 'student', 'sonia': 'teacher', 'Diego': 'student'}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.