list problem

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Wed Jul 26 05:28:49 EDT 2006


placid:

This may be a solution:

l1 = ['acXXX1', 'XXX2', 'wXXX3', 'kXXX5']
l2 = [ 'bXXX1', 'xXXX2', 'efXXX3', 'yXXX6', 'zZZZ9']

import re
findnum = re.compile(r"[0-9]+$")
s1 = set(int(findnum.search(el).group()) for el in l1)
s2 = set(int(findnum.search(el).group()) for el in l2)
nmax = max(max(s1), max(s2))
# XXXnmax is surely unavailable
missing = set(range(1, nmax)) - s1 - s2
print ["XXX%d" % i for i in sorted(missing)]

# Output: ['XXX4', 'XXX7', 'XXX8']

If you need more speed you can replace some of those sets (like the
range one) with fors.

Bye,
bearophile




More information about the Python-list mailing list