0

I have a function that creates a list of aminoacids (aaCandidates), but this list can even be empty. If this list is empty I would like to jump the step and continue with the following one.

My code is:

def magicfunction(referenceDistance,referenceAA):

    amminoacids = ('A', 'R', 'N', 'D', 'C', 'Q', 'E', 'G', 'H', 'I', 'L', 'K', 'M', 'F', 'P', 'S', 'T', 'W', 'Y', 'V')
    aaCandidates = list()
    for aa in amminoacids:
        if distance(aa,referenceAA) == referenceDistance:
            aaCandidates.append(aa)
        if not aaCandidates:
            break

    luckyAA = choice(aaCandidates)

    return(luckyAA)

I call this function in another file as follow:

for i in range(lenghtPairs):
    r1 = randrange(20)
    r2 = randrange(20)
    coppie.append([aminoacidi[r1], aminoacidi[r2]])

for i in range(lenghtPairs):
    dictionary = dict()
    frequenze = dict()

    if i == 0:
        a = randrange(20)
        b = randrange(20)
        pairs[0] = [aminoacids[a], aminoacids[b]]
    else:
        c = randrange(20)
        pairs[i][0] = aminoacids[c]
        distanceNeighbours = distance(pairs[i][0],pairs[i-1][0])
        aaChosen = magicfunction(distanceNeighbours,pairs[i-1][1])
        pairs[i][1] = aaChosen

    print(i + 1)

I tried the condition => if not aaCandidates: break but it didn't work:

      File "/.../lib/python3.4/random.py", line 255, in choice
    raise IndexError('Cannot choose from an empty sequence')
IndexError: Cannot choose from an empty sequence
13
  • Define didn't work more closely. You mean the random.choice() call raises an exception? Commented Feb 26, 2015 at 10:31
  • 2
    Note that it is far more common (and natural) to use [] to define an empty list, and {} for an empty dictionary, rather than list() and dict(). Commented Feb 26, 2015 at 10:32
  • if "len(aaCandidates) == 0"? Commented Feb 26, 2015 at 10:32
  • @en_Knight: why would that work when not aaCandidates didn't? Something else is wrong. Commented Feb 26, 2015 at 10:32
  • Note that if not aaCandidates: break will immediately break out of the for loop if the first aa in amminoacids doesn't get appended to aaCandidates. Commented Feb 26, 2015 at 10:37

1 Answer 1

1

Your list is empty, so random.choice() fails. You'll need to decide what to do instead when the list is empty, but do so outside of the for loop, so when the list has completed building:

for aa in amminoacids:
    if distance(aa,referenceAA) == referenceDistance:
        aaCandidates.append(aa)

if not aaCandidates:
    return 'some default choice'

luckyAA = choice(aaCandidates)
return luckyAA

All that putting your break in the loop achieves is to ensure that nothing is going to be added to your list if the first aa was not a candidate.

Sign up to request clarification or add additional context in comments.

2 Comments

Or: return heapq.nlargest(1, (aa for aa in amminoacids if distance(aa, referenceAA) == referenceDistance), key=lambda L: random.random()) or 'default') ?
@JonClements: yay, lets go find some more ultra-advanced one-liners! :-P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.