slice

How to check if interface{} is a slice

与世无争的帅哥 提交于 2019-12-01 01:37:10
问题 I'm noob in Go :) so my question may be stupid, but can't find answer, so. I need a function: func name (v interface{}) { if is_slice() { for _, i := range v { my_var := i.(MyInterface) ... do smth } } else { my_var := v.(MyInterface) ... do smth } } How can I do is_slice in Go? Appreciate any help. 回答1: In your case the type switch is the simplest and most convenient solution: func name(v interface{}) { switch x := v.(type) { case []MyInterface: fmt.Println("[]MyInterface, len:", len(x)) for

Customize Python Slicing, please advise

醉酒当歌 提交于 2019-12-01 01:19:11
问题 I have a class that subclasses the list object. Now I need to handle slicing. From everything I read on the intertubes this has to be done using the __getitem__ method. At least in Python 2.7+ which is what I'm using. I have done this (see below), but the __getitem__ method isn't called when I pass in a slice. Instead, a slice is done and a list is returned. I would like a new instance of myList returned. Please help me discover what is wrong. Thanks! class myList(list): def __init__(self,

How do I create two new mutable slices from one slice?

随声附和 提交于 2019-12-01 01:12:25
问题 I would like to take a mutable slice and copy the contents into two new mutable slices. Each slice being one half of the original. My attempt #1: let my_list: &mut [u8] = &mut [0, 1, 2, 3, 4, 5]; let list_a: &mut [u8] = my_list[0..3].clone(); let list_b: &mut [u8] = my_list[3..6].clone(); println!("{:?}", my_list); println!("{:?}", list_a); println!("{:?}", list_b); Output: error: no method named `clone` found for type `[u8]` in the current scope --> src/main.rs:3:43 | 3 | let list_a: &mut

How to slice a generator object or iterator in Python

╄→尐↘猪︶ㄣ 提交于 2019-12-01 00:05:28
问题 I would like to loop over a "slice" of an iterator. I'm not sure if this is possible as I understand that it is not possible to slice an iterator. What I would like to do is this: def f(): for i in range(100): yield(i) x = f() for i in x[95:]: print(i) This of course fails with: --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-37-15f166d16ed2> in <module>() 4 x = f() 5 ----> 6 for i in x[95:]: 7 print(i)

How to slice a string in PHP?

做~自己de王妃 提交于 2019-11-30 23:43:25
Ok, so I've got this string: "MICROSOFT CORP CIK#: 0000789019 (see all company filings)" And I would like to cut off everything after the "CORP" bit. How would I go about doing this in PHP? I am used to Python so I am not sure how this is done. To be clear, this is the output I want: "MICROSOFT CORP" I am trying: $companyname = substr($companyname, 0, strpos($companyname, " CIK")); and I am getting nothing showing. Here is my full code: <?php include 'simple_html_dom.php'; $html = file_get_html('http://www.sec.gov/cgi-bin/browse-edgar?company=&match=&CIK=MSFT&filenum=&State=&Country=&SIC=

Rails gem to break a paragraph into series of sentences

筅森魡賤 提交于 2019-11-30 23:42:51
I'm trying to split a paragraph into series of sentences such that each sentence group stays under N characters. In case of a single sentence that is longer than N, it should be split into chunks with punctuation marks or spaces as separators. E.g., if N = 50, then the following string "Lorem ipsum, consectetur elit. Donec ut ligula. Sed acumsan posuere tristique. Sed et tristique sem. Aenean sollicitudin, sapien sodales elementum blandit. Fusce urna libero blandit eu aliquet ac rutrum vel tortor." would become ["Lorem ipsum, consectetur elit. Donec ut ligula.", "Sed acumsan posuere tristique.

golang中慎用slice的赋值

随声附和 提交于 2019-11-30 23:27:28
一篇很典型的golang slice采坑记录: https://studygolang.com/articles/6557 有如下代码: type AutoGenerated struct { Age int `json:"age"` Name string `json:"name"` Child []int `json:"child"` } func main() { jsonStr1 := "{\"age\": 12,\"name\": \"potter\", \"child\":[1,2,3]}" a := AutoGenerated{} json.Unmarshal([]byte(jsonStr1), &a) aa := a.Child fmt.Println(aa) jsonStr2 := "{\"age\": 14,\"name\": \"potter\", \"child\":[3,4,5,7,8,9]}" json.Unmarshal([]byte(jsonStr2), &a) fmt.Println(aa) } 会发现,第一次打印aa时,aa是 [1,2,3],第二次打印aa时,aa就变成了[3,4,5] 这是因为两次调用 unmarshal 时,a 里面的 Child 字段实际上是同一个 slice,刚开始第一次 unmarshal 时,a.Child = [1

Golang: convert slices into map

醉酒当歌 提交于 2019-11-30 22:15:01
问题 Is there an easy/simple means of converting a slice into a map in Golang? Like converting an array into hash in perl is easy to do with simple assignment like %hash = @array this above will convert all the elements in the array into a hash, with keys being even-numbered index elements while the values will be odd-numbered index elements of the array. In my Go code, I have slices of string and would like to convert it into a map. I am wondering if there is a Go's library code to do this. func

JavaScript array slice versus delete

百般思念 提交于 2019-11-30 21:09:19
Is there any reason why one should be used over the other? e.g. var arData=['a','b','c']; arData.slice(1,1);//removes 'b' var arData=['a','b','c']; delete arData[1];//removes 'b' delete leaves you with [ 'a', undefined, 'c' ] splice leaves you with [ 'a', 'c' ] slice doesn't do anything to the original array :) But it returns [ 'b' ] in your code delete only makes that certain location of the array undefined but the array still contains 3 items: ['a',undefined,'c'] the other way to do it is splice and not slice . splice totally removes that item and it's location, so you end up with ['a','c']

Slicing a tensor by using indices in Tensorflow

断了今生、忘了曾经 提交于 2019-11-30 20:18:45
Basically I have a 2d array and I want to do this nice numpy-like thing noise_spec[:rows,:cols] in Tensorflow. Here rows and cols are just two integers. Indeed, TensorFlow now has better support for slicing, so you can use the exact same syntax as NumPy: result = noise_spec[:rows, :cols] found out, it's tf.slice(noise_spec, [0,0],[rows, cols]) 来源: https://stackoverflow.com/questions/41715511/slicing-a-tensor-by-using-indices-in-tensorflow