Python iterative loop -
i'm trying iterate through search list, i've written in c want re-write more pythonic.
i've been trying enumerate can't seem work, searching lines of data key-words saved in array called strings, can show me or explain correct python syntax please.
thanks
for line in f:     jd = json.loads(line)     n=0     while n<=(len(strings)-1):         if findwholeword(strings[n])(line) != none:             print (jd['user_id'], jd['text'])             break         n=n+1      
there seems no need use enumerate here. iterate on strings directly:
for s in strings:     if findwholeword(s)(line) != none:         print (jd['user_id'], jd['text'])         break   if need index variable n, use enumerate:
for n, s in enumerate(strings):     if findwholeword(s)(line) != none:         # n here?         print (jd['user_id'], jd['text'])         break   but since break after first match anyway, use any builtin:
if any(findwholeword(s)(line) != none s in strings):     jd = json.loads(line)     print (jd['user_id'], jd['text'])   also, pointed out in @ben's answer, can improve performance of check turning either strings or line set of words , using in operator check whether word 1 set in other. difficult tell without knowing findwholeword doing.
Comments
Post a Comment