slice

slicing list of lists in Python

廉价感情. 提交于 2019-12-17 19:41:52
问题 I need to slice a list of lists in python. A = [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]] idx = slice(0,4) B = A[:][idx] The code above isn't giving me the right output. What I want is : [[1,2,3],[1,2,3],[1,2,3]] 回答1: With numpy it is very simple - you could just perform the slice: In [1]: import numpy as np In [2]: A = np.array([[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]]) In [3]: A[:,:3] Out[3]: array([[1, 2, 3], [1, 2, 3], [1, 2, 3]]) You could, of course, transform numpy.array back to the list : In

Is there analog of memset in go?

萝らか妹 提交于 2019-12-17 19:32:55
问题 In C++ I can initialize an array with some value using memset: const int MAX = 1000000; int is_prime[MAX] memset(is_prime, 1, sizeof(is_prime)) What memset does, crudely can be described as filling the array with some value, but doing this really really fast. In go I can do is_prime := make([]int, 1000000) , but this will create a slice with all 0, in the similar manner I can use new([1000000]int) , but nothing will allow me to create an array/slice with all 1 or any other non-zero element.

golang prepend a string to a slice …interface{}

馋奶兔 提交于 2019-12-17 19:13:14
问题 I've a method that has as an argument v ...interface{} , I need to prepend this slice with a string . Here is the method: func (l Log) Error(v ...interface{}) { l.Out.Println(append([]string{" ERROR "}, v...)) } When I try with append() it doesn't work: > append("some string", v) first argument to append must be slice; have untyped string > append([]string{"some string"}, v) cannot use v (type []interface {}) as type string in append What's the proper way to prepend in this case? 回答1: append(

Show u8 slice in hex representation

£可爱£侵袭症+ 提交于 2019-12-17 19:11:53
问题 I need to convert &[u8] to a hex representation. For example [ A9, 45, FF, 00 ... ] . The trait std::fmt::UpperHex is not implemented for slices (so I can't use std::fmt::format ). Rust has the serialize::hex::ToHex trait, which converts &[u8] to a hex String, but I need a representation with separate bytes. I can implement trait UpperHex for &[u8] myself, but I'm not sure how canonical this would be. What is the most canonical way to do this? 回答1: Rust 1.26.0 and up The :x? "debug with

Improving pure Python prime sieve by recurrence formula

不打扰是莪最后的温柔 提交于 2019-12-17 18:23:02
问题 I am trying to optimize further the champion solution in prime number thread by taking out the complex formula for sub-list length. len() of the same subsequence is too slow as len is expensive and generating the subsequence is expensive. This looks to slightly speed up the function but I could not yet take away the division, though I do the division only inside the condition statement. Of course I could try to simplify the length calculation by taking out the optimization of starting marking

Register multiple routes using range for loop slices/map

∥☆過路亽.° 提交于 2019-12-17 17:11:49
问题 Consider I have slice of string paths: paths := []string{"/path0", "/path1", "/path2" /*... "/path-n"*/ } // where n is the last path Using package net/http , I want to register handler for this path using for loop with range clause. This is how I do this: for _, path := range paths { http.HandleFunc(path, handler) } // in this case every handler is print the path to the console or to the browser EDIT: Basically the asker used this code: for _, path := range paths { http.HandleFunc(path, func

Slicing a list using a variable, in Python

眉间皱痕 提交于 2019-12-17 16:15:00
问题 Given a list a = range(10) You can slice it using statements such as a[1] a[2:4] However, I want to do this based on a variable set elsewhere in the code. I can easily do this for the first one i = 1 a[i] But how do I do this for the other one? I've tried indexing with a list: i = [2, 3, 4] a[i] But that doesn't work. I've also tried using a string: i = "2:4" a[i] But that doesn't work either. Is this possible? 回答1: that's what slice() is for: a = range(10) s = slice(2,4) print a[s] That's

Does go garbage collect parts of slices?

十年热恋 提交于 2019-12-17 15:56:14
问题 If I implement a queue like this... package main import( "fmt" ) func PopFront(q *[]string) string { r := (*q)[0] *q = (*q)[1:len(*q)] return r } func PushBack(q *[]string, a string) { *q = append(*q, a) } func main() { q := make([]string, 0) PushBack(&q, "A") fmt.Println(q) PushBack(&q, "B") fmt.Println(q) PushBack(&q, "C") fmt.Println(q) PopFront(&q) fmt.Println(q) PopFront(&q) fmt.Println(q) } ... I end up with an array ["A", "B", "C"] that has no slices pointing to the first two elements.

Slicing Sparse Matrices in Scipy — Which Types Work Best?

故事扮演 提交于 2019-12-17 15:38:52
问题 The SciPy Sparse Matrix tutorial is very good -- but it actually leaves the section on slicing un(der)developed (still in outline form -- see section: "Handling Sparse Matrices"). I will try and update the tutorial, once this question is answered. I have a large sparse matrix -- currently in dok_matrix format. import numpy as np from scipy import sparse M = sparse.dok_matrix((10**6, 10**6)) For various methods I want to be able to slice columns and for others I want to slice rows. Ideally I

Python reverse-stride slicing

独自空忆成欢 提交于 2019-12-17 15:38:44
问题 A specific example of my question is, "How can I get '3210' in this example?" >>> foo = '0123456' >>> foo[0:4] '0123' >>> foo[::-1] '6543210' >>> foo[4:0:-1] # I was shooting for '3210' but made a fencepost error, that's fine, but... '4321' >>> foo[3:-1:-1] # How can I get '3210'? '' >>> foo[3:0:-1] '321' It seems strange that I can write foo[4:0:-1], foo[5:1:-1], etc. and get what I would expect, but there's no way to write the slice so that I get '3210'. A makeshift way of doing this would