I used this way to make all combinations:
import itertools
lst = [[1, 2, 3], [1,2,2,4]]
combs = []
for i in xrange(1, len(lst)+1):
combs.append(i)
els = [list(x) for x in itertools.combinations(lst, i)]
combs.append(els)
But what I want is that each list includes every possible combinations of elements inside. With the solution above, each element pairs are scattered. How can I solve it?
Are you looking for something like this?
import itertools
lst = [1, 2, 3], [1, 2, 2, 4]
combs = []
for i in range(len(lst)):
els = [list(x) for x in itertools.combinations(lst[i], 2)]
combs.append(els)
print(combs)
Output
[[[1, 2], [1, 3], [2, 3]], [[1, 2], [1, 2], [1, 4], [2, 2], [2, 4], [2, 4]]]
Since you said, you want - each element in its own list make pairs and put back in that list, so i am assuming that [1, 2, 3] should be converted to [[1, 2], [1, 3], [2, 3]]. The above example does the same thing!
Update
If you want to generate all possible combinations (of length greater than 1) from those lists, then you can do something like this.
import itertools
lst = [1, 2, 3], [1, 2, 2, 4]
combs = []
for i in range(len(lst)):
temp_list = []
for j in range(len(lst[i])+1):
if j < 2: # skipping zero and one length combinations
continue
els = [x for x in itertools.combinations(lst[i], j)]
temp_list.extend(els)
# remove duplicate combinations
new_list = []
[new_list.append(i) for i in temp_list if not new_list.count(i)]
combs.append(new_list)
print(combs)
Output
[[(1, 2), (1, 3), (2, 3), (1, 2, 3)], [(1, 2), (1, 4), (2, 2), (2, 4),
(1, 2, 2), (1, 2, 4), (2, 2, 4), (1, 2, 2, 4)]]
Related
I have a list like this,
l=[1,2,3,4]
Now I want to create other lists from l where next value will be disappeared every time, so the output will be
[[2,3,4], [1,3,4],[1,2,4], [1,2,3]]
Few more examples,
[1,2] --> [[2], [1]]
[10,32,15] --->[[32,15], [10,15], [10,32]]
I could do it using a for loop but am looking for a pythonic way to do it more efficiently.
You can use itertools.combinations for the job
list(itertools.combinations([1,2,3,4], 3))
[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
If you need the elements to be lists instead of tuples, a slight modification will do:
[list(elem) for elem in itertools.combinations([1,2,3,4], 3)]
[[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
> old_list = [1, 2, 3, 4]
> new_list = []
> for ITEM in old_list:
> copy_old_list = old_list[:]
> copy_old_list.remove(ITEM)
> new_list.append(copy_old_list)
> print(new_list)
[2, 3, 4], [1, 3, 4], [1, 2, 4], [1, 2, 3]]
In summary, add the entire list minus the item to a new list
Given an array of integers, how can we generate all the increasing subsequences such that all of them have same length ?
Example: given this list
l = [1, 2, 4, 5, 3, 6]
The answer should be if we consider subsequences of length 4:
[1, 2, 3, 6]
[1, 2, 4, 5]
[1, 2, 4, 6]
[1, 2, 5, 6]
[1, 4, 5, 6]
[2, 4, 5, 6]
from itertools import combinations
# the second item of the tuple is the position of the element in the array
zeta = [(1, 1), (2, 2), (4, 3), (5, 4), (3, 5), (6, 6)]
comb = combinations(sorted(zeta, key=lambda x: x[0]), 4)
def verif(x):
l = []
for k in x:
l.append(k[1])
for i in range(len(l)-1):
if l[i+1]-l[i] < 0:
return 0
return 1
for i in comb:
if verif(list(i)):
print(i)
I want a better approach like dynamic programming solution, because obviously my solution is very slow for bigger list of integers. Is LIS problem helpful in this situation?
I have a list of, say, 3 players:
[1, 2, 3]
How can I generate, in python, a list of lists, of the form:
[[1], [2], [3], [1,2], [1,3], [2,3], [1,2,3]]
representing all teams that can be formed with the above players?
You could use itertools.combinations() where we can set r parameter to all lengths from 1 to length of our list (x), to get all possible combinations that are going to be flattened in list comprehension.
from itertools import combinations
x = [1, 2, 3]
result = [c for i in range(1, len(x)+1) for c in combinations(x, i)]
print(result) # -> [(1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
Use https://docs.python.org/3/library/itertools.html#itertools.combinations
It does exactly what you want.
import itertools
players = [1, 2, 3]
print(list(itertools.chain.from_iterable(itertools.combinations(players, r) for r in range(1, len(players) + 1))))
Output:
[(1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
This is probably the most efficient answer due to the use of itertools.chain
You can use itertools.combinations with a given size to generate fixed size combinations. In order to generalize, you can just use a for loop over all the sizes. The code would look like this:
import itertools
my_list = [1, 2, 3]
for L in range(0, len(my_list)+1):
for subset in itertools.combinations(my_list, L):
print(subset)
Itertools is you friend for these kind of operations:
import itertools
l = [1, 2, 3]
l = [list(x) for y in range(1,len(l)+1) for x in itertools.combinations(l,y) ]
print(l)
Gives:
[[1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]
You can use itertools. What you want to do is generate powerset of the given list.
>>> import itertools
>>> a=[1,2,3]
>>> out=[]
>>> for i in range(len(a)+1):
out+=list(itertools.combinations(a,i))
>>> out
[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
>>>
You can write recursive function to generate powerset as follows:
>>> def powerset(s,idx,curr,out):
if idx==len(s):
out.append(curr)
return
(powerset(s,idx+1,curr+[s[idx]],out))
(powerset(s,idx+1,curr,out))
return sorted(out,key=lambda x:len(x))
>>> z=powerset(a,0,[],[])
>>> z
[[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]
I just extracted some data from a list using python but think it's overcomplicated and unpythonic and there's probably a much better way to do this. I'm actually pretty sure I saw this somewhere in the standard library docs but my brain refuses to tell me where.
So here it goes:
Input:
x = range(8) # any even sequence
Output:
[[0, 1], [2, 3], [4, 5], [6, 7]]
My take:
[ [x[i], x[i+1]] for i in range(len(x))[::2] ]
Tuples?
In Python 2.n
>>> zip(*2*[iter(x)])
[(0, 1), (2, 3), (4, 5), (6, 7)]
In Python 3.n
zip() behaves slightly differently...
>> zip(*2*[iter(x)])
<zip object at 0x285c582c>
>>> list(zip(*2*[iter(x)])])
[(0, 1), (2, 3), (4, 5), (6, 7)]
Lists?
The implementation is the same in Python 2 and 3...
>>> [[i,j] for i,j in zip(*2*[iter(x)])]
[[0, 1], [2, 3], [4, 5], [6, 7]]
Or, alternatively:
>>> [list(t) for t in zip(*2*[iter(x)])]
[[0, 1], [2, 3], [4, 5], [6, 7]]
The latter is more useful if you want to split into lists of 3 or more elements, without spelling it out, such as:
>>> [list(t) for t in zip(*4*[iter(x)])]
[[0, 1, 2, 3], [4, 5, 6, 7]]
If zip(*2*[iter(x)]) looks a little odd to you (and it did to me the first time I saw it!), take a look at How does zip(*[iter(s)]*n) work in Python?.
See also this pairwise implementation, which I think is pretty neat.
If you want tuples instead of lists you can try:
>>> zip(range(0, 8, 2), range(1, 8, 2))
[(0, 1), (2, 3), (4, 5), (6, 7)]
Input:
x = range(8) # any even sequence
Solution:
output = []
for i, j in zip(*[iter(x)]*2):
output.append( [i, j] )
Output:
print output
[[0, 1], [2, 3], [4, 5], [6, 7]]
You can rewrite it a bit:
>>> l = range(8)
>>> [[l[i], l[i+1]] for i in xrange(0, len(l), 2)]
[[0, 1], [2, 3], [4, 5], [6, 7]]
For some list tasks you can use itertools, but I'm pretty sure there's no helper function for this one.
I have a list of lists containing tuples:
[[(1L,)], [(2L,)], [(3L,)], [(4L,)], [(5L,)]
how do i edit the list so the list looks like:
l = [[1][2][3][4][5]]
>>> a
[[(1L,)], [(2L,)], [(3L,)], [(4L,)], [(5L,)]]
>>> a = [[x[0][0]] for x in a]
>>> a
[[1L], [2L], [3L], [4L], [5L]]
if you have, for instance, two items in each sub-list you would need something like this
example_list = [[(1, 0)], [(1, 1)], [(1, 3)], [(1, 4)], [(1, 5)]]
example_list = [[x[0][0], x[0][1]] for x in example_list]
print(example_list)
Output: [[1, 0], [1, 2], [1, 3], [1, 4], [1, 5]]
(note: this is using Python 3.8)