Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python Comprehension and Generators

Similar presentations


Presentation on theme: "Python Comprehension and Generators"— Presentation transcript:

1 Python Comprehension and Generators
Peter Wad Sackett

2 List comprehension 1 Creating new list with other list as basis
primes = [2, 3, 5, 7] doubleprimes = [2*x for x in primes] The same as doubleprimes = list() for x in primes: doubleprimes.append(2*x) Any expression (the part before the for loop) can be used. Example: A tab separated line of numbers are read from a file, convert the numbers from strings to floats. for line in datafile: numbers = [float(no) for no in line.split()] # Do something with the list of numbers

3 List comprehension 2 Filtering with comprehension – using if
odd = [no for no in range(20) if no % 2 == 1] numbers = [1, 3, -5, 7, -9, 2, -3, -1] positives = [no for no in numbers if no > 0] Nested for loops in comprehension Example: Creating all combinations in tuples of two numbers, where no number is repeated in a combination. combi = [(x, y) for x in range(10) for y in range(10) if x != y] Flatten a list of lists (matrix) into a simple list matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] flatList = [no for row in matrix for no in row] A list does not have to form the basis of the comprehension – any iterable will do, like sets or dicts.

4 Set and dict comprehension
It works like list comprehension, just use {} instead of [] Create a set of all codons nuc = [’A’, ’T’, ’C’, ’G’] codons = { x+y+z for x in nuc for y in nuc for z in nuc } Turning a dict inside out myDict = {'a': 1, 'b': 2, 'c': 3} reverseDict = {value:key for key, value in myDict.items()} Result: {1: 'a', 2: 'b', 3: 'c'} This only works if the values of the dictionary are immutable.

5 Examples # This function will find the common elements of any combination of list, set or dict. # The "highest" datatype will be returned, so list+list=>list, and list+set=>set, etc. def common(param1, param2): if not isinstance(param1, (list, set, dict)): raise TypeError("First parameter is not a list, set or dict") if not isinstance(param2, (list, set, dict)): # Useful internal functions def __commonlistset(plist, pset): return [ x for x in plist if x in pset ] def __commoncollectiondict(pcollection, pdict): return { x:pdict[x] for x in pcollection if x in pdict } # Main function code - simply figure out the combination of input if isinstance(param1, list): if isinstance(param2, list): return __commonlistset(param1, set(param2)) elif isinstance(param2, set): return set(__commonlistset(param1, param2)) else: return __commoncollectiondict(param1, param2) elif isinstance(param1, set): return set(__commonlistset(param2, param1)) return param1.intersection(param2) elif isinstance(param1, dict): return __commoncollectiondict(param2, param1)

6 Generators Generators are your own defined iterators, like range.
Generators look like functions, but they keep the state of their variables between calls, and they use yield instead of return. Also calling them again resumes execution after the yield statement. Generators deal with possibly memory issues as values are generated on the fly. Example: range(10) returns the numbers between 0 and 9, both inclusive, myrange(10) returns the numbers between 1 and 10. def myrange(number): result = 1 while result <= number: yield result result += 1 for i in myrange(10): print(i) More info:

7 Examples A generator for making a random gene. Parameters are the required min and max length of the gene. Start and stop codon included. import random def randomgene(minlength, maxlength): if minlength < 2 or minlength > maxlength: raise ValueError(’Wrong minlength and/or maxlength') # Give the initial start codon yield 'ATG' stopcodons = ('TGA', 'TAG', 'TAA') # Compute codons in gene minus start/stop codons codoncount = random.randrange(minlength, maxlength+1) - 2 while codoncount > 0: codon = ’’.join([random.choice('ATCG') for i in range(3)]) if codon not in stopcodons: yield codon codoncount -= 1 # Finally give a stop codon yield random.choice(stopcodons) # Finally using it print(''.join(randomgene(40,50))) for codon in randomgene(50,100): print(codon)


Download ppt "Python Comprehension and Generators"

Similar presentations


Ads by Google