slice

Fetch last five values from NSMutableArray

房东的猫 提交于 2019-12-02 13:21:13
I have a NSMutableArray whose count is 10 and I want to extract the last 5 values and store them in another array. How can I do this? I think that - (NSArray *) subarrayWithRange:(NSRange)range ( doc ) might help you. NSRange theRange; theRange.location = [wholeArray count] - 5; theRange.length = 5; NSArray *result = [wholeArray subarrayWithRange:theRange]; EDIT : Be careful to check that your array has at least five elements, else, subarrayWithRange will throw an exception. Use following way : NSMutableArray *first = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@

How to get the count of Custom tuples against two lists

荒凉一梦 提交于 2019-12-02 13:04:36
Please help me to get the counter for the list SS2 in list SS1 in PYTHON using from collections import Counter or any other fastest way SS1 = [(1, 2, 3, 4, 5), (1, 2, 3, 4, 6), (1, 2, 3, 5, 6), (1, 2, 4, 5, 6), (1, 3, 4, 5, 6), (2, 3, 4, 5, 6)] SS2=[(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 3, 4), (1, 3, 5), (1, 3, 6), (1, 4, 5), (1, 4, 6), (1, 5, 6), (2, 3, 4), (2, 3, 5), (2, 3, 6), (2, 4, 5), (2, 4, 6), (2, 5, 6), (3, 4, 5), (3, 4, 6), (3, 5, 6), (4, 5, 6)] Here is what i have tried and i don't know how to get the count for (1,2,4) th elements of the each tuple SS1=[(1, 2, 3, 4, 5), (1

Slice an array into 4 other arrays

南笙酒味 提交于 2019-12-02 10:04:30
问题 I have an array which I want to slice in 4 other arrays because I want to display the content of the first array on four columns. I have tried the code above, but what I get is N columns with 4 items. $groups = array(); for ($i = 0; $i < count($menu); $i += 4) $groups[] = array_slice($menu, $i, 4); Can this be modified in order to get exactly 4 columns and distribute the values so they fit? 回答1: Like Michael Berkowski suggested: $groups = array_chunk($menu,4); Should give you what you need.

Remove an element of a slice in a struct [duplicate]

北城余情 提交于 2019-12-02 10:03:58
This question already has an answer here: How to change struct variable content? 1 answer Why can't I append to a slice that's the property of a struct in golang? 1 answer I have a struct "Guest" which contains metadata of a party guest (a unique ID, name, surname and a list of the unique IDs of the guests who are friends of this guest. type Guest struct { id int name string surname string friends []int } I have the following code to remove an ID from the list of friends: func (self Guest) removeFriend(id int) { for i, other := range self.friends { if other == id { self.friends = append(self

Writting in sub-ndarray of a ndarray in the most pythonian way. Python 2

坚强是说给别人听的谎言 提交于 2019-12-02 09:01:44
问题 I have a ndarray like this one: number_of_rows = 3 number_of_columns = 3 a = np.arange(number_of_rows*number_of_columns).reshape(number_of_rows,number_of_columns) a array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) But I want something like this: array([[0, 100, 101], [3, 102, 103], [6, 7, 8]]) To do that I want to avoid to do it one by one, I rather prefer to do it in arrays or matrices, because later I want to extend the code. Nothe I have change a submatrix of the initial matrix (in mathematical

JS语言-Leetcode1051(slice、sort)

拈花ヽ惹草 提交于 2019-12-02 08:31:44
题目地址: https://leetcode-cn.com/problems/height-checker/ 思路:将乱序数组排序后,遍历对比两数组有多少处不同 代码: /** * @param {number[]} heights * @return {number} */ var heightChecker = function(heights) { let sortArr = heights.slice(0,heights.length); let number = 0; sortArr.sort(function(a, b) {return a - b}); for(let i = 0; i < heights.length; i++){ if(heights[i] !== sortArr[i] ){ number++; } } return number; }; 收获: arrayObject.slice(start,end):start:必需。    规定从何处开始选取。如果是负数,那么它规定从数组尾部开始算起的位置。也就是说,-1 指最后一个元素,-2 指倒数第二个元素,以此类推。end:可选。    规定从何处结束选取。该参数是数组片断结束处的数组下标。如果没有指定该参数,那么切分的数组包含从 start 到数组结束的所有元素。    如果这个参数是负数

Combining slicing and broadcasted indexing for multi-dimensional numpy arrays

左心房为你撑大大i 提交于 2019-12-02 08:20:35
问题 I have a ND numpy array (let say for instance 3x3x3) from wich I'd like to extract a sub-array, combining slices and index arrays. For instance: import numpy as np A = np.arange(3*3*3).reshape((3,3,3)) i0, i1, i2 = ([0,1], [0,1,2], [0,2]) ind1 = j0, j1, j2 = np.ix_(i0, i1, i2) ind2 = (j0, slice(None), j2) B1 = A[ind1] B2 = A[ind2] I would expect that B1 == B2, but actually, the shapes are different >>> B1.shape (2, 3, 2) >>> B2.shape (2, 1, 2, 3) >>> B1 array([[[ 0, 2], [ 3, 5], [ 6, 8]], [[

Store multidimensional numpy array slice with newaxis to object

亡梦爱人 提交于 2019-12-02 07:46:32
I have some code where I repeatedly need to repeatedly broadcast arrays in complex ways, for example: a = b[np.newaxis, ..., :, np.newaxis] * c[..., np.newaxis, np.newaxis, :] Is there an object to which I can store these slicing specifications? i.e. (but obviously this doesn't work): s1 = magic([np.newaxis, ..., :, np.newaxis]) s2 = magic([..., np.newaxis, np.newaxis, :]) Edit: perhaps this could be done with numpy.broadcast_to , but it's unclear how exactly while making sure that the correct axes are broadcast over... You can construct the index tuple manually, but NumPy includes a helper

Using assert_eq or printing large fixed sized arrays doesn't work

[亡魂溺海] 提交于 2019-12-02 07:20:58
I have written some tests where I need to assert that two arrays are equal. Some arrays are [u8; 48] while others are [u8; 188] : #[test] fn mul() { let mut t1: [u8; 48] = [0; 48]; let t2: [u8; 48] = [0; 48]; // some computation goes here. assert_eq!(t1, t2, "\nExpected\n{:?}\nfound\n{:?}", t2, t1); } I get multiple errors here: error[E0369]: binary operation `==` cannot be applied to type `[u8; 48]` --> src/main.rs:8:5 | 8 | assert_eq!(t1, t2, "\nExpected\n{:?}\nfound\n{:?}", t2, t1); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: an implementation of `std::cmp:

ArraySlice index out of range in Swift

不想你离开。 提交于 2019-12-02 07:15:52
问题 I have data in the following format ["DATA1-1","DATA1-2","DATA1-3","DATA1-4","","DATA2-1","DATA2-2","DATA2-3","DATA2-4","","DATA3-1","DATA3-2","DATA3-3","DATA3-4",""]. I need to perform specific operations within each component of the array. The components are separated by "". In other words, I first need to perform an operation on ["DATA1-1","DATA1-2","DATA1-3","DATA1-4"], then the same operation on ["DATA2-1","DATA2-2","DATA2-3","DATA2-4"], etc. First, I slice the array into separate