efficient intersection of lists with rounding

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gordon Williams

    #1

    efficient intersection of lists with rounding

    Hi,

    I have to lists that I need to find the common numbers (2nd rounded to
    nearest integral) and I am wondering if there is a more efficient way of
    doing it.
    [color=blue][color=green][color=darkred]
    >>> a= [(123,1.3),(123, 2.4),(123,7.8), (123,10.2)]
    >>> b= [(123, 0.9), (123, 1.9), (123, 8.0)]
    >>> [ (i,round(j)) for i,j in a for l,m in b if (i,round(j)) ==[/color][/color][/color]
    (l,round(m))]
    [(123, 1.0), (123, 2.0), (123, 8.0)][color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]
    This works but a and b can be in the order of 30K long.

    A couple of other bits of info.
    - a and b are ordered smallest to largest (could bisect module be used?)
    - in the future I will want to round the second number of closest 0.25
    rather than whole number.

    Would the sets module be more efficient?

    I'm using python 2.3.

    Thanks for any ideas.

    Regards,

    Gordon Williams

Working...