-
Define a variable to hold information about a student, including name, age, grade (single subject), phone number, gender
stu_inf={'name':'Xiao Ming','age':18,'score':98,'tel':13884465289,'gender':'female'}
-
Define a list with information about six students (including name, age, grade (single subject), phone, gender (male, female, unknown)
stu_inf=[{'name':'Xiao Ming','age':18,'score':98,'tel':'13884465288','gender':'female'}, {'name':' Little Red','age':19,'score':78,'tel':'13884464589','gender':'female'}, {'name':' Little Black','age':24,'score':72,'tel':'13884995289','gender':'unknown'}, {'name':' Blue','age':15,'score':59,'tel':'13884435288','gender':'male'}, {'name':' Floret','age':17,'score':88,'tel':'13684995289','gender':'female'}, {'name':' Xiaowang','age':19,'score':85,'tel':'13881295289','gender':'male'}]
-
Count the number of failed students
count=0 for dict in stu_inf: if dict['score']<60: count+=1 print(count)
-
Print the names of underage students who fail and the corresponding results
for dict in stu_inf: if dict['score']<60 and dict['age']<18: print(dict['name'],dict['score']) #Small Blue 59
-
Seek the average age of all boys
nums=0 sum=0 for dict in stu_inf: if dict['gender']=='male': nums += 1 sum+=dict['age'] average=sum/nums print('The average age of boys is:',int(average))
-
Print the name of the student whose last number is 8
for dict in stu_inf: if dict['tel'][-1]=='8': print('Name:',dict['name']) #Name: Xiao Ming Name: Blue
-
Print the highest score and the name of the corresponding student
for dict in stu_inf: list = [dict['score'] for dict in stu_inf] score=max(list) if dict['score']==score: print(dict['name']) #Xiao Ming
-
Delete all students of Unknown Gender
for dict in stu_inf: if dict['gender']=='unknown': stu_inf.remove(dict) print(stu_inf)
-
Sort the list by student grade (struggle, give up if you can't)
-
-
Define a variable to hold information about a class, which includes class name, class location, head teacher information, lecturer information, and all the students in the class (determine the data type and specific information based on the actual situation)
-
A list is known to hold dictionaries for multiple dogs:
dogs = [ {'name': 'Beibei', 'color': 'white', 'breed': 'Silver Fox', 'age': 3, 'gender': 'mother'}, {'name': 'tearful', 'color': 'gray', 'breed': 'Fa Dou', 'age': 2}, {'name': 'Finance', 'color': 'black', 'breed': 'Poodle', 'age': 5, 'gender': 'common'}, {'name': 'Steamed stuffed bun', 'color': 'yellow', 'breed': 'Siberian Husky', 'age': 1}, {'name': 'cola', 'color': 'white', 'breed': 'Silver Fox', 'age': 2}, {'name': 'Prosperous Money', 'color': 'yellow', 'breed': 'Poodle', 'age': 2, 'gender': 'mother'} ]
-
Obtain all dog breeds using list derivation
['Silver Fox','Fa Dou','Poodle','Husky','Silver Fox','Poodle']
breed=[x['breed']for x in dogs] print(breed) #['Silver Fox','Fa Dou','Poodle','Husky','Silver Fox','Poodle']
-
Get the names of all white dogs using list derivation
['Beibei','Coke']
names=[x['name'] for x in dogs if x['color']=='white'] print(names) #['Beibei','Coke']
-
Add a gender of'male'to a dog that has no gender in the dogs
for x in dogs: x.setdefault('gender','common') print(dogs)
-
Count the number of Silver Foxes
count=0 for x in dogs: if x['breed']=='Silver Fox': count+=1 print(count) #2
Day 8 Dictionary
1. Get elements from variables
1. Use multiple variables to get the elements of a list or tuple at the same time
Requires that the number of variables be consistent with the number of tuple/list elements
2. Use multiple variables to get the elements of a list or tuple at the same time
1) When a variable is less than the number of elements, it must be preceded by a variable*
2) Let the unbanded variables get the corresponding elements in the order when they are obtained, and save the rest in the banded variables
3. Tuples can be omitted without ambiguity
2. Understanding Dictionaries
Note: Use a dictionary when you need to save data of different meanings, or a list when the data you save is of the same type
1. What is a dictionary?
1) Dictionaries are container data types (sequences); As the flag of the container, there are several key-value pairs separated by commas, and each key-value pair is an element: {Key 1: Value 1, Key 2: Value 2, Key 3: Value 3,...}
2) Dictionaries are variable (supporting additions, deletions and alterations); Dictionary out of order (subscript operations are not supported)
3) Requirements for elements - elements are key-value pairs
Requirements for keys: Only data of immutable type can be used as keys. Generally strings are used as keys, keys are unique, and a key can only appear once in a dictionary
Required: No Requirements
2. Empty dictionary: {}
3. Additions, deletions and alterations of dictionaries
1. Check-Get Values
1) Check Individual
Dictionary [Key] - Gets the value corresponding to the specified key, and error occurs if the key does not exist
Dictionary.get (key) - Gets the value corresponding to the specified key, the key does not exist and returns none
Dictionary.get (key, default) - Gets the value corresponding to the specified key, the key does not exist to return the default value
2) Traversal
Forkey in dictionary
2. Add or Change
1) Dictionary [Key]=Value - Modify the value corresponding to the specified key if it exists, and add key-value pairs if it does not exist
2) Dictionary.default (key, value) - Add key-value pairs, add only without modification
3. Delete-Delete key-value pairs
1) del Dictionary [Key] - Delete key-value pairs corresponding to specified keys
2) Dictionary.pop (key) - Take out the value corresponding to the specified key
4. Dictionary-related operation functions and methods
1. Operators
Relative lists, dictionaries do not support: +, *, compare sizes
2. Correlation Functions
Dict - Convert data to a dictionary
Data requirements:
1) This data must be a sequence
2) Elements in a sequence must have a small sequence of only two elements, the first of which is immutable data.
3. Related Methods
1) Dictionary. clear() - Empty Dictionary
2) Dictionary.copy() - Copying a dictionary produces an identical new dictionary
3) a. Dictionary.values() - Gets all the values of a dictionary and returns a new sequence
b.Dictionary.keys() - Gets all keys of a dictionary and returns a new sequence
c.Dictionary.items() - Gets all keys and values of a dictionary, one tuple for each key-value pair, and returns a new sequence
4. Dictionary Derivation
1) {Expression 1: Expression 2 for variable in sequence}
2) {Expression 1: Expression 2 for variable in sequence if conditional statement}
5.Dictionary 1. update (Dictionary 2) - Add all key-value pairs from Dictionary 2 to Dictionary 1
-
day8-Dictionary job
Added by PetrZagvazdin on Wed, 08 Dec 2021 19:44:08 +0200