2007/1/16, Carlos &lt;<a href="mailto:carloslara@web.de">carloslara@web.de</a>&gt;:<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>&#39;interior&#39; 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&#39;t differ that much in their behaviour, so there&#39;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>&nbsp;&nbsp;&nbsp; # Get the elements of list for which the first part of the pair is between xmin and xmax
<br>&nbsp;&nbsp;&nbsp; # (inclusive) and the second between ymin and ymax.<br>&nbsp;&nbsp;&nbsp; return [c for c in list if xmin &lt;= c[0] &lt;= xmax and ymin &lt;= c[1] &lt;= ymax]<br><br>The longer but clearer method of doing the same would be:<br>
<br>def withinrange(list,xmin,xmax,ymin,ymax):<br>&nbsp;&nbsp;&nbsp; templist = []<br>&nbsp;&nbsp;&nbsp; for c in list:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if xmin &lt;= c[0] &lt;= xmax and ymin &lt;= c[1] &lt;= ymax:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; templist.append(c)<br>&nbsp;&nbsp;&nbsp; return templist<br>
<br>-- <br>Andre Engels, <a href="mailto:andreengels@gmail.com">andreengels@gmail.com</a><br>ICQ: 6260644&nbsp;&nbsp;--&nbsp;&nbsp;Skype: a_engels