Home > Blockchain >  Output is repeating the first dictionary every time
Output is repeating the first dictionary every time

Time:04-11

I am supposed to be receiving the output for a reformatted dictionary from three separate inputs.

Current output:

{'JS': {'Paid': [200, 400, 500, 600]}, 'SK': {'Paid': [400, 1000, 1600]}}
{'JS': {'Paid': [200, 400, 500, 600]}, 'SK': {'Paid': [400, 1000, 1600]}}
{'JS': {'Paid': [200, 400, 500, 600]}, 'SK': {'Paid': [400, 1000, 1600]}}

Expected output:

print(buildCoverageDictionary(paidList1)) ->
{'JS': {'Paid': [200, 400, 500, 600]}, 'SK': {'Paid': [400, 1000, 1600]}}
print(buildCoverageDictionary(paidList2)) ->
{'JS': {'Paid': [200, 400, 500, 600]}, 'SK': {'Paid': [1010, 2000]}, 'MJ': {'Paid':
[5, 6, 7]}, 'ZF': {'Paid': [2660, 500]}}
print(buildCoverageDictionary(paidList3)) -> {'SK': {'Paid': [200, 400]}}

Two questions:

  1. How can I edit the code to output the expected output?
  2. How can I edit the code to not require me to individually reformat each dictionary and instead create a function that can properly reformat the dictionary no matter what the input is?

My code is as follows:

paidList1 = [["JS", 200, 400, 500, 600], ["SK", 400, 1000, 1600]]
paidList2 = [["JS", 200, 400, 500, 600], ["SK", 1010, 2000], ["MJ", 5, 6, 7], ["ZF", 2660, 500]]
paidList3 = [["SK", 200, 400]]


def buildCoverageDictionary(paidList):
    JS_dict = {'Paid': paidList1[0][1:5]}
    SK_dict = {'Paid': paidList1[1][1:4]}
    new_dict1 = {paidList1[0][0]: JS_dict, paidList1[1][0]: SK_dict}
    return new_dict1

    JS_dict = {'Paid': paidList2[0][1:5]}
    SK_dict = {'Paid': paidList2[1][1:3]}
    MJ_dict = {'Paid': paidList2[2][1:4]}
    ZF_dict = {'Paid': paidList2[3][1:3]}
    new_dict2 = {paidList2[0][0]: JS_dict, paidList2[1][0]: SK_dict, paidList2[2][0]: MJ_dict, paidList2[3][0]: ZF_dict}   
    return new_dict2
    
    SK_dict = {'Paid': paidList3[0][1:3]}
    ew_dict3 = {paidList3[0][0]: SK_dict}
    return new_dict3
    
print(buildCoverageDictionary(paidList1))
print(buildCoverageDictionary(paidList2))
print(buildCoverageDictionary(paidList3))

CodePudding user response:

As @Barmar mentioned, return ends the functions and nothing after it is executed. You could try something like below, and keep it more flexible to changes in input, e.g. by looping on paidList and not hard coding the index, instead use [1:] which translates from index 1 till the last.

paidList1 = [["JS", 200, 400, 500, 600], ["SK", 400, 1000, 1600]]
paidList2 = [["JS", 200, 400, 500, 600], ["SK", 1010, 2000], ["MJ", 5, 6, 7], ["ZF", 2660, 500]]
paidList3 = [["SK", 200, 400]]


def buildCoverageDictionary(paidList):
    output = {}
    for item in paidList:
        output[item[0]] = {'Paid': item[1:]}
    return output

print(buildCoverageDictionary(paidList1))
print(buildCoverageDictionary(paidList2))
print(buildCoverageDictionary(paidList3))

Using dict comprehension

def buildCoverageDictionary(paidList):
    return {item[0]: {'Paid': item[1:]} for item in paidList}

CodePudding user response:

This should answer your first question:

paidList1 = [["JS", 200, 400, 500, 600], ["SK", 400, 1000, 1600]]
paidList2 = [["JS", 200, 400, 500, 600], ["SK", 1010, 2000], ["MJ", 5, 6, 7], ["ZF", 2660, 500]]
paidList3 = [["SK", 200, 400]]


def buildCoverageDictionary():
    JS_dict = {'Paid': paidList1[0][1:5]}
    SK_dict = {'Paid': paidList1[1][1:4]}
    new_dict1 = {paidList1[0][0]: JS_dict, paidList1[1][0]: SK_dict}
    print(new_dict1)

    JS_dict = {'Paid': paidList2[0][1:5]}
    SK_dict = {'Paid': paidList2[1][1:3]}
    MJ_dict = {'Paid': paidList2[2][1:4]}
    ZF_dict = {'Paid': paidList2[3][1:3]}
    new_dict2 = {paidList2[0][0]: JS_dict, paidList2[1][0]: SK_dict, paidList2[2][0]: MJ_dict, paidList2[3][0]: ZF_dict}
    print(new_dict2)

    SK_dict = {'Paid': paidList3[0][1:3]}
    new_dict3 = {paidList3[0][0]: SK_dict}
    print(new_dict3)


buildCoverageDictionary()

The changes I've made are:

  • You don't use the parameter paidList, so I got rid of that.
  • When you call the return function, it returns the thing you want and skips the rest of the function. So I replaced the returns with print.
  • You spelled new_dict3(as ew_dict3) wrong.
  • Related