I just play with a simulation ( "The First Law of Mendel’s Inheritance" ).
Before I can allow the creatures to mate and analyze the result, a population must be generated, i.e. the list must be filled with different numbers of three different types of tuples, without unpacking them.
When trying to get acquainted with itertools (I will need combinations later in the conjugate part), I came up with the following Solution:
import itertools k = 2 m = 3 n = 4 hd = ('A', 'A') # homozygous dominant het = ('A', 'a') # heterozygous hr = ('a', 'a') # homozygous recessive fhd = itertools.repeat(hd, k) fhet = itertools.repeat(het, m) fhr = itertools.repeat(hr, n) population = [x for x in fhd] + [x for x in fhet] + [x for x in fhr]
which will result in:
[('A', 'A'), ('A', 'A'), ('A', 'a'), ('A', 'a'), ('A', 'a'), ('A', 'a'), ('A', 'a'), ('A', 'a'), ('A', 'a')]
Is there a more reasonable, pythonic or memorable way to save the final list, for example. without first generating lists for the three types of faces?
Klaus-dieter warzecha
source share