What do you think this will do?
It gives you a list, which contains a lowercased word for each item in the list 'words':
You can use a list comprehension to transform each item on a list
Try it for yourself: Write a function that takes as input a list of strings and returns a list of the same strings ,but with one space before and one space after each string:
Here is the solution:
Addressing punctuation (a first attempt)
Punctuation often gets in our way if we want to process words. For example, above we had the sentence "Pierre Vinken, 61 years old, will join the board as a nonexecutive director Nov. 29." (This sentence is famous among computational linguists because it is the first sentence on the Wall Street Journal corpus, which everyone uses as an example sentence.) If we split this sentence into words to process it, it will, among other things, contain "old.", which is unfortunately a different string from "old" and won't be counted as the same word. So we will often want to separate punctuation from the words.
But what is punctuation? Python has this:
Now how can we remove punctuation at the end of a word? We can use the Python string function rstrip():
If given no further argument, it removes all whitespace on the end of a string. if given an argument, for example "!?", it will remove all "!" and "?" at the end of the string.
Putting things together: We will use list comprehensions, string.punctuation, and rstrip() to remove punctuation.
Here's what this will do:
More uses of list comprehensions
What do you think this will do?
It only retains the members of mylist that are not stopwords. So you can use list comprehensions to filter lists:
So we have seen uses of list comprehensions that transform each item on the list, and uses that filter a list. You can do both at the same time. Here's an example task:
Given a list of numbers,
It gives you the "modulo". When you divide 5 by 3, the remainder is 2, so 5 % 3 = 2. When you divide 19 by 5, the remainder is 4, so 19 % 5 = 4. How can we use this to test whether a number is even or odd?
An even number modulo 2 is zero, an odd number modulo 2 is 1. Putting things together:
Next, we will make a function that takes an integer as input and returns a list of its digits. So we want
We will proceed as follows: Given a number myint,
So if you convert a string to a list, what you get is a list of the letters of the string.
Putting things together:
Can you make a function that takes a string as input and returns the reverse string?
aibohphobia is the fear of palindromes… See http://en.wiktionary.org/wiki/aibohphobia
Here are two possibilities of how to do this. The first uses your well-known idiom of starting with an empty container (here, new_string, a string), and filling it over the course of a for-loop. Note that if we iterate over a string, for letter in my_string, we iterate over the letters in the string. Each new letter is attached to the beginning of new_string, in order to invert the original string.
And here is another version, which iterates over the indices of my_string backwards:
Note the start and end point of the for-loop: We start at len(my_string) – 1, the index of the last letter. We stop before index – 1, that is, we stop at the first letter. And we proceed in steps of -1.