Posts: 42
Threads: 12
Joined: Jul 2018
Hi
I have 2 list .I want to compare and get the difference.
a = [['vlan 158', ' name MARKET', ' mode vpc'], []] b = [['vlan 158', ' name MARKETING', ' mode vpc'], ['vlan 159', ' name SALES', ' mode vpc']] Expected Output :
Missing in "a"
vlan 159
name SALES
mode vpc please suggest how can it be done?
Note : list "a" has Vlan 158 so as list "b" .So its a MATCH ,though "name" is different.not worried about that...
Posts: 12,028
Threads: 485
Joined: Sep 2016
Is this a homework assignment?
look into using set to do comparisons.
by the way, there's no way the 'code' you show would have a chance of running.
Make an attempt.
hint, use a logical and on two sets
Posts: 42
Threads: 12
Joined: Jul 2018
this is the last part of my script and i am stuck...
Posts: 85
Threads: 12
Joined: May 2018
(Aug-03-2018, 06:45 PM)Nirmal Wrote: this is the last part of my script and i am stuck...
Assuming this is a hw assignment I won't give you the answer but to get you started:
a = [['vlan 158', 'name MARKET', 'mode vpc']]
b = [['vlan 158', 'name MARKETING', 'mode vpc'], ['vlan 159', 'name SALES', 'mode vpc']]
x = a.pop()
y = b.pop()
for i in range(len(y)):
for j in range(i + 1, len(x)):
if not(x[i] == y[j]):
print(x[i],y[j])
Posts: 42
Threads: 12
Joined: Jul 2018
Aug-04-2018, 08:32 AM
(This post was last modified: Aug-04-2018, 08:33 AM by Nirmal.)
@ Vysero : hey no this is no hw assignment .This i am doing as corporate task.The thing is i cant "pop" as the list content can be extended .It needs to be a generic one . So the first element of "list a" ( e:g vlan 158" in this case ) has to be checked in "list b" and vice versa and if it is found then fine. If it is not found ( e:g vlan 159 ) is not available in "list a" then it should return everything i.e "list a " has below missing
vlan 159
name SALES
mode vpc
Posts: 2,953
Threads: 48
Joined: Sep 2016
If there are no repeating elements in the lists you can turn them into sets. Here you can see what you can do with sets.
Posts: 42
Threads: 12
Joined: Jul 2018
Aug-04-2018, 04:54 PM
(This post was last modified: Aug-04-2018, 04:54 PM by Nirmal.)
i tried
print ("+++++++++++++++++ The missing configuration is++++++++++++++\n")
p = [item for index, item[1] in enumerate(list2) if [] != [it for it in item if it not in list1[index]]]
print('\n'.join(['\n'.join(item[1]) for item in p]))
print ("+++++++++++++++++ The missing configuration is++++++++++++++\n")
q = [item for index, item[1] in enumerate(list1) if [] != [it for it in item if it not in list2[index]]]
print('\n'.join(['\n'.join(item[1]) for item in q])) output:
+++++++++++++++++ The missing configuration is++++++++++++++
vlan 159
name SALES
mode vpc
vlan 159
name SALES
mode vpc
+++++++++++++++++ The missing configuration is++++++++++++++ repeated ,.....i am missing something here..please suggest.
Posts: 12,028
Threads: 485
Joined: Sep 2016
Another approach:
>>> a = [['vlan 158', ' name MARKET', ' mode vpc'], []]
>>> b = [['vlan 158', ' name MARKETING', ' mode vpc'], ['vlan 159', ' name SALES', ' mode vpc']]
>>> c = []
>>> d = []
>>> for item in a:
... c.append(set(item))
...
>>> for item in b:
... d.append(set(item))
...
>>> for n in range(len(c)):
... print(c[n] & d[n])
...
{' mode vpc', 'vlan 158'}
Posts: 42
Threads: 12
Joined: Jul 2018
This does not give the desired output !!
Posts: 85
Threads: 12
Joined: May 2018
Aug-06-2018, 04:43 PM
(This post was last modified: Aug-06-2018, 04:44 PM by Vysero.)
(Aug-05-2018, 02:16 PM)Nirmal Wrote: So the first element of "list a" ( e:g vlan 158" in this case ) has to be checked in "list b" and vice versa and if it is found then fine. If it is not found ( e:g vlan 159 ) is not available in "list a" then it should return everything i.e "list a " has below
When you say: "has to be checked in "list b"" are you meaning the first element of "list a" needs to be checked against the first element of each list in "list b"? S/T each matching instance can be ignored but none matching instances will result in a 'print' of the remaining elements in that list? For instance the program would for:
a = [['vlan 158', ' name MARKET', ' mode vpc'], []]
b = [['vlan 158', ' name MARKETING', ' mode vpc'], ['vlan 159', ' name SALES', ' mode vpc']] produce the output:
#first element of list a matches first element of list b thus ignore
name SALES, mode vpc #first element of list a does not match second element of list b thus print
name MARKETING, mode vpc #second element in list a is an empty list thus will not match first element in list b thus print
name SALES, mode vpc #second element in list a is an empty list thus will not match second element in list b thus print
|