0

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

I want combinations of non- adjacent elements of the list

16th Jan 2022, 4:41 PM
Anna Hari
Anna Hari - avatar
10 Answers
0
You can use the permutations() function by importing the itertools module
16th Jan 2022, 4:58 PM
Kamil Hamid
Kamil Hamid - avatar
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
16th Jan 2022, 5:00 PM
Anna Hari
Anna Hari - avatar
0
Kamil Hamid but I need non-adjacent elements. If you know how plz send the code
16th Jan 2022, 5:02 PM
Anna Hari
Anna Hari - avatar
0
Do you need a non adjacent elements? Or just in general
16th Jan 2022, 5:11 PM
Kamil Hamid
Kamil Hamid - avatar
0
Combinations of non-adjacent elements
16th Jan 2022, 5:14 PM
Anna Hari
Anna Hari - avatar
0
All combinations?
16th Jan 2022, 5:25 PM
Kamil Hamid
Kamil Hamid - avatar
0
Yes
16th Jan 2022, 5:27 PM
Anna Hari
Anna Hari - avatar
0
Start your code, link it in the question using + button and describe your difficulties. This way, you learn as you get help.
17th Jan 2022, 2:46 AM
Emerson Prado
Emerson Prado - avatar
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.
17th Jan 2022, 2:58 AM
Anna Hari
Anna Hari - avatar
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
17th Jan 2022, 8:46 AM
Bob_Li
Bob_Li - avatar