running a nested loop on a list of unknown length


running a nested loop on a list of unknown length



I have defined a list as:


comp =
comp.append(["A", "B", "C", "D"])
comp.append(["E", "F", "I"])



In real case, I don't know the length of comp or comp[x]


comp


comp[x]



Now, I am trying to run a nested loop on this, but failed. What I mean is,
if I run my current snippet:


for compsi in range(len(comp)):
for elemn in range(len(comp[compsi])):
print(comp[compsi][elemn])



the output is A B C D E F I.


A B C D E F I



What I am trying is, for each element of comp[0], the full comp[1] will run, so that, I will get:
A E F I B F I C F I D E F I and so;


comp[0]


comp[1]


A E F I B F I C F I D E F I



When I know I len(comp) = 2, I can easily do this using nested for loop as:


len(comp)


for lo in range(len(comp[0])):
for l1 in range(len(comp[1])):
print(...)



But, how I can achieve the same when I don't know the lenght of comp?


comp



Kindly help!



Ok, say, we have comp=[['A', 'B', 'C', 'D'], ['E', 'F', 'G'], ['H'], ['I', 'J','K']]. We will end up with:


comp=[['A', 'B', 'C', 'D'], ['E', 'F', 'G'], ['H'], ['I', 'J','K']]


A E H I
A E H J
A E H K
A F H I
...
B E H I
B E H J
B E H K
...
D G H K



in this way.





how should look the combination for len(comp) == 4 ?
– RomanPerekhrest
Jul 1 at 15:58


len(comp) == 4





Kindly check the update in OP, as I cant do multiline math mode here
– BaRud
Jul 1 at 16:01






The first and second example in your post are different. Which one do you want? (Given the logic of the second example and using the input from the first example, the output should be AE, AF, AI, BE, BF, BI,... - this is called the cartesian product)
– mkrieger1
Jul 1 at 16:06






recursion on the loop for the next element?
– Eran
Jul 1 at 16:06






You are using for loop in the wrong place, also u are using it in a wrong way
– DarkSuniuM
Jul 1 at 16:08




1 Answer
1



Assuming your second given example with the list [('A', 'B', 'C', 'D'), ('E', 'F', 'G'), ('H',), ('I', 'J', 'K')] is correct, you can use a combination of itertools.product and functools.reduce to accomplish what you're looking for:


[('A', 'B', 'C', 'D'), ('E', 'F', 'G'), ('H',), ('I', 'J', 'K')]


itertools.product


functools.reduce


from functools import reduce
from itertools import product
comp = [('A', 'B', 'C', 'D'), ('E', 'F', 'G'), ('H',), ('I', 'J', 'K')]
print(list(reduce(lambda a, b: [(*p[0], p[1]) for p in product(a, b)], comp)))



This outputs:


[('A', 'E', 'H', 'I'), ('A', 'E', 'H', 'J'), ('A', 'E', 'H', 'K'), ('A', 'F', 'H', 'I'), ('A', 'F', 'H', 'J'), ('A', 'F', 'H', 'K'), ('A', 'G', 'H', 'I'), ('A', 'G', 'H', 'J'), ('A', 'G', 'H', 'K'), ('B', 'E', 'H', 'I'), ('B', 'E', 'H', 'J'), ('B', 'E', 'H', 'K'), ('B', 'F', 'H', 'I'), ('B', 'F', 'H', 'J'), ('B', 'F', 'H', 'K'), ('B', 'G', 'H', 'I'), ('B', 'G', 'H', 'J'), ('B', 'G', 'H', 'K'), ('C', 'E', 'H', 'I'), ('C', 'E', 'H', 'J'), ('C', 'E', 'H', 'K'), ('C', 'F', 'H', 'I'), ('C', 'F', 'H', 'J'), ('C', 'F', 'H', 'K'), ('C', 'G', 'H', 'I'), ('C', 'G', 'H', 'J'), ('C', 'G', 'H', 'K'), ('D', 'E', 'H', 'I'), ('D', 'E', 'H', 'J'), ('D', 'E', 'H', 'K'), ('D', 'F', 'H', 'I'), ('D', 'F', 'H', 'J'), ('D', 'F', 'H', 'K'), ('D', 'G', 'H', 'I'), ('D', 'G', 'H', 'J'), ('D', 'G', 'H', 'K')]



But then given your first example of [('A', 'B', 'C', 'D'), ('E', 'F', 'I')], it would output the following instead:


[('A', 'B', 'C', 'D'), ('E', 'F', 'I')]


[('A', 'E'), ('A', 'F'), ('A', 'I'), ('B', 'E'), ('B', 'F'), ('B', 'I'), ('C', 'E'), ('C', 'F'), ('C', 'I'), ('D', 'E'), ('D', 'F'), ('D', 'I')]



I'm assuming your second example is correct because the logic behind your first example simply wouldn't make sense when it scales to a length greater than 2.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to make file upload 'Required' in Contact Form 7?

Rothschild family

amazon EC2 - How to make wp-config.php to writable?