简体   繁体   中英

Finding index of list item which is assembled by Fraction class

I'm trying to find the index of specifyed element in my list that looks like this for example Which is generated by xrange with Fractional.

>> print lst
[Fraction(1, 8), Fraction(1, 7), Fraction(1, 6), Fraction(1, 5), Fraction(1, 4),          Fraction(2, 7), Fraction(1, 3), Fraction(3, 8), Fraction(2, 5), Fraction(3, 7), Fraction(1,   2), Fraction(4, 7), Fraction(3, 5), Fraction(5, 8), Fraction(2, 3), Fraction(5, 7), Fraction(3, 4), Fraction(4, 5), Fraction(5, 6), Fraction(6, 7), Fraction(7, 8)] 

I have been trying everything. Why doesn't this work.

lst.index(1,6) 

or

lst.index( Fraction(1,6) )

and I have tried many more but I always get

print lst.index(Fraction(1,6))
ValueError: Fraction(1, 6) is not in list

Any advice ?

More on how I created the list :

for d in xrange(1,limit+1):
        for n in xrange(1,limit+1):
                x = Fraction(n,d)
                lst.append(x)

seems fine to me...

>>> from fractions import Fraction
>>> d = [Fraction(1, 8), Fraction(1, 7), Fraction(1, 6), Fraction(1, 5), Fraction(1, 4),          Fraction(2, 7), Fraction(1, 3), Fraction(3, 8), Fraction(2, 5) , Fraction(3, 7), Fraction(1,   2), Fraction(4, 7), Fraction(3, 5), Fraction(5, 8), Fraction(2, 3), Fraction(5, 7), Fraction(3, 4), Fraction(4, 5), Fraction(5,6), Fraction(6, 7), Fraction(7, 8)]
>>> d.index(Fraction(1,6))
2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM