1

Looking to compare a dictionary to a set list, with a given key to use by user. Having a hard time comparing to a list.

For example:

I'm given the key:A I would like to compare all test in the dict to the list.

Expected results:

For A:

Test1: Okay

Test2: Okay

Test3: Okay

Test4: Invalid

Test5: Okay

List:

 data = {'A': [{'Test1', 'abc'},
                {'abc', 'def', 'Test2'},
                {'abc', 'ghi', 'Test3'},
                {'Test4', 'abc, xyz'},
                {'abc', 'def','Test5'}],
     'B': [{'Test1', 'abc'},
                {'abc', 'ghi', 'Test3'},
                {'ghi','Test4'},
                {'Test5', 'efg'}]}

Dictionary: Given either the value either A or B as key with the dict below and comparing to the list.

testCase =    {'Test1': 'abc',
     'Test2': 'def',
     'Test3': 'ghi',
     'Test4': 'ghi',
     'Test5': 'def'}

Attempt:

given = 'A'

data = {'A': [{'Test1', 'abc'},
                {'abc', 'def', 'Test2'},
                {'abc', 'ghi', 'Test3'},
                {'Test4', 'abc, xyz'},
                {'abc', 'def','Test5'}],
     'B': [{'Test1', 'abc'},
                {'abc', 'ghi', 'Test3'},
                {'ghi','Test4'},
                {'Test5', 'efg'}]}

testCase =    {'Test1': 'abc',
                'Test2': 'def',
                'Test3': 'ghi',
                'Test4': 'ghi',
                'Test5': 'def'}

for items in data:
    for given in data.keys():
        if testCase in data:
            print (testCase.keys() + ": Okay")
        else:
            print (testCase.keys() + ": Invalid")
jojo_first
  • 13
  • 4

2 Answers2

0

Idea for solution

  • Separate the logic: Put checking one key (A or B) + list -pair into one function, (e.g. check_items()). Also, checking if value (e.g. 'def') and the testname (e.g. 'Test2') are found in the list in the data[key] could be separated to another function, check_test_ok()

Code

Maybe something like

def check_test_ok(testname, value, case_items):
    found = False
    for item in case_items:
        if (testname in item) and (value in item):
            found = True
    if found:
        print(testname, ": Okay")
        return
    print(testname, ": Invalid")


def check_items(case_items, test_case):
    for testname, value in test_case.items():
        check_test_ok(testname, value, case_items)


for key, case_items in data.items():
    print('Checking items for ', key)
    print('=' * 25)
    check_items(case_items, testCase)
    print('\n')

Example Output:

Checking items for  A
=========================
Test1 : Okay
Test2 : Okay
Test3 : Okay
Test4 : Invalid
Test5 : Okay


Checking items for  B
=========================
Test1 : Okay
Test2 : Invalid
Test3 : Okay
Test4 : Okay
Test5 : Invalid
Niko Föhr
  • 28,336
  • 10
  • 93
  • 96
0

I solved it like that.

 given = 'A'

data = {'A': [{'Test1', 'abc'},
              {'abc', 'def', 'Test2'},
              {'abc', 'ghi', 'Test3'},
              {'Test4', 'abc, xyz'},
              {'abc', 'def', 'Test5'}],
        'B': [{'Test1', 'abc'},
              {'abc', 'ghi', 'Test3'},
              {'ghi', 'Test4'},
              {'Test5', 'efg'}]}

testCase = {'Test1': 'abc',
            'Test2': 'def',
            'Test3': 'ghi',
            'Test4': 'ghi',
            'Test5': 'def'}

for items in data.values():

    for given,tests in zip(items,testCase.values()):
        print(given,' : ',tests)
        if tests in given:
            print(testCase.keys() , ": Okay")
        else:
            print(testCase.keys() , ": Invalid")
Tom Riddle
  • 58
  • 1
  • 8