Python Forum
Nested for loops - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Nested for loops (/thread-21567.html)



Nested for loops - d79danny - Oct-04-2019

Hey Py community,
Im very new to Python so I trying to learn by giving myself small projects at work. Here is a basic version of what I'm
trying to do. The output I'm looking for is:
Output:
Gi0/1 Gi0/3 Gi0/4 Gi0/5 Gi0/6 Gi0/7 Gi0/9
But the code and the output I getting is this.

code:
a = ("Gi0/1", "Gi0/2", "Gi0/3", "Gi0/4", "Gi0/5", "Gi0/6", "Gi0/7", "Gi0/8", "Gi0/9", "Gi0/10")
b = ("Gi0/2", "Gi0/8", "Gi0/10")

for num_a in a: 
    for num_b in b:
        if num_a == num_b:
            print(num_a)
Output:
Output:
Gi0/2 Gi0/8 Gi0/10
I also tried if not num_a == num_b:

If someone can point me in the right direction I would greatly appreciate it.

Danny


RE: Nested for loops - ichabod801 - Oct-04-2019

The typical way you would do this in Python is with the 'in' operator:

a = ("Gi0/1", "Gi0/2", "Gi0/3", "Gi0/4", "Gi0/5", "Gi0/6", "Gi0/7", "Gi0/8", "Gi0/9", "Gi0/10")
b = ("Gi0/2", "Gi0/8", "Gi0/10")
 
for num_a in a: 
    if num_a in b:
        print(num_a)
Note that the way to check if something is not equal to another thing is with the != operator.


RE: Nested for loops - d79danny - Oct-04-2019

Thanks ichabod801
Your code still outputs
Gi0/2
Gi0/8
Gi0/10

How ever I do get the output I'm looking for if I add the not in the if statement.
Would this be the correct usage of not?

a = ("Gi0/1", "Gi0/2", "Gi0/3", "Gi0/4", "Gi0/5", "Gi0/6", "Gi0/7", "Gi0/8", "Gi0/9", "Gi0/10")
b = ("Gi0/2", "Gi0/8", "Gi0/10")

for num_a in a:
if not num_a in b:
print(num_a)


RE: Nested for loops - ichabod801 - Oct-04-2019

First of all, I'm sorry, my bad.

Second of all, use Python tags.

Finally, it is normally done if num_a not in b:.


RE: Nested for loops - newbieAuggie2019 - Oct-04-2019

(Oct-04-2019, 10:13 PM)d79danny Wrote: a = ("Gi0/1", "Gi0/2", "Gi0/3", "Gi0/4", "Gi0/5", "Gi0/6", "Gi0/7", "Gi0/8", "Gi0/9", "Gi0/10")
b = ("Gi0/2", "Gi0/8", "Gi0/10")
The output I'm looking for is:
Output:
Gi0/1 Gi0/3 Gi0/4 Gi0/5 Gi0/6 Gi0/7 Gi0/9

Hi!

I was thinking about another way. Not sure if it is what you are looking for, or even if it is very pythonic, but here it is:
a = ("Gi0/1", "Gi0/2", "Gi0/3", "Gi0/4", "Gi0/5", "Gi0/6", "Gi0/7", "Gi0/8", "Gi0/9", "Gi0/10")
b = ("Gi0/2", "Gi0/8", "Gi0/10")

c = (set(a)-set(b))
print(sorted(c))
that produces the following output:
Output:
['Gi0/1', 'Gi0/3', 'Gi0/4', 'Gi0/5', 'Gi0/6', 'Gi0/7', 'Gi0/9']
All the best,

Hi again!

Just making a little twist to my previous program, I have now:

a = ("Gi0/1", "Gi0/2", "Gi0/3", "Gi0/4", "Gi0/5", "Gi0/6", "Gi0/7", "Gi0/8", "Gi0/9", "Gi0/10")
b = ("Gi0/2", "Gi0/8", "Gi0/10")

c = (set(a)-set(b))
print(*sorted(c), sep="\n")
that produces the following output, that I think it corresponds to what you wanted:
Output:
Gi0/1 Gi0/3 Gi0/4 Gi0/5 Gi0/6 Gi0/7 Gi0/9
I'll explain:
line 4:
c = (set(a)-set(b))
creates a new set named 'c', as a result from the subtraction of sets 'a' - 'b'.
line 5:
print(*sorted(c), sep="\n")
The asterisk (*) separates the elements of the ordered (sorted) new set 'c', separating each element with the separator (sep) equal to "\n", that is to say, printing each element on a different line.

All the best,


RE: Nested for loops - d79danny - Oct-04-2019

Hi ichabod801
no need to apologize I'm grateful for you help you pointed me in the right direction. I took 1 python class last year and trying to relearn it. I'm not familiar with python tags.

Hi newbieAuggie2019
I will try that code out however not sure if it will work in my situation. Im looping though 2 files and comparing regex groups out of each file. I just simplified it with my sample code. I will give it a try and let you know.

Thanks to the both of you.


RE: Nested for loops - newbieAuggie2019 - Oct-04-2019

(Oct-04-2019, 10:52 PM)d79danny Wrote: I will try that code out however not sure if it will work in my situation. Im looping though 2 files and comparing regex groups out of each file.
I think it should work if 'b' is a subset of 'a', I mean, ALL THE ELEMENTS IN 'B' ARE ALSO IN 'A', and you want to take them out. If the case is that some are and some are not, it probably won't work as it is.

All the best,


RE: Nested for loops - ichabod801 - Oct-05-2019

One thing about Auggie's solution is that it will only work on hashable items. It should work fine with strings, but I'm not sure if it would work well with regex matches. Regex matches are hashable, but they also need to be equal.


RE: Nested for loops - newbieAuggie2019 - Oct-05-2019

(Oct-05-2019, 12:24 AM)ichabod801 Wrote: One thing about Auggie's solution is that it will only work on hashable items. It should work fine with strings, but I'm not sure if it would work well with regex matches. Regex matches are hashable, but they also need to be equal.

d79danny, I have no idea about hashable items or regex matches, Huh so if you want to be sure, it's better to follow ichabod801's advice.

All the best,