2007/1/16, Carlos <<a href="mailto:carloslara@web.de">carloslara@web.de</a>>:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hello to everybody,<br><br>I have a question that I think is a little related to clustering, I have<br>a list of lists, like this:<br><br>List = [[1,5],[6,8],[48,10],[99,56]]<br><br>The list is composed by a large number of lists not just four, and each
<br>'interior' list contains two numbers that are the location of an object<br>in a plane, so they are X and Y coordinates, my question is:<br><br>Can I use this list to evaluate how many points are in a given range?
<br>Thats is taking the highest and lowest values for X an Y and, lets say<br>divide that range in 10 regions, then get the number of objects on each<br>region. Is this possible? and if yes, how canI do it? I ask this because
<br>as you know my python skills are not that great :)<br></blockquote></div><br>First, this feels like a list of tuples rather than a list of lists; however, tuples and lists don't differ that much in their behaviour, so there's nothing really lost.
<br><br>And yes, it is possible. An inline if would be the way I would resolve that:<br><br>def withinrange(list,xmin,xmax,ymin,ymax):<br> # Get the elements of list for which the first part of the pair is between xmin and xmax
<br> # (inclusive) and the second between ymin and ymax.<br> return [c for c in list if xmin <= c[0] <= xmax and ymin <= c[1] <= ymax]<br><br>The longer but clearer method of doing the same would be:<br>
<br>def withinrange(list,xmin,xmax,ymin,ymax):<br> templist = []<br> for c in list:<br> if xmin <= c[0] <= xmax and ymin <= c[1] <= ymax:<br> templist.append(c)<br> return templist<br>
<br>-- <br>Andre Engels, <a href="mailto:andreengels@gmail.com">andreengels@gmail.com</a><br>ICQ: 6260644 -- Skype: a_engels