Skip to content Skip to sidebar Skip to footer

Compare Integer Values Between Two Dictionaries In Python

My goal for this script is to compare current versions of clients software with current software versions that are available from the vendor. At this stage I just want to print out

Solution 1:

You could use the following for loop:

>>>for i inrange(len(versions)):...for j inrange(len(versions['v8'+str(i)])):...try:...if client['client_' + str(i+1)][j] < versions['v8' + str(i)][j]:...print ("client_" + str(i+1), "Program_" + str(j+1), client["client_" + str(i+1)][j],"-->",versions["v8"+str(i)][j])...except TypeError:...print ("client_" + str(i+1), "Program_" + str(j+1), "Missing")...continue... 
client_1 Program_1 80.1 --> 80.24
client_1 Program_2 80.1 --> 80.16
client_1 Program_3 80.1 --> 80.15
client_1 Program_4 80.1 --> 80.7
client_2 Program_1 81.1 --> 81.12
client_2 Program_2 80.1 --> 81.7
client_2 Program_3 80.1 --> 81.4
client_2 Program_4 80.1 --> 81.11
client_3 Program_1 82.1 --> 82.7
client_3 Program_2 80.1 --> 82.5
client_3 Program_3 80.1 --> 82.9
client_3 Program_4 80.1 --> 82.6
client_4 Program_1 81.1 --> 83.0
client_4 Program_2 Missing
client_4 Program_3 80.1 --> 83.1
client_4 Program_4 80.1 --> 83.0

However, this relies on the information you have provided being consistent, the number of versions and clients being the same, with the same length lists.

If you were a bit more clear, I could try for a more robust solution.

Solution 2:

You could introduce a third dictionary that acts as an interface

versions = {
'v80':[80.24,80.16,80.15,80.7],
'v81':[81.12,81.7,81.4,81.11],
'v82':[82.7,82.5,82.9,82.6],
'v83':[83.0,83.1,83.1,83.0]
}

interface = {

"client_1":"v80",
"client_2":"v81",
"client_3":"v82",
"client_4":"v83",
"client_5":"v84"
}

client = {
'client_1':[80.1,80.1,80.1,80.1],
'client_2':[81.1,80.1,80.1,80.1],
'client_3':[82.1,80.1,80.1,80.1],
'client_4':[81.1," ",80.1,80.1],
'client_5':[80.1," "," "," "]
}

Now just use the following to compare:

if client['client_1'][0] < versions[interface["client_1"]][0]:
    print client["client_1"][0],"-->",versions[interface["client_1"]][0]

Solution 3:

You can try:

if client['client_1'][0] < versions['v80'][0]:
    print(client["client_1"][0] ,"-->",versions["v80"][0])

To do it for all client and versions:

versions = {
'v80':[80.24,80.16,80.15,80.7],
'v81':[81.12,81.7,81.4,81.11],
'v82':[82.7,82.5,82.9,82.6],
'v83':[83.0,83.1,83.1,83.0]
}

client = {
'client_1':[80.1,80.1,80.1,80.1],
'client_2':[81.1,80.1,80.1,80.1],
'client_3':[82.1,80.1,80.1,80.1],
'client_4':[81.1," ",80.1,80.1],
'client_5':[80.1," "," "," "]
}


for cli, num_cl in client.items():
    for ver, num in versions.items():
        ifint(num[0]) == int(num_cl[0]):
            print(cli, num_cl, ver, num)

And the output:

client_4 [81.1, ' ', 80.1, 80.1] v81 [81.12, 81.7, 81.4, 81.11]
client_5 [80.1, ' ', ' ', ' '] v80 [80.24, 80.16, 80.15, 80.7]
client_2 [81.1, 80.1, 80.1, 80.1] v81 [81.12, 81.7, 81.4, 81.11]
client_3 [82.1, 80.1, 80.1, 80.1] v82 [82.7, 82.5, 82.9, 82.6]
client_1 [80.1, 80.1, 80.1, 80.1] v80 [80.24, 80.16, 80.15, 80.7]

Post a Comment for "Compare Integer Values Between Two Dictionaries In Python"