Take the 2-minute tour ×

Input data:

rules = (
    ("1|1": "A"),
    ("2|1": "B"),
    ("3|1": "C"),
    ("2|2": "X")

pattern = [[1,2,3], [7,8]]

I need a function to do this job for the below result:

list1 = [[1,3,4], [7,9]] # result: B
list2 = [[1,2,3], [7,9]] # result: C
list3 = [[0,5,4], [8,5]] # result: None
list4 = [[1,6,2], [7,8]] # result: X
list5 = [[1,6,2,5], [7,8]] # result: Error
share|improve this question
Can you clarify the algorithm a bit? It's not clear to me what it is. –  David Wolever Dec 24 '11 at 8:32
Definetly Need more informations about how it's suppose to work, and what it's about in order to help you.. –  Oleiade Dec 24 '11 at 8:34
Also, I'm voting to close as too localized because, as the description says, this question is "… only relevant to … an extraordinarily narrow situation not generally applicable to the worldwide audience of the internet". –  David Wolever Dec 24 '11 at 8:34
For instance, with list1 we see 2 elements of list1[0] are in pattern, 1 element of list1[1] is in pattern list. So we should compare with rules to show the result. –  anhtran Dec 24 '11 at 8:35
Please edit the question to explain what is going on -- your description in the comment is far too cryptic for me to understand. Step through one of the examples step-by-step. (And heck, that might even be the best answer...) –  sarnold Dec 24 '11 at 8:43
show 1 more comment

closed as not a real question by David Wolever, Sylvain Defresne, pyfunc, Karl Knechtel, Joe Dec 24 '11 at 12:53


1 Answer

up vote 3 down vote accepted

A bit of functional Python:

>>> rules = {
    "1|1": "A",
    "2|1": "B",
    "3|1": "C",
    "2|2": "X"
>>> pattern = [[1,2,3], [7,8]]
>>> l = [[1,3,4], [7,9]]
>>> diffs = '|'.join(map(lambda x: str(len(set(x[0]) & set(x[1]))),
                         zip(pattern, l)))
>>> rules.get(diffs)

Works the same for the rest of lists. Error handling is the exercice for you:)

share|improve this answer
add comment
