InfoDb Lists
Making personalized lists using InfoDb
In the cell below, the code creates a "dictionary" where all the data is stored. I decided to define certain things about me to show my understanding of data structures. tesst
InfoDb = []
# Creating keys and values for the data structure
InfoDb.append({
"FirstName": "Trent",
"LastName": "Cardall",
"Class": "APCSP",
"Date of Birth": "March 9, 2005",
"Place of Birth": "Scottsdale, AZ",
"Hobbies": ["Playing music", "Surfing", "Playing Lacrosse"] # [] makes ordered list
})
print (InfoDb)
The data structure was printed which shows proof that it works. However, it is not exactly easy for the average person to read. By using other functions we can format the list to make the output more organized. There are multiple ways to pull data out of the structure, but all of them will produce the same output.
def print_data(d_rec):
print(d_rec["FirstName"], d_rec["LastName"])
print("\t", "-Class:", d_rec["Class"])
print("\t", "-Date of Birth:", d_rec["Date of Birth"])
print("\t", "-Place of Birth:", d_rec["Place of Birth"])
print("\t", "-Hobbies:", end="")
print(", ".join(d_rec["Hobbies"]))
def for_loop():
print("A little about myself:")
for record in InfoDb:
print_data(record)
for_loop() # activating function
def while_loop():
print("A little about myself:")
i = 0
while i < len(InfoDb):
record = InfoDb[i]
print_data(record)
i +=1
return
while_loop()
def recursive_loop(i):
if i < len(InfoDb):
record = InfoDb[i]
print_data(record)
recursive_loop(i + 1)
print ("A little about myself:")
recursive_loop(0)
def reverse_order(i):
if i <= len(InfoDb):
record = InfoDb[i]
print_data(record)
reverse_order(i - 1)
print("Reverse order"+"\n")
print("A little about myself:")
reverse_order(6)
That didn't seem to work, and I couldn't think of any other ideas, so I tried a quick google search, and attempted new methods
InfoDb.reverse()
print(InfoDb)
That didn't seem to do anything either. I am not quite sure how to reverse this particular list, so I am going to attempt making a simpler list and reversing it.
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "v", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
print("Normal order:", alphabet, "\n")
alphabet.reverse()
print("Reverse order:", alphabet)
That worked! Unfortunately I was not able to find a way to reverse the previous, more complicated list I made but I did reverse a list.