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)
Related
This question already has answers here:
Matrix Transpose in Python [duplicate]
(19 answers)
Closed 1 year ago.
new to python so i am writing this code that takes lists and returns a transpose version of it for example
mat = [[1,2],[3,4],[5,6]]
mat_T = mat_transpose(mat)
print(mat)
# [[1, 2], [3, 4], [5, 6]]
print(mat_T)
# [[1, 3, 5], [2, 4, 6]]
this is an example of a correct output
now how do i access the ints in a way that i can add them to a new list like the ints 1 3 5 are all in different lists but i need the in the same list and so are the ints 2 4 6 and so on if there were more ints in more lists
You don't need to access the individual values, use zip to iterate over the transposed values and map+list to convert each transposed subarray into list:
mat = [[1,2],[3,4],[5,6]]
mat_T = list(map(list, zip(*mat)))
mat_T
output:
>>> mat_T
[[1, 3, 5], [2, 4, 6]]
This question already has answers here:
Append value to each sublist in a list
(3 answers)
Closed 2 years ago.
I'm trying to figure out how to increase the size of the lists inside a list of lists. For example [[1,2,3,4], [1,2,3,4],[1,2,3,4]] and want to be able to add 1 more onto it so it is [[1,2,3,4,5], [1,2,3,4,5],[1,2,3,4,5]]. The numbers are just to show i want them to now have a new element in each.
You can use a for-loop and .append().
>>> L = [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
>>> for sublist in L:
... sublist.append(5)
...
>>> L
[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]
This question already has answers here:
How to sort a list of lists by a specific index of the inner list?
(12 answers)
Understanding slicing
(38 answers)
Closed 3 years ago.
Input : [[0, 2], [1, 4], [2, 6]]
Description : I need to print two lists with greater value by comparing the element in the 2nd place.
Expected Output: [[1, 4], [2, 6]]
You can use sorted and specify in the key argument that you want to sort each sublist by the second element using operator.itemgetter. Then slice the returned list to select the two last sublists:
l = [[0, 2], [1, 4], [2, 6]]
from operator import itemgetter
sorted(l, key=itemgetter(1))[-2:]
Output
[[1, 4], [2, 6]]
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)
This question already has answers here:
Subsetting a 2D numpy array
(5 answers)
Closed 7 years ago.
I wish to extract rows and columns from a matrix using a single "fancy" slice, is this possible?
m = matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
My target is
matrix([[1, 3],
[7, 9]])
Where I have a list of the items I want
d = [0,2]
I can achieve the functionality by
m[d][:,d]
But is there a simpler expression?
You can do this using numpy.ix_:
m = matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
d = [0,2]
print m[ix_(d,d)]
which will emit:
[[1 3]
[7 9]]