jeudi 5 février 2015

Searching python list using for loop and conditional statements


Vote count:

0





a = [1,1,1,4,4,4,5]
b = [20150602, 20150603, 20150604, 20150605, 20150606, 20150607,20150608]
c = zip(a,b)

output = []
for i in range(0, len(c)-1):
if c[i][0] == c[i+1][0] and c[i+1][1] - c[i][1] <= 3:
output.append(c[i])
print output


This code searches 2 lists using conditionals. If element n == element n+1 in list a AND (element n+1) - (element n)<= 3 in list b .....it prints the results. The problem I'm having is that it cuts off 1 element that should be printing. (i.e. This code will output



[(1, 20150602), (1, 20150603), (4, 20150605), (4, 20150606)]


but should be outputting



[(1, 20150602), (1, 20150603), (1,20150604), (4, 20150605), (4, 20150606), (4, 20150607)])




asked 3 mins ago







Searching python list using for loop and conditional statements

Aucun commentaire:

Enregistrer un commentaire