How to report an error if an element is missing in the list of lists

前端 未结 2 776
耶瑟儿~
耶瑟儿~ 2021-01-28 16:22

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条回答
  •  无人共我
    2021-01-28 16:35

    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)]]
    

提交回复
热议问题