Strings can be imagined as a sequence of array
#Indexing
planet = 'Pluto'
planet[0]
'P'
#Slicing
planet[-3:]
'Pet'
#How long
only (planets)
String methods
# Capitalization
claim = 'Pluto is a planet'
claim.upper()
#all lowercase
claim.lower()
#search for the first index of substring
claim.index('plan')
Going between strings and lists : .splits and .join()
words = claim.split()
datestr='1956-01-31'
year,month,day = datestr.split('-')
'/'.join ([month,day,year])
'/'.join([word.upper() for word in words])
The value is too non-string object, can be used to convert values str
str(position)
format placeholder
This is getting hard to read and annoying to type. str.format() to the rescue
"{}. you'll always be the{}th planet to me".format{planet,position}
Detecting whether a value tuple:
seq = ['one', 'two', 'three']
if 'one' in seq:
print True
Dictionraries
numbers={'one':1, 'two':2, 'three': 3}
numbers['one']
1
numbers['eleven']=11
numbers
Exercise 1:
A researcher has gathered thousands of news articles. But she wants to focus her attention on articles including a specific word. Complete the function below to help her filter her list of articles.
Your function should meet the following criteria
- Do not include documents where the keyword string shows up only as a part of a larger word. For example, if she were looking for the keyword “closed”, you would not include the string “enclosed.”
- She does not want you to distinguish upper case from lower case letters. So the phrase “Closed the case.” would be included when the keyword is “closed”
- Do not let periods or commas affect what is matched. “It is closed.” would be included when the keyword is “closed”. But you can assume there are no other types of punctuation.
Answer:
Solution:
def word_search(doc_list, keyword):
indices = []
for i,doc in enumerate(doc_list):
temp=doc.split()
word = [item.strip('.,').lower() for item in temp]
if keyword.lower() in word:
indices.append(i)
return indices