0

I have a code that prints a list of the intersections from two sequences (even duplicates) (so I can't use set() function).

here is my code:

print("This program finds the intersection of two sets")

sequenceOne = input("Please enter the space-separated elements of the first set: ")
sequenceTwo = input("Please enter the space-separated elements of the second set: ")

sequenceOne = sequenceOne.split()
sequenceTwo = sequenceTwo.split()
listOfIntersection = []


for i in sequenceOne:
    for j in sequenceTwo:
        if i == j:
            sequenceTwo.remove(i)
            listOfIntersection.append(j)



print('{',(", ".join(str(i) for i in listOfIntersection)), '}')

for this code, I need to use nested loops, and the output needs to be between curly braces {}. the problem is in my output, its wrong and I don't know how to fix it.

My output:

Please enter the space-separated elements of the first set: 12 k e 34 1.5 12 hi 12 0.2
Please enter the space-separated elements of the second set: 1.5 hi 12 0.1 54 12 hi hi hi

The intersection of these two sets is { 12, 12, 1.5, hi, hi, hi }

desired output:

The intersection of these two sets is {12, 12, 1.5, hi}

Could you please help me with it?

MSS98
  • 15
  • 7
  • Is the problem with _formatting_ (extra spaces before the first item and after the last item), or _content_ (`hi` appearing three times instead of once)? – John Gordon Mar 17 '21 at 15:41
  • I am not sure why you would expect 12 to be printed twice but 'hi' to only be printed once. You should run this in DeBug mode, you will see exactly what it's doing and why the results are what you are getting – JD2775 Mar 17 '21 at 15:43
  • @JohnGordon, the spaces are also a problems, but the main one is (hi) appearing more than once. since it only intersects once between the two sequences – MSS98 Mar 17 '21 at 15:46
  • @JD2775 its basically finding the intersections or matches between the two sequences. if it occurs in both sequences, it adds it to the printout, so 12 appear twice in both sequences, where hi appear once in the first sequence – MSS98 Mar 17 '21 at 15:47
  • DarrylG Yes, I have tried this method and it works, but its not nested loops. I need to implement nested loops – MSS98 Mar 17 '21 at 16:12
  • @MSS98--did you check out all the methods in the link? The method by DrFalcon for instance (last method) uses nested loops. – DarrylG Mar 17 '21 at 16:21
  • Use a set instead of a list for keeping the intersections. – John Gordon Mar 17 '21 at 18:01
  • @JohnGordon using set, will remove duplicates. where in this program, if a duplicate occurs it should also store it. – MSS98 Mar 17 '21 at 18:25
  • I'm really confused -- earlier you said that printing `hi` more than once is a problem, but now you say that each duplicate should be kept? – John Gordon Mar 17 '21 at 18:35
  • @JohnGordon basically if the duplicate occurs in both sets. for example: ' ' 'seq one: 12 hi 12 hi hi ' ' ' seq Two: 12 12 hi 2 ' ' ' the output should be {12, 12, hi} so if the number or string occurs in both sequences. like connect the same element to each other once – MSS98 Mar 18 '21 at 15:10
  • @JohnGordon so its every time you find a couple (1 from seq one and 1 from seq two) – MSS98 Mar 18 '21 at 15:20

0 Answers0