tally: arcane, grim, esoteric, corpus, persnickety, menagerie

Lists: a review


In [1]:
x = [5, 10, 15, 20, 25, 30]

In [2]:
x[3]


Out[2]:
20

In [3]:
[2, 4, 6, 8, 10][4]


Out[3]:
10

In [4]:
x[90]


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-4-1306804ab952> in <module>()
----> 1 x[90]

IndexError: list index out of range

In [5]:
type(x)


Out[5]:
list

In [6]:
type(x[0])


Out[6]:
int

In [7]:
len(x)


Out[7]:
6

In [8]:
len([10])


Out[8]:
1

In [9]:
type([10])


Out[9]:
list

In [10]:
len([])


Out[10]:
0

In [12]:
x


Out[12]:
[5, 10, 15, 20, 25, 30]

In [13]:
max(x)


Out[13]:
30

In [14]:
min(x)


Out[14]:
5

In [15]:
sum(x)


Out[15]:
105

In [16]:
sorted(x)
#takes a list and returns it sorted in numerical or alphabetical order


Out[16]:
[5, 10, 15, 20, 25, 30]

In [17]:
sorted([17, -4, 1004, 3, 15, 8.3])


Out[17]:
[-4, 3, 8.3, 15, 17, 1004]

In [18]:
sorted(["badger", "crocodile", "aardvark", "zebra", "emu"])


Out[18]:
['aardvark', 'badger', 'crocodile', 'emu', 'zebra']

In [19]:
range(10)


Out[19]:
range(0, 10)

In [20]:
list(range(10))


Out[20]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [21]:
list("this is a test")


Out[21]:
['t', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 't', 'e', 's', 't']

using expressions as indices


In [25]:
x[3]


Out[25]:
20

In [27]:
n = 1 + 2
x[n]


Out[27]:
20

In [28]:
x[(4 + 2)//3]
#side note what is thaaaaat


Out[28]:
15

Negative indices


In [22]:
x[-1]


Out[22]:
30

In [23]:
x[-2]


Out[23]:
25

In [24]:
x[-3]


Out[24]:
20

In [29]:
x[-90]


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-29-054a10529b6b> in <module>()
----> 1 x[-90]

IndexError: list index out of range

List slices


In [30]:
x


Out[30]:
[5, 10, 15, 20, 25, 30]

In [31]:
# _xprlist[__exprint_:__exprint__]

In [33]:
x[1:4]


Out[33]:
[10, 15, 20]

In [34]:
x[2:5]


Out[34]:
[15, 20, 25]

In [35]:
n = 2
x[n:n+3]


Out[35]:
[15, 20, 25]

In [36]:
x[-3:-1]


Out[36]:
[20, 25]

In [37]:
x[3:9000]
#just givesyou what's included, no indicator you're way off base


Out[37]:
[20, 25, 30]

In [38]:
type(x[1:4])


Out[38]:
list

In [39]:
for item in x[2:5]:
    print(item)


15
20
25

omitting slice values


In [40]:
x[0:4]


Out[40]:
[5, 10, 15, 20]

In [41]:
x[:4]
#at beginning, is zero


Out[41]:
[5, 10, 15, 20]

In [42]:
x[4:]
#at end, gives you to the end


Out[42]:
[25, 30]

In [43]:
x[-3:]


Out[43]:
[20, 25, 30]

In [44]:
x[-2:]


Out[44]:
[25, 30]

In [45]:
x[:-1]


Out[45]:
[5, 10, 15, 20, 25]

In [46]:
x[1:]


Out[46]:
[10, 15, 20, 25, 30]

List comprehensions


In [47]:
#very common to take a list, apply some transformation to it to get another list
#another thing that's very common is to take a list, filter it, and end up with another list

In [48]:
x


Out[48]:
[5, 10, 15, 20, 25, 30]

In [53]:
source = [3, -1, 4, -2, 5, -3, 6]
dest = []
for item in source:
    if item > 0:
        dest.append(item * item)
dest


Out[53]:
[9, 16, 25, 36]

In [54]:
[item * item for item in source if item > 0]


Out[54]:
[9, 16, 25, 36]

In [56]:
ark = ["aardvark", "badger", "crocodile", "dingo", "emu", "flamingo"]
zoo = []
for item in ark:
    if len(item) <= 6:
        zoo.append(item)
zoo


Out[56]:
['badger', 'dingo', 'emu']

In [57]:
[item for item in ark if len(item) <= 6]


Out[57]:
['badger', 'dingo', 'emu']

In [58]:
#[_expr_ for _temp_ in _source_ if _expression_ ]
#if... is optional

In [59]:
x


Out[59]:
[5, 10, 15, 20, 25, 30]

In [60]:
#what i want is: [2, 7, 12, 17, 22, 27]
stuff = []
for item in x:
    stuff.append(item - 3)
stuff


Out[60]:
[2, 7, 12, 17, 22, 27]

In [61]:
[item - 3 for item in x]


Out[61]:
[2, 7, 12, 17, 22, 27]

In [62]:
source = [3, -1, 4, -2, 5, -3, 6]

In [63]:
# modulo operator: %
#gives you remainder, basically
#can be used to determine if a number is odd/even, just modulo 2 ya know
60 % 5


Out[63]:
0

In [64]:
60 % 7


Out[64]:
4

In [65]:
dest = []
for i in range(10):
    if i % 2 == 0:
        dest.append(i*i)
dest


Out[65]:
[0, 4, 16, 36, 64]

In [67]:
[i*i for i in range(10) if i % 2 == 0]


Out[67]:
[0, 4, 16, 36, 64]

In [68]:
#in python any number other than zero is considered to be True

a slightly more practical example


In [69]:
rawdata = "2,3,5,7,11,13,17,19,23"
rawdata.split(",")


Out[69]:
['2', '3', '5', '7', '11', '13', '17', '19', '23']

In [70]:
values = rawdata.split(",")

In [71]:
sum(values)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-71-b545fc186eaf> in <module>()
----> 1 sum(values)

TypeError: unsupported operand type(s) for +: 'int' and 'str'

In [72]:
#that doesn't work

In [73]:
int("17")


Out[73]:
17

In [74]:
srcstr = "1237"
type(srcstr)


Out[74]:
str

In [75]:
converted = int(srcstr)
type(converted)


Out[75]:
int

In [76]:
#int: str -> int() -> int
int("-17")


Out[76]:
-17

In [77]:
values


Out[77]:
['2', '3', '5', '7', '11', '13', '17', '19', '23']

In [78]:
int(values)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-78-988e4db7b648> in <module>()
----> 1 int(values)

TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

In [81]:
#it does not work!
num_values = [int(i) for i in values]
#values is source list
#predicate expression is calling the function on the thing

In [82]:
sum(num_values)


Out[82]:
100

In [84]:
sum([int(i) for i in rawdata.split(",")])


Out[84]:
100

In [85]:
stuff = "hello there how are you"

In [86]:
stuff.split(" ")
#split divides things into a list based on what the divider is


Out[86]:
['hello', 'there', 'how', 'are', 'you']

In [87]:
stuff.split("e")


Out[87]:
['h', 'llo th', 'r', ' how ar', ' you']

In [88]:
for i in stuff.split("e"):
    print(i)


h
llo th
r
 how ar
 you

string operations

the in operator


In [89]:
"foo" in "buffoon"


Out[89]:
True

In [90]:
#if the string on the left is a substring of the string on the right
"foo" in "reginald"


Out[90]:
False

In [91]:
ark


Out[91]:
['aardvark', 'badger', 'crocodile', 'dingo', 'emu', 'flamingo']

In [92]:
[animal for animal in ark if 'a' in animal]


Out[92]:
['aardvark', 'badger', 'flamingo']

In [93]:
for animal in ark:
    if 'a' in animal:
        print(animal)


aardvark
badger
flamingo

In [94]:
check = "foodie"

In [95]:
check.startswith("foo")


Out[95]:
True

In [96]:
check.startswith("f")


Out[96]:
True

In [97]:
check.startswith("blah")


Out[97]:
False

In [98]:
check.startswith("Foo")
#case-sensitive, FYI


Out[98]:
False

In [99]:
check.isdigit()
#will say True if it is entirely numeric


Out[99]:
False

In [100]:
number_str = "12345"

In [101]:
number_str.isdigit()


Out[101]:
True

In [102]:
number_str = "12,345"
number_str.isdigit()


Out[102]:
False

In [103]:
number_str = "123.45"
number_str.isdigit()


Out[103]:
False

In [104]:
#.islower() .isupper()
check.islower()


Out[104]:
True

In [105]:
check.isupper()


Out[105]:
False

In [106]:
yelling = "I LIKE THINGS AND THYE ARE GOOD"

In [107]:
yelling.isupper()


Out[107]:
True

String indexes and slices


In [112]:
message = "bungalow"
message[0]


Out[112]:
'b'

In [113]:
message[-1]


Out[113]:
'w'

In [114]:
message[3:-2]


Out[114]:
'gal'

Finding substrings


In [108]:
src = "Now is the winter of our discontent"

In [109]:
src.find("win")


Out[109]:
11

In [110]:
src.find("lose")


Out[110]:
-1

In [111]:
location = src.find("win")
src[location:]


Out[111]:
'winter of our discontent'

In [115]:
src[-1]


Out[115]:
't'

In [116]:
location = src.find("z")
if location != -1:
    print(src[location:location+2])

In [117]:
location = src.find("z")
print(location)
print(src[location:location+2])


-1


In [118]:
src


Out[118]:
'Now is the winter of our discontent'

In [119]:
src.count("is")
#count takes a substring and counts how many it appears in the string


Out[119]:
2

In [122]:
src.count(" is ")


Out[122]:
1

In [124]:
my_is_patterns = ["is", " is ", "is ", " is", "Is"]
for item in my_is_patterns:
    print(item, src.count(item))


is 2
 is  1
is  1
 is 1
Is 0

In [121]:
for vowel in ['a', 'e', 'i', 'o', 'u']:
    print(vowel, src.count(vowel))


a 0
e 3
i 3
o 4
u 1

In [125]:
src2 = "Someone tell me where the poetry is."
src3 = "is this really all the poetry you have?"

String transformations


In [126]:
comment = "ARGUMENTATION! DISAGREEMENT! STRIFE!"

In [127]:
comment.lower()


Out[127]:
'argumentation! disagreement! strife!'

In [128]:
comment


Out[128]:
'ARGUMENTATION! DISAGREEMENT! STRIFE!'

In [129]:
message = "e.e. cummings is not happy about this"

In [130]:
message.upper()


Out[130]:
'E.E. CUMMINGS IS NOT HAPPY ABOUT THIS'

In [131]:
str1 = "dog"
str2 = "Dog"
#so say i wanted to compare these but not case-sensitively

In [132]:
str1 == str2


Out[132]:
False

In [133]:
str1.lower() == str2.lower()


Out[133]:
True

In [134]:
movie = "dr. strangelove, or, how i learned to stop worrying and love the bomb"

In [135]:
movie.title()


Out[135]:
'Dr. Strangelove, Or, How I Learned To Stop Worrying And Love The Bomb'

In [136]:
movie2 = "rosemary's baby" 
#you would think, "Rosemary's Baby"
movie2.title()
#but no


Out[136]:
"Rosemary'S Baby"

In [137]:
rawtext = "      weird extra spaces before and after      "

In [138]:
rawtext.strip()


Out[138]:
'weird extra spaces before and after'

In [139]:
line = "hello there this is a line of text\n"
#\n is NEW LINE

In [140]:
print(line)


hello there this is a line of text


In [141]:
print(line.strip())


hello there this is a line of text

In [142]:
song = "I got rhythm, I got music, I got my man, who could ask for anything more"

In [148]:
step1 = song.replace("I got", "I used to have")
#turning it into a COUNTRY SONG
print(step1)


I used to have rhythm, I used to have music, I used to have my man, who could ask for anything more

In [149]:
step2 = step1.replace("more", "less")
print(step2)


I used to have rhythm, I used to have music, I used to have my man, who could ask for anything less

In [154]:
telegram = song.replace("I got", "someday I will have").replace("anything more", "a future more bright").replace(",", " STOP").upper() + " STOP"

In [155]:
print("original:", song)
print("much improved:", telegram)


original: I got rhythm, I got music, I got my man, who could ask for anything more
much improved: SOMEDAY I WILL HAVE RHYTHM STOP SOMEDAY I WILL HAVE MUSIC STOP SOMEDAY I WILL HAVE MY MAN STOP WHO COULD ASK FOR A FUTURE MORE BRIGHT STOP

In [144]:
song
#original stays the same eh


Out[144]:
'I got rhythm, I got music, I got my man, who could ask for anything more'

In [145]:
rawdata = "Get data that<br>looks like this<br>because it was"

In [146]:
print(rawdata)


Get data that<br>looks like this<br>because it was

In [147]:
print(rawdata.replace("<br>", "\n"))


Get data that
looks like this
because it was

A difficult problem


In [156]:
input_str = "Yes, y zip code is 12345. I heard that Gary's zip code is 23456. But 212 is not a zipc code."

In [158]:
print(input_str)


Yes, y zip code is 12345. I heard that Gary's zip code is 23456. But 212 is not a zipc code.

In [162]:
# results in: ["12345", "23456"]
current = ""
zips = []
for ch in input_str:
    if ch.isdigit():
        current += ch
    else:
        current = ""
    if len(current) == 5:
        zips.append(current)
        current = ""
zips


Out[162]:
['12345', '23456']

Regular expressions


In [163]:
import re
zips = re.findall(r"\d{5}", input_str)
zips
#that means finds five digits in a row


Out[163]:
['12345', '23456']

In [ ]: