slice

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

為{幸葍}努か 提交于 2019-12-02 03:36:02
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 terms, in terms of this example ndarray). In the example the columns considered are [1,2] and the rows [0

Slice an array into 4 other arrays

有些话、适合烂在心里 提交于 2019-12-02 03:15:39
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? Like Michael Berkowski suggested: $groups = array_chunk($menu,4); Should give you what you need. If you're more into "manual labour" : $groups = array(); while($groups[] = array_splice($menu,0,4)) {//no

Why can't I change the values in a range of type structure?

主宰稳场 提交于 2019-12-02 03:09:40
This is my first post so please "Go" easy on me. :) ... I am quite familiar with many traditional programming languages but I am new to Go and having trouble understanding the use of slices and ranges. The program code and comments below illustrate my consternation. Thank you! package main import ( "fmt" "time" ) type myStruct struct { Name string Count int } Wrote my own Mod function because I could not find on in the Go libraries. func modMe(mod int, value int) int { var m int var ret int m = value / mod ret = value - m*mod return ret } func main() { mod := 4 cnt := 16 fmt.Printf("Record mod

Slice each string-valued element of an array in Javascript

大兔子大兔子 提交于 2019-12-02 02:52:11
I have the following array: var arr = ["Toyota", "Hyundai", "Honda", "Mazda"]; I want to slice each element backwards, like: var arr = ["Toyota", "Hyundai", "Honda", "Mazda"].slice(-2); so it will return: arr = ["Toyo", "Hyund", "Hon", "Maz"]; Is it possible? or is there anyway of doing this? You can't use slice directly, as it has a different meaning with an array and will return you a list of array elements. var arr = ["Toyota", "Hyundai", "Honda", "Mazda"] arr.slice(0, -2) // returns the elements ["Toyota", "Hyundai"] In order to do the slice on each element, you can use .map() (on IE9+):

Using : for multiple slicing in list or numpy array

余生颓废 提交于 2019-12-02 02:41:33
问题 I'm having some difficulty trying to figure out how to do extract multiple values in a list that are spaced some indices apart. For example, given a list l = [0,1,2,3,4,5,6,7,8,9,10] , I want to only extract the values [1,2,3] and [6,7,8,9] . I could do l[1:4]+l[6:-1] , but is there a way such to write l[1:4,6:-1] ? This is really a ghost problem to the actual problem I am having in a pandas dataframe. I have a dataframe, df , with columns ['A','B','C','I1','D','E','F','I2','I3'] , and I only

Is working past the end of a slice idiomatic?

≯℡__Kan透↙ 提交于 2019-12-02 00:27:27
I was reading through Go's compress/flate package, and I found this odd piece of code [1]: n := int32(len(list)) list = list[0 : n+1] list[n] = maxNode() In context, list is guaranteed to be pointing to an array with more data after. This is a private function, so it can't be misused outside the library. To me, this seems like a scary hack that should be a runtime exception. For example, the following D code generates a RangeError: auto x = [1, 2, 3]; auto y = x[0 .. 2]; y = y[0 .. 3]; Abusing slices could be done more simply (and also look more safe) with the following: x := []int{1, 2, 3} y

Extract subarrays of numpy array whose values are above a threshold

旧巷老猫 提交于 2019-12-01 22:46:58
I have a sound signal, imported as a numpy array and I want to cut it into chunks of numpy arrays. However, I want the chunks to contain only elements above a threshold. For example: threshold = 3 signal = [1,2,6,7,8,1,1,2,5,6,7] should output two arrays vec1 = [6,7,8] vec2 = [5,6,7] Ok, the above are lists, but you get my point. Here is what I tried so far, but this just kills my RAM def slice_raw_audio(audio_signal, threshold=5000): signal_slice, chunks = [], [] for idx in range(0, audio_signal.shape[0], 1000): while audio_signal[idx] > threshold: signal_slice.append(audio_signal[idx])

Show a string of certain length without truncating

五迷三道 提交于 2019-12-01 22:46:12
问题 I am displaying a string of certain length in ruby. only 80 characters of that string can be displayed in one line. for example if string length is 82 then it will be shown in 2 lines if length is 250 then string will be shown in 5 lines etc and i want to split on whitespace not the word. I am new in this so don't know how can i solve this. 回答1: def wrap(str, max_line_len) str.scan /(?<=\A| ).{1,#{max_line_len}}(?= |\z)/ end str = "Little Miss Muffet she sat on her tuffet, eating her curds

How to implement __delitem__ to handle all possible slice scenarios?

时光怂恿深爱的人放手 提交于 2019-12-01 21:34:49
问题 I work on a class with and embedded list. class a: def __init__(self, n): self.l = [1] * n def __getitem__(self, i): return self.l[i] def __delitem__(self, i): print type(i) print i I want to use the del operator with the full syntax of slices: p = a(10) del p[1:5:2] The __delitem__ receives a slice object if the parameter is not a single index. How can I use the slice object to iterate through the specified elements? 回答1: The indices method of the slice object will, given the length of the

Different behavior of variable and return value of function

和自甴很熟 提交于 2019-12-01 21:23:44
问题 I want to join two lines, but I get an error message. Original: hash := sha1.Sum([]byte(uf.Pwd)) u.Pwhash = hex.EncodeToString(hash[:]) Joint: u.Pwhash = hex.EncodeToString(sha1.Sum([]byte(uf.Pwd))[:]) The first one works fine, the second produces the error message: models/models.go:104: invalid operation sha1.Sum(([]byte)(uf.Pwd))[:] (slice of unaddressable value) Why is that? 回答1: You get an error message in the 2nd case because you try to slice the return value of a function call (that of