placid wrote:
> Hi all,
>
> I have two lists that contain strings in the form string + number for
> example
>
> >>> 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']
>
> and now what ive been trying to do is find the first string that is
> available,
> i.e a string that is in neither of the two lists so the following code
> should only print XXX4 then return.
>
> for i in xrange(1,10):
>     numpart = str(1) + str("%04i" %i)
>     str = "XXX" + numpart
>
>       for list1_elm in list1:
>           if list1_elm == str:
>                break
>           else:
>                for list2_elm in list2:
>                    if list2_elm == str:
>                       break
>                    else:
>                       print str
>                       return
>
> Cheer

Well first off, don't use 'str' for a variable name.

Second, "%04i" % i creates a string, don't call str() on it.

Third, str(1) will always be "1" so just add that to your format string
already "1%04i" % i

(And if the "XXX" part is also constant then add that too: "XXX1%04i" %
i)

Finally, you can say:

for i in xrange(1,10):
    s = "XXX1%04i" % i
    if s not in list1 and s not in list2:
        print s

HTH,
~Simon

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to