0

How to print k pairs of sequence elements in order using list?

Example: When k=3, list is [1,2,3,4] to print element in this order [2,3,4], [3,4,1], [4,1,2], [1,2,3] this all are possible pairs also. Please help to solve this problem!!

19th Aug 2021, 5:53 PM
Sabarinathan.M
Sabarinathan.M - avatar
5 ответов
+ 2
Sabarinathan.M a=[1,2,3,4] print(list([a[1+i-4],a[2+i-4],a[3+i-4]] for i in range(4))). The above solution might not work for other cases.
19th Aug 2021, 6:31 PM
Abhay
Abhay - avatar
+ 1
You can use combination in itertools from itertools import combinations combinations(urlist, k)
19th Aug 2021, 6:00 PM
Abs Sh
Abs Sh - avatar
0
Yes, combinations creating a pairs but i want to print above order, combinations will print this order (1, 2, 3),(1, 2, 4),(1, 3, 4),(2, 3, 4) it's different from my solution
19th Aug 2021, 6:13 PM
Sabarinathan.M
Sabarinathan.M - avatar
0
What do you mean what is the order how do you define what comes first
19th Aug 2021, 6:55 PM
Abs Sh
Abs Sh - avatar
0
Abhay your solution is static that's right but I need dynamic solution, how to do?
20th Aug 2021, 1:50 AM
Sabarinathan.M
Sabarinathan.M - avatar