In: Computer Science
Python Questions:
Q1. list_1= ['wind', 'spring', 'summer', 'purse', 'great', 'sports']
Use re.search() in the re package and list comprehension to find all words in list_1 that does not contain the letter "r".
Q2.
str_1= '''I live in a room by the sea,
where the view is great and the food is free.
Some of the tenants come and go.
Some I eat, if they're too slow.
One end of me is firmly locked.
The other end just gently rocks.
I live in a room by the sea.
It's perfect for an anemone. '''
Use re.search() in the re package to find all lines that does not
start with the letter "s", "S", or "I".
Q3. list_1= ['attitudes', 'ab\nchild', '2!apologies', 'echess',
'fly\n', 'cheer']
Use list comprehension and functions in re package to filter all
elements that start with "ch" or end with "es".
Q4. Remove everything from the first occurrence of "e" till end of the string, using the function .sub() from the "re" package
str_1 = 'Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.'
Before we start coding we have to know the basics of re
. (dot) represents any character except \n
^ represents starts with ex: '^h' will satisfy "helloworld"
$ represents ends with ex: 'i$' will satisfy "hii"
| represents or ex: '^h|o$' will satisfy for both "hi" and "hello"
* Zero or more occurrences
ex: '.*' will satisfy any string
here ->
So ( .* ) finally represents any number of
characters.
List comprehension in python :
Solutions
Q1
import re
list_1= ['wind', 'spring', 'summer', 'purse', 'great', 'sports']
print([i for i in list_1 if not re.search('r',i)])
[ i for i in list_1 if not re.search('r',i) ]
this statement is equivalent to
for word in list_1:
if not re.search('r',word): #if r is not found in word
print(word)
Hope you understand how list comprehension internally works!
Q2 Solution
import re
str_1= '''I live in a room by the sea,
where the view is great and the food is free.
Some of the tenants come and go.
Some I eat, if they're too slow.
One end of me is firmly locked.
The other end just gently rocks.
I live in a room by the sea.
It's perfect for an anemone. '''
print(*[line for line in str_1.split('\n') if not re.search('^S|^s|^I',line)])
#^s|^s|^I means if line starts with s or S or I
#str_1.split means total string is splitted into lines and matches with re
Q3 Solution
import re
list_1= ['attitudes', 'ab\nchild', '2!apologies', 'echess', 'fly\n', 'cheer']
print([word for word in list_1 if re.match('^ch|.*es$',word)])
#re will match word startswith ch ^ch | or ends with es .*es$
Q4 Solution
import re
str_1 = 'Two things are infinite: the universe and human stupidity; and I\'m not sure about the universe.'
print("".join([str_1[i] for i in range(re.search('e',str_1).start())]))
#start() method in re search will say the first position that e appears
#''.join will join all the characters obtained in the list comprehension
#we will traverse through str_1 and prints upto first appearance of character e
Thank You
post your doubts in comment section so that i can modify answer if
u need
please support by giving a thumbs up