jeudi 26 février 2015

how to handle an empty list, python3

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.


Aucun commentaire:

Enregistrer un commentaire