This question already has an answer here:
How to return a subset of a list that matches a condition [duplicate]
(1 answer)
Closed 2 years ago.
I have a 2d List in python like below:
[[0,2,3,4],[0,1,3,4],[1,2,3,4]]
And I want to get only items that the first item is zero. In this Example the first and second item.
How could I do this?
In [46]: a = [[0,2,3,4],[0,1,3,4],[1,2,3,4]]
In [47]: [i for i in a if i[0] == 0]
Out[47]: [[0, 2, 3, 4], [0, 1, 3, 4]]
OR
In [49]: list(filter(lambda x: x[0] == 0, a))
Out[49]: [[0, 2, 3, 4], [0, 1, 3, 4]]
Related
This question already has answers here:
Get unique values in List of Lists
(6 answers)
Find unique rows in numpy.array
(20 answers)
Count Distinct Values in a List of Lists
(5 answers)
Closed 6 months ago.
I have a list A containing an array with indices. I want to print all the unique index numbers by scanning each [i,j]. In [0,3], i=0,j=3. I present the expected output.
import numpy as np
A=[np.array([[[0, 1],
[0, 3],
[1, 3],
[3, 4],
[3, 6],
[4, 5],
[4, 7],
[5, 7],
[6, 4]]])]
The expected output is
A=[0,1,3,4,5,6,7]
numpy has this very cool function np.unique.
Simply to get the output A=[0,1,3,4,5,6,7]
B = np.unique(A[0])
Output :
[0 1 3 4 5 6 7]
A=[[0, 1],[0, 3],[1, 3],[3, 4],[3, 6],[4, 5],[4, 7],[5, 7],[6, 4]]
K = []
for _ in range(len(A)):
K.extend(A[_])
print(set(K))
OUTPUT:
{0, 1, 3, 4, 5, 6, 7}
A is basically a list and within list you make an array.
Do this:
def unique(A):
x = np.array(A)
print(np.unique(x))
unique(A)
if A is an array we can simply do this:
np.unique(A)
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 7 months ago.
I have a matrix like:
matrix = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
]
and I have to assign a value to every element based on the formula (2 ∗ i + 3 ∗ j) mod 6, where i and j are the indexes.
I'm using the following code to iterate and assign value:
for i in range(len(matrix)):
for j in range(len(matrix[i])):
matrix[i][j] = (((2 * i) + (3 * j)) % 6)
but I have this output:
matrix = [
[4, 1, 4],
[4, 1, 4],
[4, 1, 4]
]
instead of the expected:
matrix = [
[0, 3, 0],
[2, 5, 2],
[4, 1, 4]
]
how can I solve this issue? Also, I can't use NumPy to solve this problem.
That is NOT how you created your array! If you did, it would work. What you did instead is
matrix = [[0]*3]*3
That gives you three references to a SINGLE list object, not three separate list objects. Initialize it as
matrix = [[0]*3 for _ in range(3)]
or better yet, use numpy.
This question already has answers here:
How to zip two 1d numpy array to 2d numpy array [duplicate]
(3 answers)
Closed 2 years ago.
I need to put pair of points into an array
for i in range(len(lon)):
for j in range(len(lat)):
tab = np.array([lon[i],lat[j]])
output:
array([1, 2])
But what I expect to have this: array([1, 2],[1, 2],[1, 3],[1,2])
The original values are:
lon = [1, 1, 1, 1]
lat = [2, 2, 3, 2]
Thanks a lot
This is because the tab variable is being overwritten everytime in the loop. You can do it also simpler with a list comprehension:
lon = [1,1,1,1]
lat = [2,2,3,2]
output = [[lon[i],lat[i]] for i in range(len(lon))]
print(output)
This shows:
[[1, 2], [1, 2], [1, 3], [1, 2]]
This question already has answers here:
Index all *except* one item in python
(11 answers)
Closed 5 years ago.
If I have the list [0, 1, 2, 3, 4, 5] I can return the last four items using list[1:].
Is there a similar way of doing this that would return the list without the second item?
I.e. list[??] == [0, 2, 3, 4 ,5]
(If there are different methods for Python 2.x and Python 3.x please detail both)
You can add two slices of the list
newLs = ls[:1] + ls[2:]
[0, 2, 3, 4, 5]
You can also delete the element
del ls[1]
you can try this:
n = 1
l = [0, 1, 2, 3, 4, 5]
new_l = [a for i, a in enumerate(l) if i != n]
print(new_l)
Output:
[0, 2, 3, 4, 5]
This question already has answers here:
How do I check if there are duplicates in a flat list?
(15 answers)
Closed 6 years ago.
So I have a list called puzzle which contains the follow lists:
puzzle = [[1, 3, 5, 5, 4],
[3, 5, 1, 3, 4],
[2, 3, 4, 5, 1],
[1, 5, 3, 2, 2],
[5, 4, 1, 3, 2]]
I would like to check each list inside puzzle and test if there are any duplicate numbers that are not zero, in which case the code would return false. How can I do this?
Almost the same approach -- except that you'd run it on a sub-list without zeros in it.
def has_dup(lst):
no_zeros = [x for x in lst if x != 0]
return len(set(no_zeros)) != len(no_zeros)
is_valid = any(has_dup(x) for x in puzzle)