My head is like a bomb right now, I can't understand what's wrong here?
config = open('s1','r').read().splitlines()
new = open('s2','r').read().splitlines()
for clean1 in config:
x = clean1.split(" ")
for clean2 in new
x2 = clean2.split(" ")
if x[0] in x2[0]:
print x[0] + " already exists."
break
if x[0] not in x2[0]:
print x[0] + " is new."
break
Let me explain:
In the file s1 we got:
192.168.1.1 test test
192.168.1.2 test test
In the file s2 we got:
192.168.1.1 test test
192.168.1.2 test test
192.168.1.3 test test
Regarding to this condition:
if x[0] in x2[0]:
print x[0] + " already exists."
break
if x[0] not in x2[0]:
print x[0] + " is new."
break
The results must be:
192.168.1.1 already exists.
192.168.1.2 already exists.
192.168.1.3 is new.
But the results are:
192.168.1.1 already exists.
192.168.1.2 is new.
I want a solution for this problem, if you can help me.
Important notice:
Do not give me a solution with set() or any kind of library which find unique records. I want a classic-solution.
>>> s1 = open('s1', 'r').readlines()
>>> s2 = open('s2', 'r').readlines()
>>> s1Codes = [x.split()[0] for x in s1]
>>> s2Codes = [x.split()[0] for x in s2]
>>> newCodes = [code for code in s2Codes if code not in s1Codes]
>>> print (newCodes)
192.168.1.3
Or if you would like to stick to something similar to your solution:
>>> s1 = open('s1', 'r').readlines()
>>> s2 = open('s2', 'r').readlines()
>>> s1Codes = [x.split()[0] for x in s1]
>>> s2Codes = [x.split()[0] for x in s2]
>>> for code in s2Codes:
... if code in s1Codes:
... print(code + " already exists")
... else:
... print(code + " is a new code")
192.168.1.1 already exists
192.168.1.2 already exists
192.168.1.3 is a new code
However, as others have stated, the use of set()
would be ideal here.