indexing - python - find position of next item in list, given criteria -


i'm new python, dumbed-down explanations appreciated. have data have read in csv file have manipulated in form: [(t1,n1,v1),(t2,n2,v2),(t3,n3,v3),...]

what i'm trying is, given non-zero value in v, find position of next occurrence of n has 0 value v, , determine difference in t values. here code far:

d=[] i,x in enumerate(list):     if x[2]!=0:         j,y in enumerate(list):             if x[1]==y[1] , j>i , y[2]==0:                 d.append(y[0]-x[0])     else: d.append(0)  print d 

i did in excel using match , offset functions, i'm bit lost transitioning index , enumerate here.

my first problem nested loop doesn't stop when finds first match, , keeps appending t value differences every matching n value. i'd find first match.

my second query if there's better way this, nested loop isn't starting @ beginning of list, , instead starts @ index i. i'm dealing quite large data sets.

edit: managed make work, though it's quite inelegant (times , notes lists of 1st , 2nd elements of each tuple in list):

d=[] i,x in enumerate(list):     if x[2]!=0:         d.append(notes[i+1:].index(x[1]))     else: d.append("na")   dur=[] i,j in enumerate(d):     if j!="na":         dur.append(times[i+j+1]-times[i])     else: dur.append(0) 

i'd appreciate ideas on cleaner solution.

first note, it's not great have list named list. wasn't 100% clear on looking for, think works.

d = [] index, tup in enumerate(lst):     if tup[2] != 0:         next in lst[index + 1:]:             if next[2] == 0 , tup[1] == next[1]:                 d.append(next[0] - tup[0])                 break         if len(d) - 1 != index:             d.append('na')     else:         d.append('na') 

for example:

input: lst = [(1,3,0),(1,5,6),(1,2,4),(3,4,1),(4,2,0),(7,5,0),(8,4,0)]

output: d = ['na', 6, 3, 5, 'na', 'na', 'na']

if need times , don't need arrays line up, remove conditional appends 'na'.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -