Reputation: 1
import sys
try:
strseta = sys.argv[1]
lista = [int(x) for x in strseta.split(',')]
strsetb = sys.argv[2]
listb = [int(x) for x in strsetb.split(',')]
print("Set A:" , strseta.split(','))
print("Set B:" , strsetb.split(','))
out3 = []
for i in listb:
if i in lista:
out3.append(i)
print("Intersection of A and B:", out3.split())
out4 = list(seta)
for j in listb:
if j not in lista:
out4.append(j)
print("Union of A and B:", out4.split(','))
out5 = []
for k in lista:
if k not in listb:
out5.append(k)
print("Difference of A and B:", out5.split(','))
except:
print("Your input is invalid!")
I need to do without using the set when I enter 2 sys.argv like python3 3.py 5,14,7,9,15,42 9,4,71,5 and run this code on cmd need to get this output:
Upvotes: 0
Views: 60
Reputation: 182
There are some syntax error in your code. i.e.
split()
function, it is available for strings. So to print a list, just do print(listABC)
, it will print each element differently.out4 = list(seta)
should be out4 = list(lista)
After these changes, your code will work.
EDIT
In order to avoid extra prints of the intermediate result of
Intersection of A and B:
bring the prints after the loop.
like this:
for i in listb:
if i in lista:
out3.append(i)
print("Intersection of A and B:", out3)
Upvotes: 1