Python - Unsupported type(s) : range and range

前端 未结 4 1663
孤街浪徒
孤街浪徒 2021-01-18 01:55

I\'m getting this strange error trying to run a script, the code appears to be correct but it seems python (3) didn\'t liked this part:

        def function(         


        
4条回答
  •  孤独总比滥情好
    2021-01-18 02:14

    In the middle of your expression you do range(-30,0) + range(1,30). This is causing the error because in Python 3 range() returns an iterator, not a list like in Python 2.x.

    One way to get this to work is to just convert each range to a list before adding:

    d = dict([(randrange(101), Racional(coeff(randrange(-20,20)),
                                    coeff(choice(list(range(-30,0))+
                                                 list(range(1,30))))))
             for k in range(non_nil)])
    

    Since it seems like you want to just exclude 0 from the range(-30, 30), you could also use filter(None, range(-30, 30)).

    Alternatively you could use choice((1, -1)) * choice(range(1, 30)), which is equivalent to choice(list(range(-30, 0)) + list(range(1, 30))). (edit: actually the prior expression will not include -30 in the possibilities, not sure whether or not that is an issue).

提交回复
热议问题