-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathExercise08_Functions_Python.py
More file actions
60 lines (50 loc) · 1.84 KB
/
Exercise08_Functions_Python.py
File metadata and controls
60 lines (50 loc) · 1.84 KB
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
#Exercise 8, Python, Useful Functions
#10/13/17, MMD
#Reading from files
var = open(filename,"r") #r appears to be optional, can at least read line by line with no r
line = line.strip() #strips \n
line = var.readline().strip() #advances to the next line, stripping \n
var.close() #closes file
#Writing to files
var = open(filename,"w")
var.write(string + "\n") #appends line to file, need to add the "\n"
var.close() #closes file
#initializes empty dictionary
var = {}
#accesses value associated with key in dictionary
var[key]
#returns T if testkey in dictionary (var), F if not
testkey in var
#initializes empty list
var = []
#appends to existing list
var.append(newvar)
#splits string on whitespace
var.split()
#for raw string input
r"string"
#joins two strings together
string1 + string2
#to convert list of numbers into data frame for plotnine
var = pandas.DataFrame(numpy.array(yourlist), columns = ["name"])
#You won't need all of these!
#can make a regex search faster, can then call other re functions on var by var.function
var = re.compile(regexString)
#excapes metacharacters, makes string literal
re.escape(regexString)
#searches for all matches, not just the first
re.findall(regexString,searchString)
#searches for pattern only at beginning of a string
re.match(regexString,searchString)
#searches for a pattern anywhere in string, returns a match object with several attributes
re.search(regexString,searchString)
#returns match group for match object var, 0 is full match
var.group(index)
#returns starting position of each match in match object var, 0 is full match
var.start(index)
#splits a string on a pattern
re.split(regexString,searchString)
#replaces regex match in search string with replacement
re.sub(regexString,replacement,searchString)
#same as above but also returns number of replacements made
re.subn(regexString,replacement,searchString)