Dictionaries
dictionary.py
lockers = {"Prudence":39, "Digby":48} lockers["Eggbert"] = 10 print(lockers["Digby"]) #48 print(lockers) #{'Prudence': 39, 'Digby': 48, 'Eggbert': 10}
searching or removing.py
lockers = {"Prudence":39, "Digby":48, "Eggbert":10} newStudent = "Eggbert" emptyLocker = 99 if newStudent in lockers: print(newStudent + " has a locker already") else: lockers[newStudent] = emptyLocker pastStudent = "Belvedere" try: del lockers[pastStudent] except KeyError: print("There is no student named:", pastStudent)
dictionaries.py
#A dictionary stores key:value pairs - KEYPAD = { 'A': '2', 'B': '2', 'C': '2', 'D': '3', 'E': '3', 'F': '3', 'G': '4', 'H': '4', 'I': '4', 'J': '5', 'K': '5', 'L': '5', 'M': '6', 'N': '6', 'O': '6', 'P': '7', 'Q': '7', 'R': '7', 'S': '7', 'T': '8', 'U': '8', 'V': '8', 'W': '9', 'X': '9', 'Y': '9', 'Z': '9', } word = input('Enter word: ') for letter in word.upper(): print(KEYPAD[letter], end= " ")
more dictionaries.py
accounts={ "Joe":"abc", "Sue":"123" } authenticated = False while not authenticated: username = input("username: ") password = input("password: ") if username not in accounts: create_choice = input("username not found. create new?") if (create_choice.lower() == "y"): accounts[username] = password authenticated = True else: print("username found. ") if password == accounts[username]: authenticated = True else: print("invalid password.") print("authenticated. ") for each_account in accounts: print(each_account, accounts[each_account])
sorting dictionary by value.py
dictionary = { "Terry": 29, "Sue": 36, "Joel": 18, "Mary": 53, "Peta": 5 } #just the keys: print(sorted(dictionary)) #just the keys, reversed: print(sorted(dictionary, reverse = True)) #just the values (reverse will work on this too): print(list(sorted(dictionary.values()))) print(list(sorted(dictionary.values(), reverse = True))) #---------------------------------------------------------------- #lambda = small, anonymous (no-name), throw-away function. syntax: #lambda argument_list: expression print(sorted(dictionary, key = lambda key: dictionary[key])) #Key parameter specifies a ~function~ to be called #on each element prior to making comparisons... #So the lambda function above is identical to: def sort_key(value): return dictionary[value] print(sorted(dictionary, key = sort_key)) #lambda is a just a function that is not bound to a name at runtime - #other then that, its just a fancy way of saying function.. e.g.: plusone = lambda x: x + 1 print(plusone(1)) #^^ could have just written a function! #lambda is useful though for sorts, maps, filter, reduce, etc.. #feel free to look these uses up!
dict_to_disk.py
#----WRITING DICT TO DISK: dictionary = { "john": "abc", "mary": "xyz" } #----------------SAVING: file = open("users.txt", "w") file.write(str(dictionary)) file.close() #----------------LOADING: import ast with open("users.txt", "r") as file: for line in file: string = line x = ast.literal_eval(string) print(x["john"]) #abc
- Read in multiple lines of plain text from the user, then print out each different word that came in from the input, with a count of how many times that word occurred. Put it in alphabetical order of word. For example, your program should look like this when it runs:
Enter line: gday mate Enter line: cheers mate Enter line:? cheers 1 gday 1 mate 2
Hint: You can usesorted
on dictionaries to put the keys into alphabetical order, for example:for word in sorted(dictionary):
- use a dictionary with a list as a value to store information about user bank accounts based on their username (key). The information included should be password and balance, for example:
user = {} user["Jane"] = ["abcd1234", 500] user["John"] = ["password", 200]
Your program should check if the user exists, and check if the password matches. If it does, set the balance. If the user doesn't exist, they should have the opportunity to register an account with a starting balance of 100. Hint:username = input("what is your name? ") if username in user:
- Create the following dictionary of foods, using categories as keys and items as values:
foods = { "pizza": ["hawaiian", "supreme", "bbq"], "pasta": ["rice", "risotto", "gnocchi"], "curry": ["massaman", "panang", "lamb"] }
Using the above foods dictionary:- List the types of pizza
- Remove rice from the types of pasta
- Add Green Chicken to the types of curry
- Add drinks to the categories (i.e. create a new key), with values: Coke, Juice and Water
- Count the number of categories in the dictionary
- Count the number of food items in the dictionary