- 7 Tem 2013
- 8,263
- 744
It's an easy way to construct lists.
The below examples shows how to add numbers from 0 to 10 into an array and there is nothing wrong with the following piece of code
Except, you can do it in one line only
The above special sytnax is a neat Python trick to include the elements of a container via a given condition or without any condition. Two important parts of the statement. The first one is; for i in range(10) our for loop. The second one [i ...] is our way to add elements into the array.
What if we would like o check if the number is even, do we need to rollback to the first version ? The answer is No
This time, we have 3 steps. First, for i in range(10) runs. Then our if i % 2 == 0 evaluated checking if the number modded to 2. Once the given condition is True the [i ...] part is run; the element is added into the end_result array.
Lets add one more step, and find the squares of the each of the even numbers.
Exactly the same run sequence from the previous example, with a additional evaluation with the [i**2] part where we find the square of the even number.
There are two inner loops in this example. Starting with for i in range(10), if the current element passes the condition if i % 2 == 0 then the second for loop starts to run for j in range(0,i) and as last the element is added to the list [j ...]
Usages
Lets abuse the list comprehensions ;
Search Highlighting
Given a text, replace all the occurences of a keyword with bold text, print the lines that contains the keyword and combine them via "..."
Analyze for which words
Let's say you want to write an algorithm that analyzes the words within sentences which contains the search keyword
The below examples shows how to add numbers from 0 to 10 into an array and there is nothing wrong with the following piece of code
Kod:
numbers = [] for i in range(10): numbers.append(i)
Kod:
>>> [i for i in range(10)] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
The above special sytnax is a neat Python trick to include the elements of a container via a given condition or without any condition. Two important parts of the statement. The first one is; for i in range(10) our for loop. The second one [i ...] is our way to add elements into the array.
What if we would like o check if the number is even, do we need to rollback to the first version ? The answer is No
Kod:
>>> even_numbers = [i for i in range(10) if i % 2 == 0] >>> even_numbers [0, 2, 4, 6, 8]
Lets add one more step, and find the squares of the each of the even numbers.
Kod:
>>> even_numbers_squares = [i**2 for i in range(10) if i % 2 == 0] >>> even_numbers_squares [0, 4, 16, 36, 64]
Kod:
>>> even_numbers_squares_counter = [j for i in range(10) if i % 2 == 0 for j in range(0,i) ] [0, 1, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 6, 7]
Kod:
[0, 1, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 6, 7]
Lets abuse the list comprehensions ;
Search Highlighting
Given a text, replace all the occurences of a keyword with bold text, print the lines that contains the keyword and combine them via "..."
Kod:
>>> text = "Python is a great language to work with. I like python a lot. The reasons are obvious; its simple and elegant. Great to read" >>> "...".join([sentence.lower().replace("python","<b>python</b>") for sentence in text.split(".") for word in sentence.split(" ") if 'python' in word.lower() ]) '<b>python</b> is a great language to work with... i like <b>python</b> a lot'
Let's say you want to write an algorithm that analyzes the words within sentences which contains the search keyword
Kod:
>>> [ (sent,word) for sent in text.split(".") for word in sent.split(" ") if len(word)> 2 and 'Python' in sent] [('Python is a great language to work with', 'Python'), ('Python is a great language to work with', 'great'), ('Python is a great language to work with', 'language'), ('Python is a great language to work with', 'work'), ('Python is a great language to work with', 'with')]