问题
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 what's available.
I have two dictionaries setup:
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," "," "," "]
}
Versions = What's available / Client = Versions that clients are running
There are 4 key values representing 4 different applications and the version they're on.
I've tried a few methods but I've hit a wall with it.
I want to compare the key values in client_[n] against the corresponding version (not sure how to link client_3 with v83, client1 with v80 etc). The comparison would just be along the lines of:
if client['client_1'][0] < versions['v80'][0]
print(client.client_1[0],"-->",versions.v80[0])
Thanks in advanced !
回答1:
You could use the following for loop:
>>> for i in range(len(versions)):
... for j in range(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.
回答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]
回答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():
if int(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]
来源:https://stackoverflow.com/questions/31635153/compare-integer-values-between-two-dictionaries-in-python