Related
i have defined a matrix m , i wish to return TRUE if there is any column which has all its elements as 1, for example :
m = [[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0]]
i have tried various approaches but they all seem to return True in all cases(since there are multiple columns with all elements as 0)
I agree with ansev's response. You gave us a list of lists. I prefer numpy for these kind of exercises.
import numpy as np
m = [[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0]]
np.array(m).all(axis=0).any()
Output
True
This question already has answers here:
How do I compute all possibilities for an array of numbers/bits (in python, or any language for that matter)
(5 answers)
Closed 2 years ago.
I am trying to achieve the following. I have a 2D array, which is of a 4x4 dimension. I want to get all possibilities, where I can insert a single 1 instead of a zero, and return an array, which contains all of these possibilities
So if we take:
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
would result in:
[[1, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 1], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
...
There would be a total of 14 entries in the resulting array, since there were 14 zeroes in the input array.
The problem is, that the code I have currently should work, as far as I understand, but I can't seem to get where it goes wrong.
def getPossibilities(arr):
p = []
for i in range(4):
for j in range(4):
if arr[i][j] == 0:
p.append(arr)
p[-1][i][j]=1
return p
for i in getPossibilities([[0,1,0,0],[0,0,1,0],[0,0,0,0],[0,0,0,0]]):
print(i)
This results in 14 arrays of solid ones.
I included the way I check the results, in case there is an error there. I also tried with first copying the arr array into a temporary one, then make the changes, but to no avail.
What goes wrong here? I cannot seem to find an answer. Also, is there a more elegant and faster way of doing this? It would be really beneficial for my usecase.
Thank you very much in advance!
This is somewhat tricky but since you have a list of lists, the copy won't work and you will be changing the array every time, what you need is deepcopy:
import copy
def getPossibilities(arr):
p = []
for i in range(4):
for j in range(4):
if arr[i][j] == 0:
tmp = copy.deepcopy(arr)
tmp[i][j]=1
p.append(tmp)
return p
for i in getPossibilities([[0,1,0,0],[0,0,1,0],[0,0,0,0],[0,0,0,0]]):
print(i)
[[1, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 1], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 0], [1, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [1, 0, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 1, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 1, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 1]]
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 3 years ago.
Here's a 3*4 matrix represented by embedded list, when I try to assign the dp[0][0][0] = 1, it changes all first value of each list element.
I'm using python 3.7, don't know where's the problem.
I want to change the first value of first [0,0,0,0] to 4
dp = [[[0,0,0,0]] * 3] *4
dp[0][0][0] =4
print(dp)
output like this
[[[4, 0, 0, 0], [4, 0, 0, 0], [4, 0, 0, 0]], [[4, 0, 0, 0], [4, 0, 0, 0], [4, 0, 0, 0]], [[4, 0, 0, 0], [4, 0, 0, 0], [4, 0, 0, 0]], [[4, 0, 0, 0], [4, 0, 0, 0], [4, 0, 0, 0]]]
You do it like this :
dp = [[[0,0,0,0] for y in range(3)] for i in range(4)]
dp[0][0][0] =4
print(dp)
Output:
[[[4, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]
Have a good day !
#DeckOfCards
deck = []
filler= [0, 0, 0, 0]
def deck_generator():
counter = 0
for i in range (52):
counter += 1
deck.append(filler)
return deck
def deck_values(i):
k = 4
temp = (i + 1) % k
return temp
deck = deck_generator()
for i in range(52):
deck[i][0] = deck_values(i)
The goal with this code is to assign the values 0-3 inclusive to the first index of the inner list to all values in the outer list.
[[0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0], [0, 0, 0, 0]] and so on. For some reason the assignment just does not work. Thanks in advance.
Append a copy of the list instead of the list itself.
deck.append(filler[:])
Try this
deck = []
for i in range(52):
deck.append([i % 4, 0, 0, 0])
print (deck)
Running this code prints (edited for ease of viewing):
[[0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0],
[0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0],
[0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0],
[0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0],
[0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0],
[0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0],
[0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0],
[0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0],
[0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0],
[0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0],
[0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0],
[0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0],
[0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0]]
are you sure you want to get [[0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0], [0, 0, 0, 0]] ?
first of all, you should use the copy of filter and then you can get a list like:
[[1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0], [0, 0, 0, 0],...]
but if you want to get the result [[0, 0, 0, 0], [1, 0, 0, 0], [2, 0, 0, 0], [3, 0, 0, 0],...]
your codes should be like this:
deck = []
filler= [0, 0, 0, 0]
def deck_generator():
counter = 0
for i in range (52):
counter += 1
deck.append(filler[:])
return deck
def deck_values(i):
k = 4
temp = i % k #not temp = (i+1) % k
return temp
deck = deck_generator()
for i in range(52):
deck[i][0] = deck_values(i)
print(deck)
I think the issue with it is temp=(i+1)%k as we do not need to add 1 to 1. It should start from 0. In addition to this, you need to append properly so it works. The code would look like this:
#DeckOfCards
deck = []
filler= [0, 0, 0, 0]
def deck_generator():
counter = 0
for i in range (52):
counter += 1
deck.append(filler[:])
return deck
def deck_values(i):
k = 4
temp = (i) % k
return temp
deck = deck_generator()
for i in range(52):
deck[i][0] = deck_values(i)
I have a list of lists such as
[[0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 2, 0, 0, 0, 0, 0], [0, 0, 3, 0, 0, 0, 0], [0, 0, 0, 2, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 3, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 2, 0], [0, 0, 0, 0, 0, 0, 3]]
I would like to combine it into one integer list as
[0,1,0,2,3,2,0,3,0,2,3]
I couldn't find a exact way how to achieve it.
The pattern is that if there is any number other than 0 in the list add it as actual number found or else enter it as 0.
3>> [functools.reduce(operator.or_, x) for x in L]
[0, 1, 0, 2, 3, 2, 0, 3, 0, 2, 3]
Is your goal to flatten the 2d list? If so, b in the following snippet is what you want:
a = [[1,2,3],[4,5,6],[7,8,9]]
b = [val for sublist in a for val in sublist]
Just iterate like so:
new = [0 for i in range(len(old[0]))]
for a in old:
for b, c in enumerate(a):
new[b] += c
presuming you only get either all zeros or a single non zero digit you can call next on filter after filtering all the 0's with 0 as the default value to next.
l = [[0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 2, 0, 0, 0, 0, 0], [0, 0, 3, 0, 0, 0, 0], [0, 0, 0, 2, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 3, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 2, 0], [0, 0, 0, 0, 0, 0, 3]]
print([next(filter(None,sub),0) for sub in l] )
[0, 1, 0, 2, 3, 2, 0, 3, 0, 2, 3]