Great opportunity to apply list comprehension (suggestion) #2

Closed
opened 2019-11-17 15:22:42 +01:00 by oekk · 1 comment

In MinimumSkew.py

Conditions met to use list comprehensions:

  • Creation of a list: positions
  • Filter of data: if skew[i] == minimum
  • Iterator: range(0, len(Genome))

With this in mind we can change the following code:

def MinimumSkew(Genome):
    positions = []
    skew = SkewArray(Genome)
    minimum = min(skew)
    for i in range(0, len(Genome)):
        if skew[i] == minimum:
            positions.append(i)
    return positions

for this:

def MinimumSkew(Genome):
    skew = SkewArray(Genome)
    minimum = min(skew)
    return [i for i in range(0, len(Genome)) if skew[i] == minimum]

The "formula" used has the following basic structure:

[expression for item in list if conditional]

which is equivalent to:

for item in list:
    if conditional:
        expression
In [MinimumSkew.py](https://coolneng.duckdns.org/gitea/coolneng/biology-meets-programming/src/branch/master/Code/MinimumSkew.py) Conditions met to use list comprehensions: - Creation of a list: positions - Filter of data: if skew[i] == minimum - Iterator: range(0, len(Genome)) With this in mind we can change the following code: def MinimumSkew(Genome): positions = [] skew = SkewArray(Genome) minimum = min(skew) for i in range(0, len(Genome)): if skew[i] == minimum: positions.append(i) return positions for this: def MinimumSkew(Genome): skew = SkewArray(Genome) minimum = min(skew) return [i for i in range(0, len(Genome)) if skew[i] == minimum] The "formula" used has the following basic structure: ``` [expression for item in list if conditional] ``` which is equivalent to: ``` for item in list: if conditional: expression ```
Owner

That is elegant, thanks!

That is elegant, thanks!
Sign in to join this conversation.
No Label
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: coolneng/bioinformatics-course#2
No description provided.