Python
Matches the end of the line.
$
Are added to a regular expression, they are ignored for the purpose of matching, but allow you to extract a particular subset of the matched string rather than the whole string when using findall().
( )
print(t[1:3])
('b', 'c')
t = tuple('lupins') >>> print(t)
('l', 'u', 'p', 'i', 'n', 's')
Applies to the immediately preceding character(s) and indicates to match zero or more times.
*
Applies to the immediately preceding character(s) and indicates to match zero or more times in "non-greedy mode".
*?
Applies to the immediately preceding character(s) and indicates to match one or more times.
+
Applies to the immediately preceding character(s) and indicates to match one or more times in "non-greedy mode".
+?
Matches any character (a wildcard).
.
Dictionaries have a method called get that takes a key and a default value. If the key appears in the dictionary get returns the corresponding value; otherwise it returns the default value. Which one is the example? >>> counts = 'chuck' : 1 , 'annie' : 42, 'jan': 100 >>> print(counts.get('jan', 0)) 100 >>> print(counts.get('tim', 0)) 0 >>> counts = { 'chuck' : 1 , 'annie' : 42, 'jan': 100} >>> print(counts('jan', 0)) 100 >>> print(counts.get('tim', 0)) 0 >>> counts = { 'chuck' : 1 , 'annie' : 42, 'jan': 100} >>> print(counts.get('jan', 0)) 100 >>> print(counts.get('tim', 0)) 0 >>> counts = {'chuck' : 1 , 'annie' : 42, 'jan': 100} >>> print(counts.get('jan', 100)) 100 >>> print(counts.get('tim', 0)) 100
>>> counts = { 'chuck' : 1 , 'annie' : 42, 'jan': 100} >>> print(counts.get('jan', 0)) 100 >>> print(counts.get('tim', 0)) 0
Dictionaries have a method called items that returns a list of tuples, where each tuple is a key-value pair:
>>> d = {'a':10, 'b':1, 'c':22} >>> t = list(d.items()) >>> print(t) [('b', 1), ('a', 10), ('c', 22)]
If the key isn't in the dictionary, you get an exception: >>> print(eng2sp['four']) KeyError: 'four' >>> print(eng2sp['four']) {'four'} >>> print(eng2sp['four']) '4'
>>> print(eng2sp['four']) KeyError: 'four'
m = [ 'have', 'fun' ] >>> (x, y) = m
>>> x 'have' >>> y 'fun' >>>
Applies to the immediately preceding character(s) and indicates to match zero or one time.
?
Applies to the immediately preceding character(s) and indicates to match zero or one time in "non-greedy mode"
??
dictionary
A mapping from a set of keys to their corresponding values.
Assume that t= (1,2,10,5), which of the following statements changes the value of the last element in the tuple 't' from 5 to 100? a- t(4) = 100 b- t[4] = 100 c- t[3] = 100 d- none of the above
The answer is d.
You can specify ranges of characters using the minus sign. This example is a single character that must be a lowercase letter or a digit.
[a-z0-9]
Matches a single character as long as that character is in the specified set. In this example, it would match "a", "e", "i", "o", or "u", but no other characters.
[aeiou]
When the first character in the set notation is a caret, it inverts the logic. This example matches a single character that is anything other than an uppercase or lowercase letter.
[ˆA-Za-z]
Matches the empty string, but not at the start or end of a word.
\B
Matches any non-digit character; equivalent to the set [ˆ0-9].
\D
Matches a non-whitespace character (opposite of \s).
\S
Matches the empty string, but only at the start or end of a word.
\b
Matches any decimal digit; equivalent to the set [0-9].
\d
Matches a whitespace character.
\s
Matches the beginning of the line.
^
line = 'Python has a powerful text processing capabilities' pattern = input('Enter a pattern: ') pattern = pattern.rstrip() result = re.findall(pattern, line) print (result) a- .+ b- \b\w\w\w\b c- \S+c\S+ d- \s+\w{4}\s+
a- ['Python has powerful text processing capabilities'] b- ['has'] c- ['processing'] d- [' text ']
More generally, the right side can be any kind of sequence (string, list, or tuple). For example, to split an email address into a user name and a domain, you could write?
addr = '[email protected]' >>> uname, domain = addr.split('@') >>> print(uname) monty >>> print(domain) python.org
Assume the following line of text: 'Hello from [email protected]', which regular expression below allows us to extract the domain name from the email address in the above text using the re.findall () function? a- \d@\d b- \S+@(\S+) c- .+ d- (@.+)
b- \S+@(\S+)
days_in_months = {'Jan' : 31, 'Feb' : 28, 'Mar' : 31, 'Apr' : 30, 'May' : 31, 'Jun' : 30, 'Jul' : 31, 'Aug' :31, 'Sep' : 30, 'Oct' : 31, 'Nov' : 30, 'Dec' : 31}; Which of the following changes the number of days in Oct. to 30? a- days_in_months[Oct] = 30 b- days_in_months['Oct'] = 30 c- Oct=days_in_months[30] d- change (days_in_months['Oct'],30)
b- days_in_months['Oct'] = 30
What is the output of the following print statement? print (len ((1 , (2,3), 'b'))) a- 1,2,3,'b' b- 1 c- 3 d-4
c- 3
What type of strings match the following regular expression: '^s\S+m$' a- any string that starts with s and ends with m b- any string that has at least one s and one m in it c- any string that begins with s and ends with m and excludes whitespaces d- any string that begins with a cart sign (^) and ends with a dollar sign ($)
c- any string that begins with s and ends with m and excludes whitespaces
days_in_months = {'Jan' : 31, 'Feb' : 28, 'Mar' : 31, 'Apr' : 30, 'May' : 31, 'Jun' : 30, 'Jul' : 31, 'Aug' :31, 'Sep' : 30, 'Oct' : 31, 'Nov' : 30, 'Dec' : 31}; I- Which of the following outputs the number of key-value pairs in 'days_in_months'? a- print(size(days_in_months)) b- print(len('days_in_months')) c- print (len(days_in_months)) d- print (days_in_months)
c- print (len(days_in_months))
Which of the following is not a tuple? a- t1=tuple() b- t1=('a' , 12) c- t1=(1) d- t1=()
c- t1=(1)
days_in_months = {'Jan' : 31, 'Feb' : 28, 'Mar' : 31, 'Apr' : 30, 'May' : 31, 'Jun' : 30, 'Jul' : 31, 'Aug' :31, 'Sep' : 30, 'Oct' : 31, 'Nov' : 30, 'Dec' : 31}; - Write a code segment to determine how many months in 'days_in_months' have 31 days?
count = 0; for month in days_in_months: if days_in_months[month] == 31: count = count + 1 print (count)
For example if we wanted to find all the entries in a dictionary with a value above ten, we could write what code?
counts = { 'chuck' : 1 , 'annie' : 42, 'jan': 100} for key in counts: if counts[key] > 10 : print(key, counts[key]) output: jan 100 annie 42
If you want to print the keys in alphabetical order, in the dictionary what should you use?
counts = { 'chuck' : 1 , 'annie' : 42, 'jan': 100} lst = list(counts.keys()) print(lst) lst.sort() for key in lst: print(key, counts[key]) output: ['jan', 'chuck', 'annie'] annie 42 chuck 1 jan 100
In our else statement, we use the more compact alternative for incrementing a variable. fname = input('Enter the file name: ') try: fhand = open(fname) except: print('File cannot be opened:', fname) exit() counts = dict() for line in fhand: words = line.split() for word in words: if word not in counts: counts[word] = 1 else: counts[word] += 1 print(counts)
counts[word] += 1 is equivalent to counts[word] = counts[word] + 1. Either method can be used to change the value of a variable by any desired amount. Similar alternatives exist for -=, *=, and /=.
- What is the output of the following script? t= (1,2,10,5) print (t[1:3]) a- 1,2,10 b- (1,2,10) c- 2,10 d- (2,10)
d- (2,10)
days_in_months = {'Jan' : 31, 'Feb' : 28, 'Mar' : 31, 'Apr' : 30, 'May' : 31, 'Jun' : 30, 'Jul' : 31, 'Aug' :31, 'Sep' : 30, 'Oct' : 31, 'Nov' : 30, 'Dec' : 31}; Which of the following deletes the key-value pair for the month of December from 'days_in_months' a- del days_in_months['Dec',31] b- remove days_in_months['Dec'] c- del days_in_months [31, 'Dec'] d- del days_in_months ['Dec']
d- del days_in_months ['Dec']
represent an empty dictionary () [] {}
eng2sp = dict() >>> print(eng2sp) {}
This output format is also an input format. For example, you can create a new dictionary with three items. But if you print eng2sp, you might be surprised: eng2sp = ('one': 'uno', 'two': 'dos', 'three': 'tres') >>> print(eng2sp) {'one': 'uno', 'three': 'tres', 'two': 'dos'} eng2sp = 'one': 'uno', 'two': 'dos', 'three': 'tres' >>> print(eng2sp) 'one': 'uno', 'three': 'tres', 'two': 'dos' eng2sp = {'one': 'uno', 'two': 'dos', 'three': 'tres'} >>> print(eng2sp) {'one': 'uno', 'three': 'tres', 'two': 'dos'}
eng2sp = {'one': 'uno', 'two': 'dos', 'three': 'tres'} >>> print(eng2sp) {'one': 'uno', 'three': 'tres', 'two': 'dos'}
a key-value pair with a colon between the key and value: eng2sp['one'] = 'uno' eng2sp('one') = 'uno' uno = eng2sp['one']
eng2sp['one'] = 'uno'
We will write a Python program to read through the lines of the file, break each line into a list of words, and then loop through each of the words in the line and count each word using a dictionary. What is the example? fname = ('Enter the file name: ') try: fhand = open(fname) except: print('File cannot be opened:', fname) exit() counts = dict() for line in fhand: words = line.split() if word not in counts: counts[word] = 1 else: counts[word] += 1 print(counts) fname = input('Enter the file name: ') try: fhand = open(fname) except: print('File cannot be opened:', fname) exit() counts = dict() for line in fhand: words = line.split() for word in words: if word counts: counts[word] = 1 else: counts[word] += 1 print(counts) fname = input['Enter the file name: '] try: fhand = open(fname) except: print('File cannot be opened:', fname) exit() counts = dict() for line in fhand: words = line.split() for word in words: if word not in counts: counts(word) = 1 else: counts(word) += 1 print(counts) fname = input('Enter the file name: ') try: fhand = open(fname) except: print('File cannot be opened:', fname) exit() counts = dict() for line in fhand: words = line.split() for word in words: if word not in counts: counts[word] = 1 else: counts[word] += 1 print(counts)
file: But soft what light through yonder window breaks It is the east and Juliet is the sun Arise fair sun and kill the envious moon Who is already sick and pale with grief fname = input('Enter the file name: ') try: fhand = open(fname) except: print('File cannot be opened:', fname) exit() counts = dict() for line in fhand: words = line.split() for word in words: if word not in counts: counts[word] = 1 else: counts[word] += 1 print(counts) output python count1.py Enter the file name: romeo.txt {'and': 3, 'envious': 1, 'already': 1, 'fair': 1, 'is': 3, 'through': 1, 'pale': 1, 'yonder': 1, 'what': 1, 'sun': 2, 'Who': 1, 'But': 1, 'moon': 1, 'window': 1, 'sick': 1, 'east': 1, 'breaks': 1, 'grief': 1, 'with': 1, 'light': 1, 'It': 1, 'Arise': 1, 'kill': 1, 'the': 3, 'soft': 1, 'Juliet': 1}
days_in_months = {'Jan' : 31, 'Feb' : 28, 'Mar' : 31, 'Apr' : 30, 'May' : 31, 'Jun' : 30, 'Jul' : 31, 'Aug' :31, 'Sep' : 30, 'Oct' : 31, 'Nov' : 30, 'Dec' : 31}; Write a code segment that prints out the name of every month that has 30 days, separate the names from each other with a tab (need a loop for this task).
for key in days_in_months: if days_in_months[key] == 30: print (key , end = '\t') print()
# Search for lines that start with 'F', followed by # 2 characters, followed by 'm:'
import re hand = open('mbox-short.txt') for line in hand: line = line.rstrip() if re.search('^F..m:', line): print(line)
# Search for lines that start with From and have an at sign
import re hand = open('mbox-short.txt') for line in hand: line = line.rstrip() if re.search('^From:.+@', line): print(line)
- Write a script that uses Python regex to print out the sum of all the numbers in the following string: 'The sum of numbers from 1 through 10 is 55'.
import re line = 'The sum of numbers from 1 through 10 is 55' result = re.findall('\d+', line) total = 0 for number in result: total = total + int(number) print(total)
A dictionary
is like a list, but more general. In a list, the index positions have to be integers; in a dictionary, the indices can be (almost) any type.
Instead, you use the ? look up the corresponding values:
key eng2sp = {'one': 'uno', 'two': 'dos', 'three': 'tres'} >>> print(eng2sp['two']) 'dos'
output the table in ascending order of keys
keyList = list (fTable.keys()) # store all the keys into a list keyList.sort() #sort the list of keys for key in keyList: # output the frequency table print (key, '\t', fTable[key])
The translate is the most subtle of the methods. Here is the documentation for translate:
line.translate(str.maketrans(fromstr, tostr, deletestr)) Replace the characters in fromstr with the character in the same position in tostr and delete all characters that are in deletestr. The fromstr and tostr can be empty strings and the deletestr parameter can be omitted.
print out the dictionary elements ordered based on their values?
list1 = [] for key , value in list (people.items()): list1.append((value,key)) list1.sort() for k,v in list1: print (k, v) #print (list1)
# print out every name (or key) and the corresponding age (or value)? list (people.keys()) for name in myNames: print (name , people[name]) myNames = list (people.keys()) for name in myNames: print (name , people[name]) myNames = list (people.keys) for name in myNames: print ( people[name]) myNames = list (people.keys()) for name in myNames: print [name , people[name]]
myNames = list (people.keys()) for name in myNames: print (name , people[name])
How can you create a list containing all the keys, and then print out the keys in ascending order? people = {'Tom' : 42 , 'Ed' : 18 , 'Cathy' : 2 , 'John' : 2} list (people.keys()) print (myNames) myNames.sort() print (myNames) myNames = list (people.keys()) print (myNames) myNames.sort[] print (myNames) myNames = list (people.keys()) print (myNames) sort() print (myNames) myNames = list (people.keys()) print (myNames) myNames.sort() print (myNames)
myNames = list (people.keys()) print (myNames) myNames.sort() print (myNames)
days_in_months = {'Jan' : 31, 'Feb' : 28, 'Mar' : 31, 'Apr' : 30, 'May' : 31, 'Jun' : 30, 'Jul' : 31, 'Aug' :31, 'Sep' : 30, 'Oct' : 31, 'Nov' : 30, 'Dec' : 31}; VII- Write a code segment that prints out the key-value pairs in 'days_in_months' in alphabetical order, each pair on a different line.
names = list(days_in_months.keys()) names.sort() for name in names: print ('%-10s%d' % (name , days_in_months[name]))
How to iterate through the dictionary and print keys one per line create a list containing all the keys? people = {'Tom' : 42 , 'Ed' : 18 , 'Cathy' : 2 , 'John' : 2} for item in people: print (item) names = [] for item in people: names.append(item) print (names) people = {'Tom' : 42 , 'Ed' : 18 , 'Cathy' : 2 , 'John' : 2} for item in people print (item) names = [] for item in people: names{item} print (names) people = {'Tom' : 42 , 'Ed' : 18 , 'Cathy' : 2 , 'John' : 2} for item in people: print (item) names = ['0',] for item in people: names.append(item) print [names] people = ('Tom' : 42 , 'Ed' : 18 , 'Cathy' : 2 , 'John' : 2) for item in print (item) names = () for item in people: names.append(item) print (names)
people = {'Tom' : 42 , 'Ed' : 18 , 'Cathy' : 2 , 'John' : 2} for item in people: print (item) names = [] for item in people: names.append(item) print (names)
days_in_months = {'Jan' : 31, 'Feb' : 28, 'Mar' : 31, 'Apr' : 30, 'May' : 31, 'Jun' : 30, 'Jul' : 31, 'Aug' :31, 'Sep' : 30, 'Oct' : 31, 'Nov' : 30, 'Dec' : 31}; Write a print-statement that prints out the number of days in 'Sep'
print (days_in_months['Sep'])
read data from an input file and find out the frequency of
read data from an input file and find out the frequency of the words in that file. file = input ("Enter a file name: ") while True: try: fhd = open (file, 'r') break except: file =input ("can't open the file, Try again: ")
t = ('A',) + t[1:] >>> print(t)? You can't modify the elements of a tuple, but you can?
replace one tuple with another: ('A', 'b', 'c', 'd', 'e')
The in operator works on dictionaries; it tells you whether? >>> vals = list(eng2sp.values()) >>> vals in 'uno' True >>> vals = list(eng2sp.values()) >>> 'uno' in list True >>> vals = list(eng2sp.values()) >>> 'uno' in vals True
something appears as a key in the dictionary (appearing as a value is not good enough). >>> 'one' in eng2sp True >>> 'uno' in eng2sp False >>> vals = list(eng2sp.values()) >>> 'uno' in vals True
t = ('a', 'b', 'c', 'd', 'e') >>> print(t[0])
t = ('a', 'b', 'c', 'd', 'e') >>> print(t[0])
The len function works on dictionaries; it returns? the number of keys: the number of key-value pairs: the number of values:
the number of key-value pairs: >>> len(eng2sp) 3
- Assume that t=(1,2,10,5,32,18,75), Write a script that prints out the average of all the numbers in the tuple 't' to the nearest 10th. total = 0 for number in t: total = total + number print ('%.1f' % (total/len(t))) total = 0 for number in p: total = total + number print ('%.1f' % (total(t))) total = 1 for number in t: total = total + number print ('%.1f' (total/len(t))) total = 0 for number in t: total = total + 1 print ('%.1f' % (total/len(t)
total = 0 for number in t: total = total + number print ('%.1f' % (total/len(t)))
create tuples, and nested tuples?
tuple5 = ('a' , ('b' ,'c', 'd') , 'x')
we can create a new tuple and assign that tuple to the same variable
tuple5 = (1,) + tuple5[1:] print (tuple5)
Which is a list of words and you want to sort them from longest to shortest? txt = 'but soft what light in yonder window breaks' words = txt.split() t = list() for word in words t.append((len(), word)) t.sort(reverse=) res = list() for length, word in t: res.append(word) print(res) txt = 'but soft what light in yonder window breaks' words = txt() t = list() for word in words: t.append((len(word), word)) t.sort() res = list() for length, word in t: res.append(word) print(res) txt = 'but soft what light in yonder window breaks' words = txt.split() t = list() for word in words t.append((len(word), word)) t.sort(reverse=True()) res = list() for length, word in t: res(word) print(res) txt = 'but soft what light in yonder window breaks' words = txt.split() t = list() for word in words: t.append((len(word), word)) t.sort(reverse=True) res = list() for length, word in t: res.append(word) print(res)
txt = 'but soft what light in yonder window breaks' words = txt.split() t = list() for word in words: t.append((len(word), word)) t.sort(reverse=True) res = list() for length, word in t: res.append(word) print(res) output: ['yonder', 'window', 'breaks', 'light', 'what', 'soft', 'but', 'in']
How do you count how many times each letter appears. There are several ways you could do it? word = 'brontosaurus' d = dict() for c in word: if c not in d: d[c] = 1 else: d[c] = d[c] + 1 print(d) word = 'brontosaurus' d = dict() for c in word: if c not in d: d[c] = 1 print(d) word = 'brontosaurus' d = "" for c in word: if c not in d: d[c] = 1 else: d[c] = d[c] + 1 print(d)
word = 'brontosaurus' d = dict() for c in word: if c not in d: d[c] = 1 else: d[c] = d[c] + 1 print(d) output {'a': 1, 'b': 1, 'o': 2, 'n': 1, 's': 2, 'r': 2, 'u': 2, 't': 1}
read the entire file into a string (usually we read the file line-by-line and not this way!)
words = fhd.read().split() fTable = {} for word in words: if word in fTable: fTable[word] += 1 else: fTable[word] = 1 print (fTable)