slice

The zero value of a slice is not nil

偶尔善良 提交于 2019-12-04 01:37:23
I was following the example https://tour.golang.org/moretypes/10 I modified the code expecting to get the same result. I did not. Is this a bug, or a documentation error? The tour states A nil slice has a length and capacity of 0. My y variable has a length and capacity of 0. package main import "fmt" func myPrint(z []int) { fmt.Println(z, len(z), cap(z)) if z == nil { fmt.Println("nil!") } } func main() { var z []int y := []int {} myPrint(z) myPrint(y) } Here is my output. [] 0 0 nil! [] 0 0 I was expecting a second "nil"~ Why didn't I get it? The doc you referenced states that a nil slice

Why can't I assign an arbitrary iterable to an extended slice whose step is -1?

ぐ巨炮叔叔 提交于 2019-12-04 01:24:46
问题 Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>> u = [4, 5, 6, 7, 8, 9] >>> u[1::1] = [3, 2, 1, 0] >>> u [4, 3, 2, 1, 0] >>> u[9:0:-1] = [8, 7, 6, 5] >>> u [4, 5, 6, 7, 8] >>> u[9:0:-1] = [16, 12, 8] Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: attempt to assign sequence of size 3 to extended slice of size 4 >>> u [4, 5, 6, 7, 8] >>> Expected

Tuple slicing not returning a new object as opposed to list slicing

心已入冬 提交于 2019-12-04 01:17:51
In Python (2 and 3). Whenever we use list slicing it returns a new object, e.g.: l1 = [1,2,3,4] print(id(l1)) l2 = l1[:] print(id(l2)) Output >>> 140344378384464 >>> 140344378387272 If the same thing is repeated with tuple, the same object is returned, e.g.: t1 = (1,2,3,4) t2 = t1[:] print(id(t1)) print(id(t2)) Output >>> 140344379214896 >>> 140344379214896 It would be great if someone can shed some light on why this is happening, throughout my Python experience I was under the impression empty slice returns a new object. My understanding is that it's returning the same object as tuples are

Generating a random, fixed-length byte array in Go

心已入冬 提交于 2019-12-04 00:26:05
I have a byte array, with a fixed length of 4. token := make([]byte, 4) I need to set each byte to a random byte. How can I do so, in the most efficient matter? The math/rand methods do not provide a Random Byte function, as far as I am concerned. Perhaps there is a built-in way, or should I go with generating a random string and converting it to a byte array? Package rand import "math/rand" func Read func Read(p []byte) (n int, err error) Read generates len(p) random bytes from the default Source and writes them into p. It always returns len(p) and a nil error. f unc (*Rand) Read func (r

Function apply with arguments using slice

孤者浪人 提交于 2019-12-04 00:03:41
Looking at this tutorial I saw the following code suggestion in one comment: init:function(callback){ var that =this ; return $http.jsonp(this.url).success( function(data){ that.availableGenres = that.getGenres(data); that.results = that.getResults(data); if(callback)callback.apply(null,[].slice.call(arguments)) } ) } But this line callback.apply(null,[].slice.call(arguments)) looks weird to me. Why not just: callback.apply(null, arguments) ? Because I don't like when I don't understand the point of something I played around with this Fiddle to understand the need of slice function in there.

Deselecting a column by name

心不动则不痛 提交于 2019-12-03 18:34:19
问题 Is there a way to select all columns of a data frame except a column that has a particular name. It would be the analog of df[, -1] , except using the column name instead of the index? 回答1: You can do this using vector subsetting. First, create a dummy data set: R> dd = data.frame(A = 1:3, B = 1:3, C=1:3, D=1:3) Then use the ! operator to reverse the selection: R> dd[ ,!(colnames(dd) == "A")] B C D 1 1 1 1 2 2 2 2 3 3 3 3 Alternatively, you could have: A slightly shorter version (courtesy of

Column with list of strings in python

情到浓时终转凉″ 提交于 2019-12-03 16:44:06
问题 I have a pandas dataframe like the following: categories review_count 0 [Burgers, Fast Food, Restaurants] 137 1 [Steakhouses, Restaurants] 176 2 [Food, Coffee & Tea, American (New), Restaurants] 390 ... .... ... ... .... ... ... .... ... From this dataFrame,I would like to extract only those rows wherein the list in the 'categories' column of that row contains the category 'Restaurants'. I have so far tried: df[[df.categories.isin('Restaurants'),review_count]] , as I also have other columns

How to calculate space between dicom slices for MPR?

主宰稳场 提交于 2019-12-03 16:02:58
Due to showing MPR view based on Dicoms. I've made a 3D array from series of dicom files. And I show it from Coronal and Sagittal sides. My 3D array includes: - z = count of dicoms - c = column value for every dicoms - r = Row value for every dicoms But I have a problem. When there is some space between slices, image is made by this way doesn't show a correct view. Because I can not think of simulation distance between them! I don't know how to calculate space between slices? I want to add extra space between slices. for example, If space between slices is 4. I have to add 4 time z inner

Pandas Dataframe datetime slicing with Index vs MultiIndex

Deadly 提交于 2019-12-03 15:42:34
With single indexed dataframe I can do the following: df2 = DataFrame(data={'data': [1,2,3]}, index=Index([dt(2016,1,1), dt(2016,1,2), dt(2016,2,1)])) >>> df2['2016-01 : '2016-01'] data 2016-01-01 1 2016-01-02 2 >>> df2['2016-01-01' : '2016-01-01'] data 2016-01-01 1 Date time slicing works when you give it a complete day (i.e. 2016-01-01), and it also works when you give it a partial date, like just the year and month (2016-01). All this works great, but when you introduce a multiindex, it only works for complete dates. The partial date slicing doesn't seem to work anymore df = DataFrame(data=

Create a slice using a tuple

梦想的初衷 提交于 2019-12-03 15:04:19
问题 Is there any way in python to use a tuple as the indices for a slice? The following is not valid: >>> a = range(20) >>> b = (5, 12) # my slice indices >>> a[b] # not valid >>> a[slice(b)] # not valid >>> a[b[0]:b[1]] # is an awkward syntax [5, 6, 7, 8, 9, 10, 11] >>> b1, b2 = b >>> a[b1:b2] # looks a bit cleaner [5, 6, 7, 8, 9, 10, 11] It seems like a reasonably pythonic syntax so I am surprised that I can't do it. (update) And the solution turns out to be: >>> a[slice(*b)] [5, 6, 7, 8, 9, 10