|
| 1 | +########## SPLIT ########## |
| 2 | + |
| 3 | +msg = "Pay attention to each word that I say..." |
| 4 | +words = msg.split() #returns list |
| 5 | +print(words) |
| 6 | + |
| 7 | +msg = "this,is,important,data" #How to parse CSV |
| 8 | +print(msg.split(",")) |
| 9 | + |
| 10 | + |
| 11 | +########## SPLIT STRING BY LINE ########## |
| 12 | + |
| 13 | + |
| 14 | +#This may have came from a file, for example. |
| 15 | +msg = """\ |
| 16 | +Hey there. |
| 17 | +My name is Caleb! |
| 18 | +What's your name? |
| 19 | +You're name is NERD? Weird... |
| 20 | +Bye for now!""" |
| 21 | + |
| 22 | +print(msg) #to see how it is stored... |
| 23 | + |
| 24 | +print(msg.split('\n')) |
| 25 | + |
| 26 | +########## INPUT USING SPLIT ########## |
| 27 | + |
| 28 | +print("List your favorite foods separated by ', '") |
| 29 | +print("Example input: ") |
| 30 | +print("Kale, bok choy, brussel sprouts") |
| 31 | + |
| 32 | +foods = input().split(', ') |
| 33 | + |
| 34 | +for food in foods: |
| 35 | + print("You said " + food) |
| 36 | + |
| 37 | +#an obvious downfall is that this is very touchy. |
| 38 | +#instead, we could ask one food per line |
| 39 | + |
| 40 | +########## LOOPING TO GET USER INPUT ########## |
| 41 | + |
| 42 | +fav_foods = [] |
| 43 | +while True: |
| 44 | + print("Enter a food. q to quit: ", end="") |
| 45 | + fav = input() |
| 46 | + if str.lower(fav) == 'q': |
| 47 | + break |
| 48 | + fav_foods.append(fav) |
| 49 | + |
| 50 | +print("all foods:", fav_foods) |
| 51 | + |
| 52 | + |
| 53 | +########## LIST AS STACK ########## |
| 54 | + |
| 55 | +#Consider a stack of plates. |
| 56 | +#The last one you add is the first to be removed. |
| 57 | + |
| 58 | +#The data structure depends on adding to the end of a list: |
| 59 | +stack = [] |
| 60 | +stack.append("added") |
| 61 | +#and removing from the end of the list |
| 62 | +stack.pop() |
| 63 | + |
| 64 | +fav_foods = [] |
| 65 | +while True: |
| 66 | + print("Enter a food. q to quit, r to remove: ", end="") |
| 67 | + fav = input() |
| 68 | + if str.lower(fav) == 'q': |
| 69 | + break |
| 70 | + if str.lower(fav) == 'r': |
| 71 | + popped = fav_foods.pop() |
| 72 | + print("removed " + popped) |
| 73 | + print("all foods:", fav_foods) |
| 74 | + continue |
| 75 | + fav_foods.append(fav) |
| 76 | + print("all foods:", fav_foods) |
| 77 | + |
| 78 | +print("final foods:", fav_foods) |
| 79 | + |
| 80 | + |
| 81 | +########## QUEUE VARIATION ########## |
| 82 | + |
| 83 | + |
| 84 | +#The difference with a queue is that the first added is the first removed |
| 85 | +#consider a line to a roller coaster |
| 86 | +#first in line rides ride first. |
| 87 | + |
| 88 | +#The data structure depends on adding to the end of a list: |
| 89 | +stack = [] |
| 90 | +stack.append("added") |
| 91 | +#and removing from the FRONT of the list |
| 92 | +stack.pop(0) #remove index 0 |
| 93 | + |
| 94 | +fav_foods = [] |
| 95 | +while True: |
| 96 | + print("(QUEUE) Enter a food. q to quit, eat to remove: ", end="") |
| 97 | + fav = input() |
| 98 | + if str.lower(fav) == 'q': |
| 99 | + break |
| 100 | + if str.lower(fav) == 'eat': |
| 101 | + popped = fav_foods.pop(0) |
| 102 | + print("removed " + popped) |
| 103 | + print("all foods:", fav_foods) |
| 104 | + continue |
| 105 | + fav_foods.append(fav) |
| 106 | + print("all foods:", fav_foods) |
| 107 | + |
| 108 | +print("final foods:", fav_foods) |
| 109 | + |
0 commit comments