Python Comprehension and Generators Peter Wad Sackett List

  • Slides: 7
Download presentation
Python Comprehension and Generators Peter Wad Sackett

Python Comprehension and Generators Peter Wad Sackett

List comprehension 1 Creating new list with other list as basis primes = [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 2 DTU Health Tech, Technical University of Denmark

List comprehension 2 Filtering with comprehension – using if odd = [no for no

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]] flat. List = [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. 3 DTU Health Tech, Technical University of Denmark

Set and dict comprehension It works like list comprehension, just use {} instead of

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 my. Dict = {'a': 1, 'b': 2, 'c': 3} reverse. Dict = {value: key for key, value in my. Dict. items()} Result: {1: 'a', 2: 'b', 3: 'c'} This only works if the values of the dictionary are immutable. 4 DTU Health Tech, Technical University of Denmark

Examples # This function will find the common elements of any combination of list,

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(param 1, param 2): if not isinstance(param 1, (list, set, dict)): raise Type. Error("First parameter is not a list, set or dict") if not isinstance(param 2, (list, set, dict)): raise Type. Error("First parameter is not a list, set or 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(param 1, list): if isinstance(param 2, list): return __commonlistset(param 1, set(param 2)) elif isinstance(param 2, set): return set(__commonlistset(param 1, param 2)) else: return __commoncollectiondict(param 1, param 2) elif isinstance(param 1, set): if isinstance(param 2, list): return set(__commonlistset(param 2, param 1)) elif isinstance(param 2, set): return param 1. intersection(param 2) else: return __commoncollectiondict(param 1, param 2) elif isinstance(param 1, dict): return __commoncollectiondict(param 2, param 1) 5 DTU Health Tech, Technical University of Denmark

Generators are your own defined iterators, like range. Generators look like functions, but they

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 += 1 for i in myrange(10): print(i) More info: http: //www. programiz. com/python-programming/generator 6 DTU Health Tech, Technical University of Denmark

Examples A generator for making a random gene. Parameters are the required min and

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 Value. Error(’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 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) 7 DTU Health Tech, Technical University of Denmark