list problem

Paul Rubin http
Wed Jul 26 12:46:59 EDT 2006


"placid" <Bulkan at gmail.com> writes:
> >>> list1 = [ ' XXX1', 'XXX2', 'XXX3', 'XXX5']
> 
> the second list contains strings that are identical to the first list,
> so lets say the second list contains the following
> 
> >>> list1 = [ ' XXX1', 'XXX2', 'XXX3', 'XXX6']

I think you meant list2 for the second one.  So:


    import re

    list1 = [ ' XXX1', 'XXX2', 'XXX3', 'XXX5']
    list2 = [ ' XXX1', 'XXX2', 'XXX3', 'XXX6']

    def num(s):
       # get the number out of one of the strings 
       # (throw exception if no number is there)
       digits = re.search('\d+$', s)
       return int(digits.group(0))

    def get_lowest_unused(list1, list2):
       prev = 0
       for n in sorted(set(map(num,list1+list2))):
           if n != prev+1:
              return prev+1
           prev = n

    print get_lowest_unused(list1, list2)

You could do all this with iterators and save a little memory, but
that's more confusing.



More information about the Python-list mailing list