How to get combinations of non-adjacent elements of the list | Sololearn: Learn to code for FREE!

0

How to get combinations of non-adjacent elements of the list

I want combinations of non- adjacent elements of the list

1/16/2022 4:41:05 PM

Anna Hari

11 Answers

New Answer

0

Please provide an example of what you want. (input/output)

0

You can use the permutations() function by importing the itertools module

0

Input : [1,2,3,4,5] Output : [[1,3,5],[2,4],[1,4],[2,5],[5,1],[3,5],[1,3]] like this those are non-adjacent elements

0

Kamil Hamid but I need non-adjacent elements. If you know how plz send the code

0

Do you need a non adjacent elements? Or just in general

0

Combinations of non-adjacent elements

0

All combinations?

0

0

Start your code, link it in the question using + button and describe your difficulties. This way, you learn as you get help.

0

Emerson Prado sir but I don't know the logic of getting combinations of non-adjacent elements of the list. So I am not able to write the code. If you get logic sir plz send me your code.

0

something like this? basically creating a list of slices separated by 2 steps --> a[i::2] but also becoming separated by more and more steps --> a[i::2+j] and calling the function recursively until all are reduced to 2-item lists. revised my code. It seems like passing mutable data like empty list as function argument have the unexpected side-effect of values in the list accumulating for subsequent function calls. discussion here: https://stackoverflow.com/questions/366422/what-is-the-pythonic-way-to-avoid-default-parameters-that-are-empty-lists used the suggested hack and the program seems to be working better now. https://code.sololearn.com/cA96NYIlZ9Ip/?ref=app