Ask Your Question
0

Creating sublist according countinuous intervals

asked 2020-06-18 03:49:52 -0500

Cyrille gravatar image

updated 2020-06-18 04:13:10 -0500

I have a list of number say

l = [1.25, 2.46, 3.99, 4, 5, 6, 7.12, 8.76 ,9.34, 10.42, 11.5, 12.]

I have a list of real intervals

Cl=[[0, 5),[5, 10),[10, 15)]

How to create sublists of l according to the intervals in Cl ? If I was work in N there will be no difficulties.

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
0

answered 2020-06-18 04:12:32 -0500

Cyrille gravatar image

I have found a solution, but I wonder if it is the best. As I am not Pierre de Fermat, I will give it

Ac=[[x for x in l if bool(x in cl[j])==True] for j in range(len(Cl))]
edit flag offensive delete link more

Comments

never ever use == True !!! Here you can just write if x in cl[j]

FrédéricC gravatar imageFrédéricC ( 2020-06-18 05:31:33 -0500 )edit

Your solution is a brute force approach which does not take into account that the lists of numbers and intervals are ordered. There are surely better solutions if both lists are large. By the way, note that your syntax for intervals is not valid. Try this:

l = [1.25, 2.46, 3.99, 4, 5, 6, 7.12, 8.76 ,9.34, 10.42, 11.5, 12.]
Cl = [[0, 5], [5, 10], [10, 15]]
[[x for x in l if c[0]<=x<c[1]] for c in Cl]
Juanjo gravatar imageJuanjo ( 2020-06-18 21:58:29 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-06-18 03:49:52 -0500

Seen: 27 times

Last updated: Jun 18