Nested for loops
 Nested for loops d79danny Unladen Swallow Posts: 3 Threads: 1 Joined: Oct 2019 Reputation: Oct-04-2019, 09:09 PM (This post was last modified: Oct-04-2019, 09:32 PM by ichabod801.) 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 ichabod801 Bunny Rabbit Posts: 4,231 Threads: 97 Joined: Sep 2016 Reputation: Oct-04-2019, 09:35 PM 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 d79danny Unladen Swallow Posts: 3 Threads: 1 Joined: Oct 2019 Reputation: Oct-04-2019, 10:13 PM 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 ichabod801 Bunny Rabbit Posts: 4,231 Threads: 97 Joined: Sep 2016 Reputation: Oct-04-2019, 10:24 PM 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 newbieAuggie2019 Splitter Posts: 195 Threads: 21 Joined: Aug 2019 Reputation: Oct-04-2019, 10:35 PM (This post was last modified: Oct-04-2019, 10:49 PM by newbieAuggie2019.) (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 d79danny Unladen Swallow Posts: 3 Threads: 1 Joined: Oct 2019 Reputation: Oct-04-2019, 10:52 PM 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 newbieAuggie2019 Splitter Posts: 195 Threads: 21 Joined: Aug 2019 Reputation: Oct-04-2019, 11:00 PM (This post was last modified: Oct-04-2019, 11:01 PM by newbieAuggie2019.) (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 ichabod801 Bunny Rabbit Posts: 4,231 Threads: 97 Joined: Sep 2016 Reputation: Oct-05-2019, 12:24 AM 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 newbieAuggie2019 Splitter Posts: 195 Threads: 21 Joined: Aug 2019 Reputation: Oct-05-2019, 02:12 AM (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, 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 How to break out of nested loops pace 11 1,311 Mar-03-2021, 06:25 PM Last Post: pace Nested for Loops sammay 1 455 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 975 Jun-24-2020, 04:05 AM Last Post: deanhystad Python beginner - nested while loops mikebarden 1 577 Jun-01-2020, 01:04 PM Last Post: DPaul best way out of nested loops? Skaperen 7 1,307 May-30-2020, 05:20 PM Last Post: Skaperen alternative to nested loops for large data set JonnyEnglish 2 862 Feb-19-2020, 11:26 PM Last Post: JonnyEnglish Printing out a triangle using nested for loops MrGoat 12 2,906 Jan-16-2019, 07:21 PM Last Post: ichabod801 Looping and nested loops albry 5 1,959 Dec-20-2018, 12:10 PM Last Post: buran nested for loops to recursion ashkea26 1 1,463 Nov-02-2018, 09:53 AM Last Post: Larz60+ nested for loops glob devenuro 3 2,632 Sep-20-2018, 09:54 PM Last Post: ODIS

Forum Jump:

User Panel Messages

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