codicodi
codicodi

Reputation: 1

how can I print list like this output

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

Answers (1)

svtag
svtag

Reputation: 182

There are some syntax error in your code. i.e.

  1. List do not have a split() function, it is available for strings. So to print a list, just do print(listABC), it will print each element differently.
  2. 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

Related Questions