Related
I need help to get a list from an other :
input :
[[1, 1], [1, 1], [2, 2], [1, 1], [1, 1], [2, 2], [3, 3], [4, 4]]
output wanted :
[0, 0, 1, 0, 0, 1, 2, 3]
I tried to use enumerate but I fail, any suggestion ?
Edit : Every time I meet a new element in the list, I associate this new element with a number (start from 0 and +1 every new element) and if I recognize it later I put the same number, so [1,1] --> 0 because is the first element we met and [2,2] --> 1 etc...
Okay I found a solution :
One more thing before, my example is bad because I can have [1,2] in element of the list for input
the solution I found is
line = [[1, 1], [1, 1], [2, 2], [1, 1], [2, 1], [2, 2], [3, 3], [4, 4]]
p = []
line_not = []
k = 0
for i in range (len(line)):
if line[i] in line[:i]:
p.append(line_not[:k].index(line[i]))
else:
p.append(k)
line_not.append(line[i])
k+=1
the output is :
[0, 0, 1, 0, 2, 1, 3, 4]
If u have a better solution, tell me !
try to make a map, this works:
inp=[[1, 1], [1, 1], [2, 2], [1, 1], [1, 1], [2, 2], [3, 3], [4, 4]]
out = [0, 0, 1, 0, 0, 1, 2, 3]
mymap={inp[0][0]:0}
output = [0]
k_count=1
for i in inp[1:]:
if i[0] in mymap.keys():
output.append(mymap[i[0]])
else:
mymap[i[0]] = k_count
output.append(mymap[i[0]])
k_count+=1
and then output == [0, 0, 1, 0, 0, 1, 2, 3]
First build a dictionary that does the assocation of each unique element with a number:
>>> x = [[1, 1], [1, 1], [2, 2], [1, 1], [1, 1], [2, 2], [3, 3], [4, 4]]
>>> d = {}
>>> for [i, _] in x:
... if i not in d:
... d[i] = len(d)
...
and then you can easily build your output list by doing lookups in that dictionary:
>>> [d[i] for [i, _] in x]
[0, 0, 1, 0, 0, 1, 2, 3]
this would work in your current example, but it is not a comprehensive solution. Without context its hard to understand what you are trying to achieve, so use with care:
import numpy as np
inp = [[1, 1], [1, 1], [2, 2], [1, 1], [1, 1], [2, 2], [3, 3], [4, 4]]
out = np.array([i[0] for i in inp]) - 1
print(out) # result: [0 0 1 0 0 1 2 3]
Hey im trying to find all k combination length of n elements with recursion without using any module
For example n = 4 so [0,1,2,3] and k=3 so the all the combination length 3 are
>>>[0,0,0],[0,0,1],[0,0,2],[0,0,3],[0,0,4],[0,1,0],[0,1,1],[0,1,2]....
I tried to think on that like a tree but i didn't mange to go from here [0,0,4] for example to here [0,1,0] all i got was [0,0,0],[0,0,1],[0,0,2],[0,0,3],[0,0,4]
We can achieve the bare minimum by using simple recursion.
def combination(n, k):
if not k:
return [[]]
res = []
nums = list(range(n + 1))
for comb in combination(n, k - 1):
for num in nums:
comb_copy = comb.copy()
comb_copy.append(num)
res.append(comb_copy)
return res
Let's take a look at how this code works. First, as with any recursion problem, we establish the base case, which is when k == 0. In this case, we would return an empty nested list.
If k != 0, then we need to perform recursion. The gist of this problem is that we need to append some numbers to the result returned by combination(n, k - 1). For example, let's say we want to obtain the result for combination(2, 2). The result returned by combination(2, 1) would be
>>> combination(2, 1)
[[0], [1], [2]]
Given this information, how can we get combination(2, 2)? For some intuition, here is the result we want:
>>> combination(2, 2)
[[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
Observe that all we need to do is to append the values 0, 1, and 2 to each element of combination(3, 1). In other words, take the first element, [0]. We append 0, 1, and 2 to this list, which results in [0, 0], [0, 1], [0, 2]. These are the first three elements of combination(3, 2).
Back to the code, we first call combination(n, k - 1), and append num to each list in the nested list returned by that function call. Finally, when the appending is over, we return res.
One fine detail here is that we create a copy of the list instead of appending to it directly. We do this in order to prevent modifying the original comb.
Here is the function in action with n = 4, k = 3:
>>> combination(4, 3)
[[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 0, 3], [0, 0, 4], [0, 1, 0], [0, 1, 1], [0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 0], [0, 2, 1], [0, 2, 2], [0, 2, 3], [0, 2, 4], [0, 3, 0], [0, 3, 1], [0, 3, 2], [0, 3, 3], [0, 3, 4], [0, 4, 0], [0, 4, 1], [0, 4, 2], [0, 4, 3], [0, 4, 4], [1, 0, 0], [1, 0, 1], [1, 0, 2], [1, 0, 3], [1, 0, 4], [1, 1, 0], [1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 1, 4], [1, 2, 0], [1, 2, 1], [1, 2, 2], [1, 2, 3], [1, 2, 4], [1, 3, 0], [1, 3, 1], [1, 3, 2], [1, 3, 3], [1, 3, 4], [1, 4, 0], [1, 4, 1], [1, 4, 2], [1, 4, 3], [1, 4, 4], [2, 0, 0], [2, 0, 1], [2, 0, 2], [2, 0, 3], [2, 0, 4], [2, 1, 0], [2, 1, 1], [2, 1, 2], [2, 1, 3], [2, 1, 4], [2, 2, 0], [2, 2, 1], [2, 2, 2], [2, 2, 3], [2, 2, 4], [2, 3, 0], [2, 3, 1], [2, 3, 2], [2, 3, 3], [2, 3, 4], [2, 4, 0], [2, 4, 1], [2, 4, 2], [2, 4, 3], [2, 4, 4], [3, 0, 0], [3, 0, 1], [3, 0, 2], [3, 0, 3], [3, 0, 4], [3, 1, 0], [3, 1, 1], [3, 1, 2], [3, 1, 3], [3, 1, 4], [3, 2, 0], [3, 2, 1], [3, 2, 2], [3, 2, 3], [3, 2, 4], [3, 3, 0], [3, 3, 1], [3, 3, 2], [3, 3, 3], [3, 3, 4], [3, 4, 0], [3, 4, 1], [3, 4, 2], [3, 4, 3], [3, 4, 4], [4, 0, 0], [4, 0, 1], [4, 0, 2], [4, 0, 3], [4, 0, 4], [4, 1, 0], [4, 1, 1], [4, 1, 2], [4, 1, 3], [4, 1, 4], [4, 2, 0], [4, 2, 1], [4, 2, 2], [4, 2, 3], [4, 2, 4], [4, 3, 0], [4, 3, 1], [4, 3, 2], [4, 3, 3], [4, 3, 4], [4, 4, 0], [4, 4, 1], [4, 4, 2], [4, 4, 3], [4, 4, 4]]
Note that we can get fancier here by implementing things like dynamic programming, but is an optimization detail you might want to consider later.
You can use itertools.combinations
import itertools
itertools.combinations('ABCD', 2)
I create all three-element permutations without mirroring, using itertools.product():
import itertools
list_1 = [list(i) for i in itertools.product(tuple(range(4)), repeat=3) if tuple(reversed(i)) >= tuple(i)]
Output:
[[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 0, 3], [0, 1, 0], [0, 1, 1], [0, 1, 2], [0, 1, 3], [0, 2, 0], [0, 2, 1], [0, 2, 2], [0, 2, 3], [0, 3, 0], [0, 3, 1], [0, 3, 2], [0, 3, 3], [1, 0, 1], [1, 0, 2], [1, 0, 3], [1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 1], [1, 2, 2], [1, 2, 3], [1, 3, 1], [1, 3, 2], [1, 3, 3], [2, 0, 2], [2, 0, 3], [2, 1, 2], [2, 1, 3], [2, 2, 2], [2, 2, 3], [2, 3, 2], [2, 3, 3], [3, 0, 3], [3, 1, 3], [3, 2, 3], [3, 3, 3]]
How do I delete these sublisters from the list list_1, which have the same number of corresponding values and then leave only one of them?
For example, in sublists [1,1,2], [1,2,1] the number of given values is the same in all, that is, in each sub-list there are two 1 and one 2, that's why I consider the sublisters to be the same and that's why I want to leave only the first one, namely [1,1,2]. How can this be done?
I was thinking about counting the number of corresponding values in each sub-list and creating a list with the occurring feature regarding the amount of given values, and then checking each element from the list list_1 in the loop or the element with the given feature has not occurred before. But it seems to me to be very complicated.
Rather than using product from the itertools module, use combinations_with_replacement. That does what you want in one line without any massaging afterward:
list1 = [list(i) for i in combinations_with_replacement(range(4),3)]
The result of print(list1) after that is
[[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 0, 3], [0, 1, 1], [0, 1, 2], [0, 1, 3], [0, 2, 2], [0, 2, 3], [0, 3, 3], [1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 2], [1, 2, 3], [1, 3, 3], [2, 2, 2], [2, 2, 3], [2, 3, 3], [3, 3, 3]]
Note that your conversion of the range object to a tuple is not necessary.
This might do the trick:
import itertools
def uniqifyList(list):
indexToReturn = []
sortedUniqueItems = []
for idx, value in enumerate(list):
# check if exists in unique_list or not
value.sort()
if value not in sortedUniqueItems:
sortedUniqueItems.append(value)
indexToReturn.append(idx)
return [list[i] for i in indexToReturn]
list1 = [list(i) for i in itertools.product(tuple(range(4)), repeat=3) if tuple(reversed(i)) >= tuple(i)]
print(list1)
list2 = uniqifyList(list1)
print(list2)
Which outputs:
[[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 0, 3], [0, 1, 1], [0, 1, 2], [0, 1, 3], [0, 2, 2], [0, 2, 3], [0, 3, 3], [1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 2], [1, 2, 3], [1, 3, 3], [2, 2, 2], [2, 2, 3], [2, 3, 3], [3, 3, 3]]
You can sort each sublist and then extract unique sublists out as follows.
list_2 = map(sorted, list_1)
list_u = []
[list_u.append(x) for x in list_2 if x not in list_u]
Output:
list_u = [[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 0, 3], [0, 1, 1], [0, 1, 2], [0, 1, 3], [0, 2, 2], [0, 2, 3], [0, 3, 3], [1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 2], [1, 2, 3], [1, 3, 3], [2, 2, 2], [2, 2, 3], [2, 3, 3], [3, 3, 3]]
Now, there are more efficient options than sorting each sublist, but I will leave that upto you.
I am trying to produce a function that should takes as input a range, and for that range should be return all the possible permutation of that range without use any library.
for example:
per(range(3))
should return:
[[0, 1, 2], [1, 0, 2], [1, 2, 0], [0, 2, 1], [2, 0, 1], [2, 1, 0]]
I have done the following but i get an empty list.
def per(l):
return [l[i]+p for i in range(len(l))for p in per(l[:i] + l [i+1:])]
Does anyone know why i get the empty list and how to solve it?
The issue is that the end condition for you would be when the length of list l is 0 and that returns an empty list back. Hence when length of list is 1, the inner loop never actually runs and hence this also returns an empty list back , and this keeps on happenning and you always get empty lists.
An fix would be to make the end condition when the length of list is 1, and you should return lists of lists, not simple lists. Example -
def per(l):
if len(l) == 1:
return [l]
return [[l[i]] + p for i in range(len(l))for p in per(l[:i] + l [i+1:])]
Demo -
>>> def per(l):
... if len(l) == 1:
... return [l]
... return [[l[i]] + p for i in range(len(l))for p in per(l[:i] + l [i+1:])]
...
>>>
>>> per([2])
[[2]]
>>> per([0,1,2])
[[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]
>>> per([0,1,2, 3])
[[0, 1, 2, 3], [0, 1, 3, 2], [0, 2, 1, 3], [0, 2, 3, 1], [0, 3, 1, 2], [0, 3, 2, 1], [1, 0, 2, 3], [1, 0, 3, 2], [1, 2, 0, 3], [1, 2, 3, 0], [1, 3, 0, 2], [1, 3, 2, 0], [2, 0, 1, 3], [2, 0, 3, 1], [2, 1, 0, 3], [2, 1, 3, 0], [2, 3, 0, 1], [2, 3, 1, 0], [3, 0, 1, 2], [3, 0, 2, 1], [3, 1, 0, 2], [3, 1, 2, 0], [3, 2, 0, 1], [3, 2, 1, 0]]
>>> len(per([0,1,2, 3]))
24
I have a list:
[0, 1, 2, 3, 4, 5]
I would like to combine the first item all others except the last.
The result should be a list, as the list below:
[[0], [0,1], [0,2], [0,3], [0,4], [0,1,2], [0,1,3] [0,1, 4], [0,2,3], [0,2,4], [0,3,4], [0,1,2,3], [0,1,2,4], [0, 2,3,4], [0,1,2,3,4]]
How can I do this?
Thank you!
import itertools
a = [0, 1, 2, 3, 4, 5]
base = (a[0],)
items = a[1:-1]
combos = [base + combo for length in range(len(items)+1) for combo in itertools.combinations(items, length)]
# In case it matters that the sublists are lists rather than tuples:
combos = [list(combo) for combo in combos]
print combos
# [[0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [0, 1, 2, 3], [0, 1, 2, 4], [0, 1, 3, 4], [0, 2, 3, 4], [0, 1, 2, 3, 4]]
First compute the power set of the all but the first item. Searching on "python power set" you'll get several hits, including this one. You didn't specifically mention it, but you probably want the results in lexographical order, and the implementation I selected gets you most of the way there.
That will give you all the combinations you need, .e.g [[], 1, ..., [1,2,3,4,5]] (note this includes the empty set and the whole set itself). Now just prepend 0 to each of these gives [[0],[0,1],...[0,1,2,3,4,5]].
This problem is related to powersets
>>> L = [0, 1, 2, 3, 4, 5]
>>> [[L[0]] + [k for j,k in enumerate(L[1:-1]) if i>>j&1] for i in range(1<<(len(L)-2))]
[[0], [0, 1], [0, 2], [0, 1, 2], [0, 3], [0, 1, 3], [0, 2, 3], [0, 1, 2, 3], [0, 4], [0, 1, 4], [0, 2, 4], [0, 1, 2, 4], [0, 3, 4], [0, 1, 3, 4], [0, 2, 3, 4], [0, 1, 2, 3, 4]]
If you want them sorted shortest to longest:
>>> M = [[L[0]] + [k for j,k in enumerate(L[1:-1]) if i>>j&1] for i in range(1<<(len(L)-2))
>>> sorted(M, key=len)
[[0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 1, 2], [0, 1, 3], [0, 2, 3], [0, 1, 4], [0, 2, 4], [0, 3, 4], [0, 1, 2, 3], [0, 1, 2, 4], [0, 1, 3, 4], [0, 2, 3, 4], [0, 1, 2, 3, 4]]
Its a combination of the first element plus the powerset of all the elements minus the first and last elements:
from itertools import chain, combinations
test = [0, 1, 2, 3, 4, 5]
def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
print [test[:1] + list(c) for c in powerset(test[1:-1])]
# [[0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [0, 1, 2, 3], [0, 1, 2, 4], [0, 1, 3, 4], [0, 2, 3, 4], [0, 1, 2, 3, 4]]
Here is a generator.
from itertools import combinations
def custom_combination_gen(l):
start = [l[0]]
L = l[1:-1]
yield start
for y in range(1, len(L)+1):
for x in combinations(L, y):
yield start + list(x)
Running the code:
print list(custom_combination_gen([0,1,2,3,4,5]))
[[0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [0, 1, 2, 3], [0, 1, 2, 4], [0, 1, 3, 4], [0, 2, 3, 4], [0, 1, 2, 3, 4]]
import itertools
a = [0, 1, 2, 3, 4, 5]
myList = []
myFinalList = []
for i in xrange(0,len(a)-2): myList += list(itertools.combinations(a[1:-1],i))
for item in myList: myFinalList.append(list(item)+[a[0]])
print myFinalList