combinations of a list [duplicate] - python

This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 4 years ago.
I'm trying to get all the combinations of my list, but everytime two elements need to be removed, how do i remove those elements?
I tried to make a for loop two times and every time its removes two elements, but at the end it does not restore the list
indexes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for een in indexes:
for twee in indexes:
temp = indexes
if een == twee:
pass
else:
if een in temp:
temp.remove(een)
temp.remove(twee)
print(temp)
temp = indexes
i expected to output every time a list of length of 9 but the list keeps getting shorter.
the output i got was:
[2, 3, 4, 5, 6, 7, 8, 9, 10]
[2, 3, 5, 6, 7, 8, 9, 10]
[2, 3, 5, 7, 8, 9, 10]
[2, 3, 5, 7, 9, 10]
[2, 3, 5, 7, 9]
[5, 7, 9]
[5, 9]
the first list is correct, but on the next one, the 1 does not return to the list. what am i doing wrong? Also after this one is done een should equal to 1 and do it all over again, after that een should equal to 2.....
this should be the output
[2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 3, 4, 5, 6, 7, 8, 9, 10]
[0, 3, 4, 5, 6, 7, 8, 9, 10]
[0, 2, 4, 5, 6, 7, 8, 9, 10]
[0, 2, 3, 5, 6, 7, 8, 9, 10]
[0, 2, 3, 4, 6, 7, 8, 9, 10]
[0, 2, 3, 4, 5, 7, 8, 9, 10]
[0, 2, 3, 4, 5, 6, 8, 9, 10]
[0, 2, 3, 4, 5, 6, 7, 9, 10]
[0, 2, 3, 4, 5, 6, 7, 8, 10]
[0, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 3, 4, 5, 6, 7, 8, 9, 10]
[0, 3, 4, 5, 6, 7, 8, 9, 10]
[0, 1, 4, 5, 6, 7, 8, 9, 10]
and should go one untill every combination is reached

Replace temp = indexes by temp = indexes[:]

Related

Python: How to efficiently create all possible 2 element swaps of an array?

I try to generate all possible 2-element swaps of a given array.
For example:
candidate = [ 5, 9, 1, 8, 3, 7, 10, 6, 4, 2]
result = [[ 9, 5, 1, 8, 3, 7, 10, 6, 4, 2]
[ 1, 9, 5, 8, 3, 7, 10, 6, 4, 2]
[ 8, 9, 1, 5, 3, 7, 10, 6, 4, 2]
[ 3, 9, 1, 8, 5, 7, 10, 6, 4, 2]
[ 7, 9, 1, 8, 3, 5, 10, 6, 4, 2]
[10, 9, 1, 8, 3, 7, 5, 6, 4, 2]
[ 6, 9, 1, 8, 3, 7, 10, 5, 4, 2]
[ 4, 9, 1, 8, 3, 7, 10, 6, 5, 2]
[ 2, 9, 1, 8, 3, 7, 10, 6, 4, 5]
[ 5, 1, 9, 8, 3, 7, 10, 6, 4, 2]
[ 5, 8, 1, 9, 3, 7, 10, 6, 4, 2]
[ 5, 3, 1, 8, 9, 7, 10, 6, 4, 2]
[ 5, 7, 1, 8, 3, 9, 10, 6, 4, 2]
[ 5, 10, 1, 8, 3, 7, 9, 6, 4, 2]
[ 5, 6, 1, 8, 3, 7, 10, 9, 4, 2]
[ 5, 4, 1, 8, 3, 7, 10, 6, 9, 2]
[ 5, 2, 1, 8, 3, 7, 10, 6, 4, 9]
[ 5, 9, 8, 1, 3, 7, 10, 6, 4, 2]
[ 5, 9, 3, 8, 1, 7, 10, 6, 4, 2]
[ 5, 9, 7, 8, 3, 1, 10, 6, 4, 2]
[ 5, 9, 10, 8, 3, 7, 1, 6, 4, 2]
[ 5, 9, 6, 8, 3, 7, 10, 1, 4, 2]
[ 5, 9, 4, 8, 3, 7, 10, 6, 1, 2]
[ 5, 9, 2, 8, 3, 7, 10, 6, 4, 1]
[ 5, 9, 1, 3, 8, 7, 10, 6, 4, 2]
[ 5, 9, 1, 7, 3, 8, 10, 6, 4, 2]
[ 5, 9, 1, 10, 3, 7, 8, 6, 4, 2]
[ 5, 9, 1, 6, 3, 7, 10, 8, 4, 2]
[ 5, 9, 1, 4, 3, 7, 10, 6, 8, 2]
[ 5, 9, 1, 2, 3, 7, 10, 6, 4, 8]
[ 5, 9, 1, 8, 7, 3, 10, 6, 4, 2]
[ 5, 9, 1, 8, 10, 7, 3, 6, 4, 2]
[ 5, 9, 1, 8, 6, 7, 10, 3, 4, 2]
[ 5, 9, 1, 8, 4, 7, 10, 6, 3, 2]
[ 5, 9, 1, 8, 2, 7, 10, 6, 4, 3]
[ 5, 9, 1, 8, 3, 10, 7, 6, 4, 2]
[ 5, 9, 1, 8, 3, 6, 10, 7, 4, 2]
[ 5, 9, 1, 8, 3, 4, 10, 6, 7, 2]
[ 5, 9, 1, 8, 3, 2, 10, 6, 4, 7]
[ 5, 9, 1, 8, 3, 7, 6, 10, 4, 2]
[ 5, 9, 1, 8, 3, 7, 4, 6, 10, 2]
[ 5, 9, 1, 8, 3, 7, 2, 6, 4, 10]
[ 5, 9, 1, 8, 3, 7, 10, 4, 6, 2]
[ 5, 9, 1, 8, 3, 7, 10, 2, 4, 6]
[ 5, 9, 1, 8, 3, 7, 10, 6, 2, 4]]
I currently achive this by using two nested for-loops:
neighborhood = []
for node1 in range(candidate.size - 1):
for node2 in range(node1 + 1, candidate.size):
neighbor = np.copy(candidate)
neighbor[node1] = candidate[node2]
neighbor[node2] = candidate[node1]
neighborhood.append(neighbor)
The larger the array gets, the more inefficient and slower it becomes. Is there a more efficient way here that can also process arrays with three-digit contents?
Thank you!
You can use a generator if you need to use those arrays one by one (in this way, you don't need to memorize them all, you need very little space):
from itertools import combinations
def gen(lst):
for i, j in combinations(range(len(lst)), 2):
yield lst[:i] + lst[j] + lst[i:j] + lst[i] + lst[j:]
And then you can use it in this way:
for lst in gen(candidate):
# do something with your list with two swapped elements
This is going to save much space, but it's probably going to be still slow overall.
Here is a solution using NumPy. This is not space efficient (because it's memorizing all possible lists with swapped elements), but it might be much faster because of NumPy optimizations. Give it a try!
from itertools import combinations
from math import comb
arr = np.tile(candidate, (comb(len(candidate), 2), 1))
indices = np.array(list(combinations(range(len(candidate)), 2)))
arr[np.arange(arr.shape[0])[:, None], indices] = arr[np.arange(arr.shape[0])[:, None], np.flip(indices, axis=-1)]
Example (with candidate = [0, 1, 2, 3]):
>>> arr
array([[1, 0, 2, 3],
[2, 1, 0, 3],
[3, 1, 2, 0],
[0, 2, 1, 3],
[0, 3, 2, 1],
[0, 1, 3, 2]])
Notice that math.comb (which gives you the total number of possible lists with 2 swapped elements) is available only with python >= 3.8. Please have a look at this question to know how to replace math.comb in case you're using an older python version.
To swap only two items in any given list, I'd recommend using range with itertools.combinations. It is probably good to use a generator with the yield statement too, though if you are getting all results at once, it probably doesn't matter much.
from itertools import combinations
def swap2(l):
for pair in combinations(range(len(l)), 2):
l2 = l[:]
l2[pair[0]], l2[pair[1]] = l2[pair[1]], l2[pair[0]]
yield l2
if __name__ == "__main__":
candidate = [5, 9, 1, 8, 3, 7, 10, 6, 4, 2]
result = [l for l in swap2(candidate)]

shuffle a list and then append it to another list

I use a for loop to shuffle a list and append it to another empty list (List A).
I can see each shuffled list is different, but the List A has been appended with multiple of the last shuffled list only.
print('---------shuffle list-------------------------------------')
matr=[ ]
entry=[1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in range(9):
shuffle(entry )
print(entry )
matr.append(entry)
print(matr)
'''
results below:
---------shuffle list-------------------------------------
[3, 1, 7, 5, 8, 9, 2, 6, 4]
[5, 4, 6, 8, 1, 9, 7, 2, 3]
[6, 4, 7, 5, 1, 3, 2, 9, 8]
[4, 9, 8, 1, 7, 3, 6, 5, 2]
[5, 1, 9, 2, 8, 6, 4, 7, 3]
[3, 5, 1, 4, 2, 6, 8, 9, 7]
[1, 2, 4, 6, 7, 8, 3, 9, 5]
[4, 8, 1, 6, 7, 3, 5, 9, 2]
[6, 4, 2, 1, 9, 8, 3, 5, 7]
[[6, 4, 2, 1, 9, 8, 3, 5, 7], [6, 4, 2, 1, 9, 8, 3, 5, 7], [6, 4, 2, 1, 9, 8, 3, 5, 7], [6, 4, 2, 1, 9, 8, 3, 5, 7], [6, 4, 2, 1, 9, 8, 3, 5, 7], [6, 4, 2, 1, 9, 8, 3, 5, 7], [6, 4, 2, 1, 9, 8, 3, 5, 7], [6, 4, 2, 1, 9, 8, 3, 5, 7], [6, 4, 2, 1, 9, 8, 3, 5, 7]]
'''
it should have appended each of the shuffled list rather the last shuffled list.
Their same objects, so you gotta do, shuffle is an exception where you need to do this:
matr=[]
entry=[1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in range(9):
entry = entry.copy()
shuffle(entry)
print(entry)
matr.append(entry)
print(matr)
Output:
[9, 7, 6, 4, 3, 2, 8, 5, 1]
[4, 3, 2, 8, 7, 1, 5, 9, 6]
[3, 9, 7, 4, 5, 8, 2, 1, 6]
[6, 1, 9, 5, 2, 3, 4, 7, 8]
[5, 4, 7, 9, 8, 2, 6, 3, 1]
[2, 3, 5, 8, 6, 7, 9, 4, 1]
[5, 4, 9, 8, 3, 6, 1, 7, 2]
[5, 1, 2, 3, 7, 8, 6, 9, 4]
[6, 8, 9, 2, 1, 5, 3, 7, 4]
[[9, 7, 6, 4, 3, 2, 8, 5, 1], [4, 3, 2, 8, 7, 1, 5, 9, 6], [3, 9, 7, 4, 5, 8, 2, 1, 6], [6, 1, 9, 5, 2, 3, 4, 7, 8], [5, 4, 7, 9, 8, 2, 6, 3, 1], [2, 3, 5, 8, 6, 7, 9, 4, 1], [5, 4, 9, 8, 3, 6, 1, 7, 2], [5, 1, 2, 3, 7, 8, 6, 9, 4], [6, 8, 9, 2, 1, 5, 3, 7, 4]]

Concatenating columns of lists containing NaNs in a dataframe

I have a pandas df with two columns having either lists or NaN values. There are no rows having NaN in both columns. I want to create a third column that merges the values of the other two columns in the following way:-
if row df.a is NaN -> df.c = df.b
if row df.b is Nan -> df.c = df.a
else df.c = df.a + df.b
Input:-
df
a b
0 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] NaN
1 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] NaN
2 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] NaN
3 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] NaN
4 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] NaN
5 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] NaN
6 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] NaN
7 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] NaN
8 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
9 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
10 NaN [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
11 NaN [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
output:
df.c
0 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
1 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
4 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
5 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
6 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
7 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
8 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
9 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
10 [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
11 [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
I tried to use this nested condition with apply
df['c'] = df.apply(lambda x: x.a if x.b is float else (x.b if x.a is float else (x['a'] + x['b'])), axis = 1)
but is giving me this error :
TypeError: ('can only concatenate list (not "float") to list', u'occurred at index 0').
I am using ( and it's acutally working)
if x is float
because is the only way I found to separate a list from a NaN value.
When you use pd.DataFrame.stack null values are dropped by default. We can then group by the first level of the index and concatenate the lists together with sum
df.stack().groupby(level=0).sum()
0 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
1 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
4 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
5 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
6 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
7 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
8 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
9 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
10 [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
11 [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
dtype: object
We can then add it to a copy of the dataframe with assign
df.assign(c=df.stack().groupby(level=0).sum())
Or add it to a new column in place
df['c'] = df.stack().groupby(level=0).sum()
You can convert the NaNs to list, and then apply np.sum:
In [718]: df['c'] = df[['a', 'b']].applymap(lambda x: [] if x != x else x).apply(np.sum, axis=1); df['c']
Out[718]:
0 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
1 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
4 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
5 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
6 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
7 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9, ...
8 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9, ...
9 [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
10 [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
Name: c, dtype: object
This works for any number of columns that have list/NaN contents.
You can use fillna for replace NaNs to empty list first:
df = pd.DataFrame({'a': [[0, 1, 2], np.nan, [0, 1, 2]],
'b':[np.nan,[0, 1, 2],[ 5, 6, 7, 8, 9]]})
print (df)
s = pd.Series([[]], index=df.index)
df['c'] = df['a'].fillna(s) + df['b'].fillna(s)
print (df)
a b c
0 [0, 1, 2] NaN [0, 1, 2]
1 NaN [0, 1, 2] [0, 1, 2]
2 [0, 1, 2] [5, 6, 7, 8, 9] [0, 1, 2, 5, 6, 7, 8, 9]

simple way to select numpy subarray using boolean conditional vector in python 3

How can you select only the columns of a 2-d numpy array that correspond to a conditional boolean vector?
Say you have a 10x10 matrix, generated by, say:
a = np.random.randint(0,1,(10,10))
a =
array([[4, 9, 1, 9, 5, 2, 1, 7, 6, 5],
[5, 4, 2, 4, 8, 1, 5, 5, 7, 5],
[3, 8, 7, 4, 3, 4, 8, 8, 8, 3],
[5, 4, 4, 4, 9, 6, 7, 1, 6, 8],
[8, 3, 2, 1, 7, 5, 8, 8, 4, 9],
[9, 5, 6, 8, 6, 8, 1, 4, 4, 5],
[5, 4, 3, 2, 8, 3, 2, 2, 8, 6],
[2, 5, 4, 5, 9, 7, 9, 2, 5, 6],
[4, 5, 9, 7, 3, 1, 5, 7, 4, 8],
[6, 1, 3, 8, 8, 3, 2, 6, 6, 7]])
and you want to cut out all the rows corresponding to a vector containing (True/False or 0/1), like, say:
b = np.random.randint(0,2,10)
b =
array([0, 1, 1, 0, 1, 0, 1, 1, 1, 1])
I spent some time trying to find the simple syntax to return only specified colummns in a numpy array in python 3 and finally have it figured out. There are a number of other threads which show more complicated ways to do this, so I thought I would put the simple solution here. This will be very obvious to more experienced python users, but for a beginner like me, it would have been useful.
The simplest way is:
new_matrix = a[:,b==1]
which yields:
new_matrix =
array([[9, 1, 5, 1, 7, 6, 5],
[4, 2, 8, 5, 5, 7, 5],
[8, 7, 3, 8, 8, 8, 3],
[4, 4, 9, 7, 1, 6, 8],
[3, 2, 7, 8, 8, 4, 9],
[5, 6, 6, 1, 4, 4, 5],
[4, 3, 8, 2, 2, 8, 6],
[5, 4, 9, 9, 2, 5, 6],
[5, 9, 3, 5, 7, 4, 8],
[1, 3, 8, 2, 6, 6, 7]])
This would have saved me a lot of time.

Two Combination Lists from One List

I am a python beginner. I am trying to get two combination lists from one list.
For example, I have a list:
c = [1, 2, 3, 4]
I want to get every possible combination using every four items to fill two lists. There are going to be ((2^4)/2)-1 possibilities.
c1 = [1] c2 = [2, 3, 4]
c1 = [2] c2 = [1, 3, 4]
c1 = [3] c2 = [2, 3, 4]
c1 = [4] c2 = [1, 2, 3]
c1 = [1, 2] c2 = [3, 4]
c1 = [1, 3] c2 = [2, 4]
c1 = [1, 4] c2 = [2, 3]
The function usually works for this kind of task is itertools, but I cannot choose the number of lists produced by itertools.combination.
The function only allows me to choose how many items per one list should be.
For example, If I try following function,
print list(itertools.combinations(c, 2))
I can get an outcome only like this.
[(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)]
I searched pretty hard to find this, but I couldn't find anything.
Update
Oh my poor English is causing such a confusion! I totally changed my example. I wanted to allocate the 4 items to 2 lists. Sorry for the confusion!
I'm unsure as to what your understanding of 10 choose 2 is. The output you receive from list(itertools.combinations(c, 2)) is what is mathematically defined as 10C2.
EDIT
From the edit to your question, it appears that you want an entirely different kind of combinations. The number of outcomes would still not be 45, but instead: 10C1 + 10C2 + 10C3 + 10C4 + 10C5.
I expect the following should help you move forward:
for i in range(1, 6):
for c1 in itertools.combinations(c, i):
c1 = set(c1)
c2 = set(c) - c1
print c1, c2
The above code was inspired by this (deleted) answer by CSZ.
The output received when range(1, 3) is used:
[1] [2, 3, 4, 5, 6, 7, 8, 9, 10]
[2] [1, 3, 4, 5, 6, 7, 8, 9, 10]
[3] [1, 2, 4, 5, 6, 7, 8, 9, 10]
[4] [1, 2, 3, 5, 6, 7, 8, 9, 10]
[5] [1, 2, 3, 4, 6, 7, 8, 9, 10]
[6] [1, 2, 3, 4, 5, 7, 8, 9, 10]
[7] [1, 2, 3, 4, 5, 6, 8, 9, 10]
[8] [1, 2, 3, 4, 5, 6, 7, 9, 10]
[9] [1, 2, 3, 4, 5, 6, 7, 8, 10]
[10] [1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2] [3, 4, 5, 6, 7, 8, 9, 10]
[1, 3] [2, 4, 5, 6, 7, 8, 9, 10]
[1, 4] [2, 3, 5, 6, 7, 8, 9, 10]
[1, 5] [2, 3, 4, 6, 7, 8, 9, 10]
[1, 6] [2, 3, 4, 5, 7, 8, 9, 10]
[1, 7] [2, 3, 4, 5, 6, 8, 9, 10]
[8, 1] [2, 3, 4, 5, 6, 7, 9, 10]
[1, 9] [2, 3, 4, 5, 6, 7, 8, 10]
[1, 10] [2, 3, 4, 5, 6, 7, 8, 9]
[2, 3] [1, 4, 5, 6, 7, 8, 9, 10]
[2, 4] [1, 3, 5, 6, 7, 8, 9, 10]
[2, 5] [1, 3, 4, 6, 7, 8, 9, 10]
[2, 6] [1, 3, 4, 5, 7, 8, 9, 10]
[2, 7] [1, 3, 4, 5, 6, 8, 9, 10]
[8, 2] [1, 3, 4, 5, 6, 7, 9, 10]
[9, 2] [1, 3, 4, 5, 6, 7, 8, 10]
[2, 10] [1, 3, 4, 5, 6, 7, 8, 9]
[3, 4] [1, 2, 5, 6, 7, 8, 9, 10]
[3, 5] [1, 2, 4, 6, 7, 8, 9, 10]
[3, 6] [1, 2, 4, 5, 7, 8, 9, 10]
[3, 7] [1, 2, 4, 5, 6, 8, 9, 10]
[8, 3] [1, 2, 4, 5, 6, 7, 9, 10]
[9, 3] [1, 2, 4, 5, 6, 7, 8, 10]
[10, 3] [1, 2, 4, 5, 6, 7, 8, 9]
[4, 5] [1, 2, 3, 6, 7, 8, 9, 10]
[4, 6] [1, 2, 3, 5, 7, 8, 9, 10]
[4, 7] [1, 2, 3, 5, 6, 8, 9, 10]
[8, 4] [1, 2, 3, 5, 6, 7, 9, 10]
[9, 4] [1, 2, 3, 5, 6, 7, 8, 10]
[10, 4] [1, 2, 3, 5, 6, 7, 8, 9]
[5, 6] [1, 2, 3, 4, 7, 8, 9, 10]
[5, 7] [1, 2, 3, 4, 6, 8, 9, 10]
[8, 5] [1, 2, 3, 4, 6, 7, 9, 10]
[9, 5] [1, 2, 3, 4, 6, 7, 8, 10]
[10, 5] [1, 2, 3, 4, 6, 7, 8, 9]
[6, 7] [1, 2, 3, 4, 5, 8, 9, 10]
[8, 6] [1, 2, 3, 4, 5, 7, 9, 10]
[9, 6] [1, 2, 3, 4, 5, 7, 8, 10]
[10, 6] [1, 2, 3, 4, 5, 7, 8, 9]
[8, 7] [1, 2, 3, 4, 5, 6, 9, 10]
[9, 7] [1, 2, 3, 4, 5, 6, 8, 10]
[10, 7] [1, 2, 3, 4, 5, 6, 8, 9]
[8, 9] [1, 2, 3, 4, 5, 6, 7, 10]
[8, 10] [1, 2, 3, 4, 5, 6, 7, 9]
[9, 10] [1, 2, 3, 4, 5, 6, 7, 8]
l = [1,2,3,4, 5, 6, 7, 8]
print [[l[:i], l[i:]] for i in range(1, len(l))]
If you want all combinations. you can do like this.
print [l[i:i+n] for i in range(len(l)) for n in range(1, len(l)-i+1)]
or
itertools.combinations

Categories

Resources