python - Losing float precision in for loop -
this question has answer here:
i have list of floats taken sqlite3 db. want find first 2 numbers greater number, 18 in case, , enumerate position in db.
the db list:
pr = [(20.49999999999983,), (16.29999999999967,), (13.799999999999102,), (18.600000000000705,), (9.600000000000364,), (11.599999999999966,), (25.30000000000001,)...]
hence try following:
fnd =([i[0] in pr if i[0] > 18]) >>> [20.49999999999983, 18.600000000000705, 25.30000000000001] j in fnd: print ([i i,k in enumerate(pr) if k == j])
for 3 empty lists. assuming problem comes fact float being rounded when use loop.
for j in fnd: print j >>>20.5, 18.6, 25.3
can offer workaround?
k
takes values of elements of pr
, 1-tuples containing numbers.
j
takes values of elements of fnd
, constructed list of numbers greater 18.
when compare k
j
, compare tuple number. never equal.
Comments
Post a Comment