Skip to content Skip to sidebar Skip to footer

How To Report An Error If An Element Is Missing In The List Of Lists

suppose list1 = [['a', (1, 1)], ['a', (1, 3)], ['a', (1, 4)], ['b', (2,1)], ['b', (2,2)], ['b',(2, 4)]] list2 = [[(1, 1), (1, 3), (2, 1), (2, 2), (2, 4)]] Now how could I report

Solution 1:

You should use a dict instead of a list. But here's a solution using your structures. s1 is a similar idea as the previous answer but note the unnecessarily long list comprehension to get the pattern you have in list1. And you require a specific for loop to check rather than the set "-" operator.

>>> s1 = [[x, (c, d)] for x in ['a', 'b']
...                   for c in range(1, 3)
...                   for d in range(1, 5)
...                   if x=='a' and c==1 or x=='b' and c==2]
>>> s1
[['a', (1, 1)], ['a', (1, 2)], ['a', (1, 3)], ['a', (1, 4)],
 ['b', (2, 1)], ['b', (2, 2)], ['b', (2, 3)], ['b', (2, 4)]]
>>>
>>> list1 = [['a', (1, 1)], ['a', (1, 3)], ['a', (1, 4)],
...          ['b', (2, 1)], ['b', (2, 2)], ['b', (2, 4)]]
>>> for thing in s1:
...     if thing not in list1:
...         print 'missing: ', thing
...         # or raise an error if you want
...         
missing:  ['a', (1, 2)]
missing:  ['b', (2, 3)]

Repeat the same for list2. Creating s2 should be easier using the example for s1 above.

Btw, the dict would look like this for list1:

dict1 = {'a': [(1, 1), (1, 3), (1, 4)], 'b': [(2, 1), (2, 2), (2, 4)]}

Then creating s1 is simplified a bit and but the comparison loop might get two lines longer.


To answer your question to generalize, then either 1. knowing letters first or 2. knowing numbers/number of letters?

Knowing letters:

>>> set_of_letters = ('a', 'b', 'c')
>>> s1 = [[x, (ord(x)-96, d)]
...       for x in set_of_letters
...       for d in range(1, 5)]
>>> s1
[['a', (1, 1)], ['a', (1, 2)], ['a', (1, 3)], ['a', (1, 4)],
 ['b', (2, 1)], ['b', (2, 2)], ['b', (2, 3)], ['b', (2, 4)],
 ['c', (3, 1)], ['c', (3, 2)], ['c', (3, 3)], ['c', (3, 4)]]

Knowing numbers:

>>> number_of_letters = 3
>>> s1 = [[chr(c+96), (c, d)]
...       for c in range(1, number_of_letters + 1)
...       for d in range(1, 5)]
>>> s1
[['a', (1, 1)], ['a', (1, 2)], ['a', (1, 3)], ['a', (1, 4)],
 ['b', (2, 1)], ['b', (2, 2)], ['b', (2, 3)], ['b', (2, 4)],
 ['c', (3, 1)], ['c', (3, 2)], ['c', (3, 3)], ['c', (3, 4)]]

Solution 2:

from collections import defaultdict
set1 = set(list1)
set2 = set(list2)
missing = []
dict1 = defaultdict(lambda: defaultdict(list))
dict2 = defaultdict(list)
for key, sublist in set1:
    dict1[key][sublist[0]].append(sublist[1])
for key, value in set2:
    dict2[key].append(value)
for key, subdict in sorted(dict1.iteritems()):
    for subkey, values in sorted(subdict.iteritems()):
        subkey_misses = []
        last_value = None
        for value in values:
            if last_value is not None and last_value + 1 != value:
                subkey_misses.extend(range(last_value + 1, value))
            last_value = value
        if subkey_misses:
            misses.append('%s.%d missing %s' % (key, subkey, subkey_misses))
for key, values in sorted(dict2.iteritems()):
    key_misses = []
    last_value = None
    for value in values:
        if last_value is not None and last_value + 1 != value:
            key_misses.append(range(last_value + 1), value))
        last_value = value
    if key_misses:
        misses.append('%d missing %s' % (key, key_misses))
print misses

Post a Comment for "How To Report An Error If An Element Is Missing In The List Of Lists"