Iterating through a string Using List Comprehension
When we run the program, the output will be:l = [ x for x in 'abcde' ] print( l)
['a', 'b', 'c', 'd', 'e']
Using if with List Comprehension
l=[x for x in range(25) if x%5==0]
print(l)
[0, 5, 10, 15, 20]
Nested IF with List Comprehension
l=[x for x in range(50) if x%5==0 if x%2==0] Here, list comprehension checks: Is x divisible by 5 or not Is x divisible by 2 or not If x satisfies both conditions, x is appended to list l.
if...else With List Comprehension
l=["Even" if x%2==0 else "odd" for x in range(10)]
print(l)
['Even', 'odd', 'Even', 'odd', 'Even', 'odd', 'Even', 'odd', 'Even', 'odd']-
Nested Loops in List Comprehension
o/pprefix = ['A', 'B', 'C'] suffix = ['a', 'b'] result = [val+" "+val2 for val in prefix for val2 in suffix ] print(result)
['A a', 'A b', 'B a', 'B b', 'C a', 'C b']
Python exercises
-
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
. Write one line of Python that takes this lista
and makes a new list that has only the even elements of this list in it.
find the list of age?given a list,years_of_birth = [1990, 1991, 1990, 1990, 1992, 1991]
- Find all of the numbers from 1-1000 that have a 3 in them?
- Count the number of spaces in a string
- Remove all of the vowels in a string
- Find all of the words in a string that are less than 4 letter.
- A list of all consonants in the sentence 'The quick brown fox jumped over the lazy dog'
- A list of all the capital letters (and not white space) in 'The Quick Brown Fox Jumped Over The Lazy Dog'
- A list of all square numbers formed by squaring the numbers from 1 to 1000.
- Create the list:
[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0] - return a list of the full path to items in a directory (hint, use os.listdir() and os.path.join())
- now extend the exercise11 to exclude directories (hint, use os.path.isdir())
- now extend exercise11 further to filter to only files ending in .jpg and .png
- Create a sample list of random integers with list comprehension and the
random
module. (2 lines max, import included) - Create a 5 x 5 matrix using a list of lists
nest_li = [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
Hint:[[output expression] for iterator variable in iterable] Note that here, the output expression is itself a list comprehension.
Challenge:
- Use a dictionary comprehension to count the length of each word in a sentence.
- Use a dictionary comprehension to build a map of every ascii character to its ordinal number chr : ord and update it with the reverse mapping ord : chr. Obvisouly the chr and ord functions and the string module will help. (3 lines max, import included)
- Use a nested list comprehension to find all of the numbers from 1-1000 that are divisible by any single digit besides 1 (2-9)
- For all the numbers 1-1000, use a nested list/dictionary comprehension to find the highest single digit any of the numbers is divisible by.
- Convert a list of dicts which you have to create with a list comprehension, all with an unique id to a dict of dicts with that id as key. (2 lines, one for the list comp, one for the dict comp)
No comments:
Post a Comment