'Selecting all rows with the first 5 different values of a list
I have a predefined list with 100 elements, whose values represent cluster labels. First, I would like to find the first 5 different cluster labels from the list. Then, I want to select all rows that have one of the five values as entry, and finally write their index and label into a new array.
How do I have to adjust my code to achive that? I think I have to use a loop, but since I am new to python I dont know how to set it correctly.
list = np.array(list)
new_array = []
for x in list:
new_array.append(list[index, value])
print(new_array)
Solution 1:[1]
You can use np.unique to get the unique values of a list:
first_vals = np.unique(list)[:5]
new_array = []
for index, value in enumerate(list):
if value in first_vals:
new_array.append([index, value])
print(new_array)
Output:
[[0, 0], [1, 0], [2, 1], [3, 0], [4, 2], [5, 1], [6, 3], [10, 4], [11, 4], [12, 1], [14, 3], [15, 1], [16, 3], [17, 2], [18, 3]]
(Note: it's bad practice use names of Python builtins as variable names, e.g. list
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | richardec |
