Bobby Sanchez
Bobby Sanchez

Reputation: 59

Boolean Statements For Elements In Lists

Suppose I have a list of coefficients of a polynomial in descending order of exponents (if len(list) == x then the exponents would range from integers x-1 to 0). I want to delete the "small" elements of said list, meaning abs(element) > 0 and abs(element) < .000001 but keep the exponents of the polynomial that are not "small."

How do I exactly do this in Python 3.0?

Here is an example of what I want in context:

my_list = [3.000000000000000e-12, 4.99999999999948, 4.00000000000002, -0.000042535500000e-15, -0.200000000000000] exponents = [4,3,2,1,0] #As stated previously' ``>>> newlist = [4.99999999999948, 4.00000000000002, -0.200000000000000] ``>>> nexexp = [3,2,0]

Hence, the polynomial would be in the form 4.999999999999948*x^3 + 4.000000000000002*x^2 -0.200000000000000

Any suggestions would be very helpful!

Upvotes: 0

Views: 89

Answers (1)

DYZ
DYZ

Reputation: 57135

Instead of deleting the small elements, keep the large ones:

newlist,newexp = zip(*[(x,e) for x,e in zip(my_list,exponents) if abs(x) > 1e-6])

You can use a filter, too:

newlist,newexp = zip(*filter(lambda x: abs(x[0]) > 1e-6, zip(my_list,exponents))))

Upvotes: 9

Related Questions