Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested for loops
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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)
Reply
#4
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:.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(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,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#6
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.
Reply
#7
(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,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#8
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
(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,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  reduce nested for-loops Phaze90 11 1,757 Mar-16-2023, 06:28 PM
Last Post: ndc85430
  Nested for loops: Iterating over columns of a DataFrame to plot on subplots dm222 0 1,638 Aug-19-2022, 11:07 AM
Last Post: dm222
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,531 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  breaking out of nested loops Skaperen 3 1,174 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  Break out of nested loops muzikman 11 3,239 Sep-18-2021, 12:59 PM
Last Post: muzikman
  How to break out of nested loops pace 11 5,262 Mar-03-2021, 06:25 PM
Last Post: pace
  Nested for Loops sammay 1 7,277 Jan-09-2021, 06:48 PM
Last Post: deanhystad
  How to make this function general to create binary numbers? (many nested for loops) dospina 4 4,330 Jun-24-2020, 04:05 AM
Last Post: deanhystad
  Python beginner - nested while loops mikebarden 1 1,838 Jun-01-2020, 01:04 PM
Last Post: DPaul
  best way out of nested loops? Skaperen 7 3,702 May-30-2020, 05:20 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020