-
Notifications
You must be signed in to change notification settings - Fork 2
/
lists.py
222 lines (162 loc) · 5.53 KB
/
lists.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python3
"""
Script to demonstrate list basics
See also: https://www.tutorialspoint.com/python/python_lists.htm
"""
def header(msg):
print("\n", msg)
print("-"*50)
# creating lists ------------------------------------------------------
mylist = list()
mylist = []
mylist = ['a', 'b', 'c', 'd', 'e']
# list index addresses ------------------------------------------------
mylist[0] # first list item
mylist[1] # second list item
mylist[-1] # last list item
# example: list index addresses ---------------------------------------
from datetime import datetime
days = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
]
header("example: list index addresses")
today = datetime.now().weekday()
print("the third day of the week is:", days[2])
print("today is:", days[today])
# list iteration ------------------------------------------------------
header("for <elm> in <list>")
for elm in mylist:
print(elm)
header("for <i>, <elm> in enumerate(<list>)")
for i, elm in enumerate(mylist):
print(f"{i}:", elm)
# example: list iteration ---------------------------------------------
colors = ["red", "green", "blue"]
header("colors")
for color in colors:
print(color)
# exercise: list iteration --------------------------------------------
"""
* Make a list of 3-5 colors, cities or lunch specials then print one per line
"""
# sean
# ----
header("sean's lunch specials")
lunch_specials = [
"ham-dogs",
"hot burgers",
"Donut Bucket",
"chicky tendies",
"Unfortunate tuna"
]
for lunch_special in lunch_specials:
print(lunch_special)
# jayson
# ------
header("jayson's cities")
# cities_that_are_special = ("Oceanside", "Oakland", "Denver")
# cities_that_are_special[3] = "Denver"
cities_that_are_special = ["Oceanside", "Oakland", "Denver"]
cities_that_are_special.append("Escondido")
for city in cities_that_are_special :
print(city)
# exercise: list iteration with enumerate() ---------------------------
"""
* Make a list of 3-5 ranked favorite movies, books, songs, or something. Print
* each one on a line with the number next to it.
"""
header("ranked favorite movies")
fav_movies = ["the prestige", "fight club", "the prophecy"]
for num, movie in enumerate(fav_movies):
print(f"#{num+1}:", movie)
# sean
# ----
header("sean's ranked favorite movies")
fav_movies = ["almost famous", "apocalypse now", "persepolis", "infinity war"]
for num, movie in enumerate(fav_movies):
print(f"#{num+1}:", movie)
# jayson
# ------
header("jayson's ranked favorite movies")
fav_seanisms = ["I'd do things that make god and robots cry", "goodbye forever", "See you in hell", "too late now asshole"]
for rank, seanism in enumerate(fav_seanisms):
print(f"#{rank+1}:", seanism)
#period combination of functions and variables? """Carry on my wayward son"""
# converting strings <--> lists ---------------------------------------
# convert a string into a list of characters
chars = list("abc") # ['a', 'b', 'c']
# split a string on whitespace
words = "red green blue".split() # ['red', 'green', 'blue']
# split a string on another delim
url = "github.com/alissa-huskey/python-class"
url_parts = url.split("/") # ['github.com', 'alissa-huskey', 'python-class']
# list to string
chars = ['H', 'e', 'l', 'l', 'o']
word = "".join(chars) # "Hello"
# list to string with delim between elements
fileparts = ['~', 'python-class', 'README.md']
path = "/".join(chars) # ~/python-class/README.md
# example: converting strings <--> lists ------------------------------
header("strings to lists: list(<string>)")
"""strings to lists: convert strings to list of characters"""
name_str = "alissa"
name_list = list(name_str)
name_list[0] = name_list[0].upper()
name = "".join(name_list)
print(f"{name_str} -> {name}")
"""
strings to lists: split a string into a list of words (split by whitespace)
ie: Make Sean Shakespearian
"""
header("strings to lists: <string>.split([<delim>])")
menu_items = "open print quit".split()
print(menu_items)
header('strings <--> lists: <string>.split([<delim>]) ; "<delim>".join(<list>)')
sentence = "I'd do things that make god and robots cry"
words = sentence.split(" ")
words.sort(reverse=True)
new_order = " ".join(words)
print(new_order)
# exercise: strings <--> lists ----------------------------------------
"""
1. Start with a string, print it
2. Convert it to a list, print it
3. Change the list somehow, print it
4. Turn it back into a string, print it
"""
header("sean's string->list->string")
varname = list("my body left for the summer")
varname.sort()
print(varname)
mystr = "werds are for nerds"
nerdy = mystr.split()
nerdy.sort()
print(nerdy)
sentence = "+".join(nerdy)
print(sentence)
header("jayson's string->list->string")
myswellvar = "golly gee wilikers Mr. Peabody!"
myswellvar = myswellvar.split()
myswellvar.sort()
print(myswellvar)
childabuse = " ".join(myswellvar)
print(childabuse)
print()
# rabbit trail: method chaining ---------------------------------------
header("rabbit trail: method chaining")
# val = input("tell me! ").lower()
# val = input("tell me! ").lower().strip().sub('.txt', '').center("100")
# print(val)
# rabbit trail: list.sort() -------------------------------------------
# this will NOT work because sort() returns None
wrong_letters = list("abcde").sort()
# this will work
letters = list("abcde")
letters.sort() # returns None, changes the order of letters
print(letters)