Related
This is what i have-
def merge_list(mylist)
list1 = []
one_len = len(mylist)
two_len = len(mylist[0][0])
for index in range(two_len):
combine_list = []
for index2 in range(one_len):
combine_list.extend([a[index] for a in mylist[
index2]])
list1.append(combine_list)
return list1
But i have a problem with the output-
for example:
input-
mylist=[[[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]], [[2, 2, 2], [2, 2, 2], [2, 2, 2], [2, 2, 2]], [[3, 3, 3], [3, 3, 3], [3, 3, 3], [3, 3, 3]]]
in short-
[[[1]*3]*4, [[2]*3]*4, [[3]*3]*4]
the output is -
[[[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3], [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3], [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3]]]
and not -
[[[1, 2, 3], [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3], [1, 2, 3]]]
in short-
[[[1, 2, 3]]*3]*4
I would be happy to solve the problem and advise how to shorten the code.
In order to fix your code, you just need to update the return statement like this:
return [list1]
Though there is still a hidden problem when your inner lists lengths are longer than one element. Check your outputs with sample inputs like [[[2,1], [3,1]]].
One further - more compact - solution may be obtained by concatenating the inner lists within a cycle, then recreate the number of dimensions you need, moving the complexity from O(n^2) to O(n).
mylist = [[[2,1]],[[3,1]]]
def merge_list(mylist):
l_out = []
for l in mylist:
l_out += l[0]
return [[l_out]]
EDIT: In case of more complex inputs, you can extract first all elements and eventually fix the dimensions. This will still bring O(n^2) complexity though:
def merge_list(lst):
elements = [[] for _ in range(len(mylist[0]))]
for l1 in mylist:
for idx, l2 in enumerate(l1):
elements[idx] += l2
return [elements]
merge_list(mylist)
Input:
[[[1], [1]], [[2], [2]], [[3], [3]]]
Output:
[[[1, 2, 3], [1, 2, 3]]]
Im trying to do permutation problem in leetcode with backtracking Algorithm, While Printing I got all the Possiblities but when i Trying to store those value in global variable I'm not allow to that
Ex:
AnswerIwant:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
when i print those value while recursion it is print
Example:
[1, 2, 3]
[1, 3, 2]
[2, 3, 1]
[2, 1, 3]
[3, 1, 2]
[3, 2, 1]
When I store The Value In Global variable for outPut I got like This
[1, 2, 3]
[[1, 2, 3]]
[[1, 2]]
[1, 3, 2]
[[1, 3, 2], [1, 3, 2]]
[[1, 3], [1, 3]]
[[1], [1]]
[2, 3, 1]
[[2, 3, 1], [2, 3, 1], [2, 3, 1]]
[[2, 3], [2, 3], [2, 3]]
[2, 1, 3]
[[2, 1, 3], [2, 1, 3], [2, 1, 3], [2, 1, 3]]
[[2, 1], [2, 1], [2, 1], [2, 1]]
[[2], [2], [2], [2]]
[3, 1, 2]
[[3, 1, 2], [3, 1, 2], [3, 1, 2], [3, 1, 2], [3, 1, 2]]
[[3, 1], [3, 1], [3, 1], [3, 1], [3, 1]]
[3, 2, 1]
[[3, 2, 1], [3, 2, 1], [3, 2, 1], [3, 2, 1], [3, 2, 1], [3, 2, 1]]
[[3, 2], [3, 2], [3, 2], [3, 2], [3, 2], [3, 2]]
[[3], [3], [3], [3], [3], [3]]
[[], [], [], [], [], []]
Here Is my code For the Above OutPut
nums = [1,2,3]
val=nums
answerIwant = []
numberOfValues=len(val)-1
answer=[]
def permutation(nums, bucket, index,sub):
global answerIwant,numberOfValues
for i in range(len(nums)):
val=nums.pop(0)
bucket.append(val)
if len(bucket)-1==numberOfValues:
print(bucket)
answerIwant.append(bucket)
permutation(nums, bucket, index,sub)
nev=bucket.pop()
nums.append(nev)
print(answerIwant)
#[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
sub=[]
bucket = []
val=(permutation(nums, bucket, index,sub))
print(val)
Explain that problem i'm facing with symbol word
Thank You
You have to append the copy of the list during appening in the answerIwant list. So you should replace answerIwant.append(bucket) to answerIwant.append(bucket.copy()).
Refer this for more info. So your code could be this,
nums = [1,2,3]
val=nums
answerIwant = []
numberOfValues=len(val)-1
answer=[]
def permutation(nums, bucket, index,sub):
global numberOfValues
for i in range(len(nums)):
val=nums.pop(0)
bucket.append(val)
if len(bucket)-1==numberOfValues:
# print(bucket)
answerIwant.append(bucket.copy())
permutation(nums, bucket, index,sub)
nev = bucket
nev=bucket.pop()
nums.append(nev)
#[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
sub=[]
bucket = []
permutation(nums, bucket, 0,sub)
print(answerIwant)
Hi I'm trying to make a list of all possible cohesive combinations of another list, so from [0, 1, 2, 3] I'd like to get [[0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]. So far I've got this:
def expandArray(arr):
result = []
for x in range(0, len(arr)):
subArray = [arr[x]]
result.append(subArray)
for y in range(x + 1, len(arr)):
subArray.append(arr[y])
result.append(subArray)
return(result)
But this returns: [[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [2, 3], [2, 3], [3]].
What am I doing wrong ?
subArray is a list that you modify in your for loop. When you append to it, you do not create a new list, but you modify it, and then put it in the list again, so will in the end get a result with several copies of the same list. Compare this code:
a = []
b = [5]
a.append(b)
b.append(1)
a.append(b)
print(a)
would output:
[[5, 1], [5, 1]]
Here is a way to have your desired output using list slicing:
def get_combs(iterable):
for k, _ in enumerate(iterable):
elm = k
while elm <= len(iterable):
data = iterable[k:elm]
elm += 1
if data:
yield data
combs = list(get_combs([0, 1, 2, 3]))
print(combs)
Output:
[[0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
I have two lists of lists of the same length in Python 3 as follows:
A = [[0], [0, 1], [0, 1, 2], [0, 1], [0, 1, 2, 3]]
W = [[2, 2], [1, 2, 3], [2, 2, 2, 3], [1, 3, 4, 4], [1, 1, 3, 4]]
Elements of A are indices of elements of W. I would like to remove the elements of W given A. So, in the example, I would like to remove W[0][0], W[1][0], W[1][1], W[2][0], W[2][1], W[2][2], etc.
What I did is this:
for t in range(len(A)):
del W[t][A[t]]
But this gives the following error: TypeError: list indices must be integers or slices, not list
Unlike numpy arrays, you cannot index a list with a list. But you can use a list comprehension for this task:
A = [[0], [0, 1], [0, 1, 2], [0, 1], [0, 1, 2, 3]]
W = [[2, 2], [1, 2, 3], [2, 2, 2, 3], [1, 3, 4, 4], [1, 1, 3, 4]]
res = [[j for i, j in enumerate(w) if i not in a] for a, w in zip(A, W)]
print(res)
[[2], [3], [3], [4, 4], []]
Or, if you are happy using a 3rd party library, numpy syntax is simpler:
import numpy as np
res = [np.delete(i, j).tolist() for i, j in zip(W, A)]
One easy way is to use two nested loops. As you have probably noticed by now, you need two index numbers - one for the list in A and another for the element number of this list. Here is one way to tackle the problem:
A = [[0], [0, 1], [0, 1, 2], [0, 1], [0, 1, 2, 3]]
W = [[2, 2], [1, 2, 3], [2, 2, 2, 3], [1, 3, 4, 4], [1, 1, 3, 4]]
#cycle through list A and keep track of the list number i
for i, a_list in enumerate(A):
#retrieve index from each list in A, start with the highest index to avoid index problems
for j in sorted(a_list, reverse = True):
#delete the element j in list i of W
del W[i][j]
print(W)
#output
#[[2], [3], [3], [4, 4], []]
Lately, I've had some problems with Python's random.choice function.
I would expect the example script I've added to print out a list that consists of 3 items. Each of those items should be a list that contains two integers and one string.
For example: [[1, 4, 'a'], [2, 1, 'b'], [3, 4, 'c']]
from random import choice
pair = [
[1, 2], [1, 3], [1, 4],
[2, 1], [2, 3], [2, 4],
[3, 1], [3, 2], [3, 4],
[4, 1], [4, 2], [4, 3]
]
list = [0, 0, 0]
list[0] = choice(pair)
list[0].append('a')
list[1] = choice(pair)
list[1].append('b')
list[2] = choice(pair)
list[2].append('c')
print(list)
Most of the times the script works as expected. Sometimes however, it prints out something like this:
[[3, 2, 'a', 'c'], [4, 3, 'b'], [3, 2, 'a', 'c']]
Two of the items in the list not only consist of one string too many, but they are identical for some reason.
First I believed this had something to do with the append function.
However when I removed the random.choice component like this:
list = [0, 0, 0]
list[0] = [1, 2]
list[0].append('a')
list[1] = [2, 3]
list[1].append('b')
list[2] = [3, 4]
list[2].append('c')
print(list)
It still worked without flaws.
Same for the random.choice function, which didn't cause any problems by itself:
from random import choice
pair = [
[1, 2], [1, 3], [1, 4],
[2, 1], [2, 3], [2, 4],
[3, 1], [3, 2], [3, 4],
[3, 1], [3, 2], [4, 3],
list = [0, 0, 0]
list[0] = choice(pair)
list[1] = choice(pair)
list[3] = choice(pair)
print(list)
Next I tested what would happen when the list contained only one item and list.append and random.choice were only executed once
from random import choice
pair = [
[1, 2], [1, 3], [1, 4],
[2, 1], [2, 3], [2, 4],
[3, 1], [3, 2], [3, 4],
[4, 1], [4, 2], [4, 3]
]
list = [0]
list[0] = choice(pair)
list.append('a')
print(list)
This script behaved like I'd expect as well. Apparently my problem only occurs when random.choice and list.append are used executed several times on different items in a list, however I don't know how to explain it. Could someone explain what's happening and how to solve it?
First I open the REPL prompt and run your first block of code
>>> from random import choice
>>>
>>> pair = [
... [1, 2], [1, 3], [1, 4],
... [2, 1], [2, 3], [2, 4],
... [3, 1], [3, 2], [3, 4],
... [4, 1], [4, 2], [4, 3]
... ]
>>>
>>> list = [0, 0, 0]
>>>
>>> list[0] = choice(pair)
>>> list[0].append('a')
>>> list[1] = choice(pair)
>>> list[1].append('b')
>>> list[2] = choice(pair)
>>> list[2].append('c')
>>>
>>> print(list)
[[4, 1, 'a'], [3, 4, 'b'], [1, 2, 'c']]
Looks ok, but what happens if you inspect pair
>>> pair
[[1, 2, 'c'], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4, 'b'], [4, 1, 'a'], [4, 2], [4, 3]]
So by appending to the lists that choice(pair) returns, you have inadvertently modified pair.
This is because choice(pair) doesn't copy the sublist.
If you mean to make a copy of the sublist, use
list[0] = choice(pair)[:]
or
list[0] = choice(pair).copy()