slice

Efficiently chunk large vector into a vector of vectors

我的梦境 提交于 2020-05-29 15:43:10
问题 I want to chunk a large vector into a vector of vectors. I know about chunks() , but am not sure of the best way to go from the iterator to a 2D Vec . I have found the following to work, but is there a better way to write this? let v: Vec<i32> = vec![1, 1, 1, 2, 2, 2, 3, 3, 3]; let v_chunked: Vec<Vec<i32>> = v.chunks(3).map(|x| x.to_vec()).collect(); println!("{:?}", v_chunked); // [[1, 1, 1], [2, 2, 2], [3, 3, 3]] https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist

Confusion between [T] and &[T]

霸气de小男生 提交于 2020-05-26 11:23:20
问题 I'm currently confused by [T] and &[T] in Rust. Let's start by what I know: [T; n] is an array of n elements, &[T; n] is a pointer to an array with n elements, [T] is unsized and points to sequence of elements T , while &[T] is a sized fat pointer and points to a sequence of elements T . My confusion starts with the naming convention of the two items. From the documentation of Rust, they provide the following example: let a: [i32; 5] = [1, 2, 3, 4, 5]; // An array of type [T, n] let slice: &

Check whether a string slice contains a certain value in Go

纵然是瞬间 提交于 2020-05-24 21:07:07
问题 What is the best way to check whether a certain value is in a string slice? I would use a Set in other languages, but Go doesn't have one. My best try is this so far: package main import "fmt" func main() { list := []string{"a", "b", "x"} fmt.Println(isValueInList("b", list)) fmt.Println(isValueInList("z", list)) } func isValueInList(value string, list []string) bool { for _, v := range list { if v == value { return true } } return false } http://play.golang.org/p/gkwMz5j09n This solution

Slice all strings in a list?

霸气de小男生 提交于 2020-05-13 06:17:07
问题 Is there a Pythonic way to slice all strings in a list? Suppose I have a list of strings: list = ['foo', 'bar', 'baz'] And I want just the last 2 characters from each string: list2 = ['oo', 'ar', 'az'] How can I get that? I know I can iterate thru the list and take list[i][-2:] from each one, but that doesn't seem very Pythonic. Less generally, my code is: def parseIt(filename): with open(filename) as f: lines = f.readlines() result = [i.split(',') for i in lines[]] ...except I only want to

Slice all strings in a list?

家住魔仙堡 提交于 2020-05-13 06:17:07
问题 Is there a Pythonic way to slice all strings in a list? Suppose I have a list of strings: list = ['foo', 'bar', 'baz'] And I want just the last 2 characters from each string: list2 = ['oo', 'ar', 'az'] How can I get that? I know I can iterate thru the list and take list[i][-2:] from each one, but that doesn't seem very Pythonic. Less generally, my code is: def parseIt(filename): with open(filename) as f: lines = f.readlines() result = [i.split(',') for i in lines[]] ...except I only want to

Golang slice append vs assign performance

孤街醉人 提交于 2020-05-11 04:10:43
问题 To make slice append operation faster we need to allocate enough capacity. There's two ways to append slice, Here is the code: func BenchmarkSliceAppend(b *testing.B) { a := make([]int, 0, b.N) for i := 0; i < b.N; i++ { a = append(a, i) } } func BenchmarkSliceSet(b *testing.B) { a := make([]int, b.N) for i := 0; i < b.N; i++ { a[i] = i } } And the result is: BenchmarkSliceAppend-4 200000000 7.87 ns/op 8 B/op 0 allocs/op BenchmarkSliceSet-4 300000000 5.76 ns/op 8 B/op a[i] = i is faster than

Match along last axis in numpy array

馋奶兔 提交于 2020-05-09 17:13:19
问题 I saw a lot of articles and answers to other questions about slicing 3D lists in python, but I can't apply those methods to my case. I have a 3D list: list = [ [[0, 56, 78], [4, 86, 90], [7, 87, 34]], [[1, 49, 76], [0, 76, 78], [8, 60, 7]], [[9, 6, 58], [6, 57, 78], [10, 46, 2]] ] The the last 2 values of the 3rd dimension stay constant but change every time I rerun the code. What the code needs to do is find 2 specific pairs of those last 2 values and slice from one pair to the other. So for

Retrieve length of slice from slice object in Python

梦想的初衷 提交于 2020-05-08 22:41:23
问题 The title explains itself, how to get 2 out of the object slice(0,2) The documentation is somewhat confusing, or it is the wrong one https://docs.python.org/2/c-api/slice.html In particular I don't understand what is the meaning of the output of slice(0,2).indices(0) # (0, 0, 1) slice(0,2).indices(10 ** 10) # (0, 2, 1) One possible workaround is to slice a list with the slice object a = [1,2,3,4,5] len(a[slice(0,2)]) # 2 But this will fail for an arbitrary large slice. Thanks, I couldn't find

Retrieve length of slice from slice object in Python

谁都会走 提交于 2020-05-08 22:38:06
问题 The title explains itself, how to get 2 out of the object slice(0,2) The documentation is somewhat confusing, or it is the wrong one https://docs.python.org/2/c-api/slice.html In particular I don't understand what is the meaning of the output of slice(0,2).indices(0) # (0, 0, 1) slice(0,2).indices(10 ** 10) # (0, 2, 1) One possible workaround is to slice a list with the slice object a = [1,2,3,4,5] len(a[slice(0,2)]) # 2 But this will fail for an arbitrary large slice. Thanks, I couldn't find

How does type conversion internally work? What is the memory utilization for the same?

南楼画角 提交于 2020-05-08 04:31:10
问题 How does Go type conversion internally work? What is the memory utilisation for a type cast? For example: var str1 string str1 = "26MB string data" byt := []byte(str1) str2 := string(byt) whenever I type convert any variable, will it consume more memory? I am concerned about this because when I try to unmarshall, I get " fatal error: runtime: out of memory " err = json.Unmarshal([]byte(str1), &obj) str1 value comes from HTTP response, but read using ioutils.ReadAll, hence it contains the