Python looping in list with if condition not working -
i trying loop in list called sizelist , check condition. code not returning right values. knows problem might be?
overavg = 0 overtre = 0 avgsize = np.mean(sizelist) in sizelist: if >= avgsize: overavg += 1 treshold = raw_input("enter size treshold: ") t in sizelist: if t >= treshold: overtre += 1 print overavg print overtre key, value in pktdict.iteritems(): sizevalue = [value] sizelist.append(sizevalue)
working code:
for key, value in pktdict.iteritems(): sizevalue = value sizelist.append(sizevalue) avgsize = np.mean(sizelist) in sizelist: if >= avgsize: overavg += 1 treshold = int(raw_input("enter size treshold: ")) t in sizelist: if t >= treshold: overtre += 1 print overavg print overtre
you need convert input size int
, otherwise comparing lexical value rather numerical. also, comment see list has list of size 1 in each field, need flatten (or initialize list 1d in first place):
import numpy np overavg = 0 overtre = 0 sizelist = [[4],[7],[43],[5],[54],[7],[4],[5]] # formatted because op's list sizelist = [item[0] item in sizelist] avgsize = np.mean(sizelist) in sizelist: if >= avgsize: overavg += 1 treshold = int(raw_input("enter size treshold: ")) t in sizelist: if t >= treshold: overtre += 1 print overavg print overtre
result:
enter size treshold: 5 2 6
in code, you're creating list 1 item each iteration, while should doing this:
for key, value in pktdict.iteritems(): sizelist.append(value)
Comments
Post a Comment