slice

Slice numpy array wth list of wanted rows

◇◆丶佛笑我妖孽 提交于 2020-12-08 06:48:05
问题 I have a numpy 2d array A , and a list of row numbers row_set . How can I get new array B such as if row_set = [0, 2, 5] , then B = [A_row[0], A_row[2], A_row[5]] ? I thought of something like this: def slice_matrix(A, row_set): slice = array([row for row in A if row_num in row_set]) but I don't have any idea, how can I get a row_num. 回答1: Use take() : In [87]: m = np.random.random((6, 2)) In [88]: m Out[88]: array([[ 0.6641412 , 0.31556053], [ 0.11480163, 0.00143887], [ 0.4677745 , 0

Is it legal to cast a struct to an array?

给你一囗甜甜゛ 提交于 2020-12-06 04:15:32
问题 Consider the following: // Just a sequence of adjacent fields of same the type #[repr(C)] #[derive(Debug)] struct S<T> { a : T, b : T, c : T, d : T, } impl<T : Sized> S<T> { fn new(a : T, b : T, c : T, d : T) -> Self { Self { a, b, c, d, } } // reinterpret it as an array fn as_slice(&self) -> &[T] { unsafe { std::slice::from_raw_parts(self as *const Self as *const T, 4) } } } fn main() { let s = S::new(1, 2, 3, 4); let a = s.as_slice(); println!("s :: {:?}\n\ a :: {:?}", s, a); } Is this code

Is it legal to cast a struct to an array?

喜欢而已 提交于 2020-12-06 04:14:20
问题 Consider the following: // Just a sequence of adjacent fields of same the type #[repr(C)] #[derive(Debug)] struct S<T> { a : T, b : T, c : T, d : T, } impl<T : Sized> S<T> { fn new(a : T, b : T, c : T, d : T) -> Self { Self { a, b, c, d, } } // reinterpret it as an array fn as_slice(&self) -> &[T] { unsafe { std::slice::from_raw_parts(self as *const Self as *const T, 4) } } } fn main() { let s = S::new(1, 2, 3, 4); let a = s.as_slice(); println!("s :: {:?}\n\ a :: {:?}", s, a); } Is this code

Slicing out a specific from a list

↘锁芯ラ 提交于 2020-11-29 19:27:46
问题 I have one list and I want to print out all elements of it but skip one specific. a = [1,2,3,4,5,6,7,8,9] I want to print out: 1,3,4,5,6,7,8,9 (in a column like a regular for-loop) Is this possible? 回答1: If you specify the element to be skipped by its position (index): for position, element in enumerate(a): if position != specified_position: print(element) If you specify the element to be skipped by its value: for element in a: if element != specified_value: print(element) 回答2: Just slice on

Slicing out a specific from a list

删除回忆录丶 提交于 2020-11-29 19:25:33
问题 I have one list and I want to print out all elements of it but skip one specific. a = [1,2,3,4,5,6,7,8,9] I want to print out: 1,3,4,5,6,7,8,9 (in a column like a regular for-loop) Is this possible? 回答1: If you specify the element to be skipped by its position (index): for position, element in enumerate(a): if position != specified_position: print(element) If you specify the element to be skipped by its value: for element in a: if element != specified_value: print(element) 回答2: Just slice on

Is there a way to initialize an empty slice?

笑着哭i 提交于 2020-11-28 07:02:07
问题 Something like this? [String, 0] Vec::new() is not an option. 回答1: This creates an empty array: let thing: [String; 0] = []; You can also get a slice from the array: let thing: &[String] = &[]; You can also use as : some_function([] as [String; 0]); some_function(&[] as &[String]); 来源: https://stackoverflow.com/questions/45533699/is-there-a-way-to-initialize-an-empty-slice

Is there a way to initialize an empty slice?

时光总嘲笑我的痴心妄想 提交于 2020-11-28 07:01:08
问题 Something like this? [String, 0] Vec::new() is not an option. 回答1: This creates an empty array: let thing: [String; 0] = []; You can also get a slice from the array: let thing: &[String] = &[]; You can also use as : some_function([] as [String; 0]); some_function(&[] as &[String]); 来源: https://stackoverflow.com/questions/45533699/is-there-a-way-to-initialize-an-empty-slice