slice

Proper way to release string for garbage collection after slicing

…衆ロ難τιáo~ 提交于 2019-12-12 12:11:12
问题 According to this Go Data Structures article, under the Strings section it states that taking a slice of a string will keep the original string in memory. "(As an aside, there is a well-known gotcha in Java and other languages that when you slice a string to save a small piece, the reference to the original keeps the entire original string in memory even though only a small amount is still needed. Go has this gotcha too. The alternative, which we tried and rejected, is to make string slicing

Python: shorter syntax for slices with gaps?

末鹿安然 提交于 2019-12-12 08:27:58
问题 Suppose I want the first element, the 3rd through 200th elements, and the 201st element through the last element by step-size 3, from a list in Python. One way to do it is with distinct indexing and concatenation: new_list = old_list[0:1] + old_list[3:201] + old_list[201::3] Is there a way to do this with just one index on old_list ? I would like something like the following (I know this doesn't syntactically work since list indices cannot be lists and since Python unfortunately doesn't have

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

♀尐吖头ヾ 提交于 2019-12-12 08:23:00
问题 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

How to slice a string using a delimiter

醉酒当歌 提交于 2019-12-12 08:17:27
问题 In Go, if I have a string variable s: var s string = "a,b,c,d,e" How can I convert or split or explode it into a slice or an array of strings so that it will become: arr[0] = "a" ... arr[4] = "e" 回答1: You should use the strings package for that. stringSlice := strings.Split(s, ",") http://play.golang.org/p/UKZbcuJUPP 来源: https://stackoverflow.com/questions/14263733/how-to-slice-a-string-using-a-delimiter

Strange golang “append” behavior (overwriting values in slice)

和自甴很熟 提交于 2019-12-12 07:56:18
问题 I have this simple code: import "fmt" type Foo struct { val int } func main() { var a = make([]*Foo, 1) a[0] = &Foo{0} var b = [3]Foo{Foo{1}, Foo{2}, Foo{3}} for _, e := range b { a = append(a, &e) } for _, e := range a { fmt.Printf("%v ", *e) } } I was expecting it to print {0} {1} {2} {3} , however it prints {0} {3} {3} {3} . What happened here? 回答1: This is because in the for loop you operate with a copy and not with the slice/array element itself. The for ... range makes a copy of the

I don't understand slicing with negative bounds in Python. How is this supposed to work?

半世苍凉 提交于 2019-12-12 07:44:45
问题 I am a newbie to Python and have come across the following example in my book that is not explained very well. Here is my print out from the interpreter: >>> s = 'spam' >>> s[:-1] 'spa' Why does slicing with no beginning bound and a '-1' return every element except the last one? Is calling s[0:-1] logically the same as calling s[:-1] ? They both return the same result. But I'm not sure what python is doing exactly. Any help would be greatly appreciated. 回答1: Yes, calling s[0:-1] is exactly

Moving non-overlapping window in Numpy

故事扮演 提交于 2019-12-12 04:57:12
问题 What's the best way to move a window over a numpy array so that each individual block does not overlap with the previous one and there is a 1 element gap between the blocks? I guess I should use np.hstack, but I can't figure out the slicing pattern. Basically what I am looking for is this: a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) result = np.array([[0, 1, 2, 3], [5, 6, 7, 8]) Thanks! 回答1: What you want to to achieve in your short example can be done by reshaping the array, then removing

When slicing a 1 row pandas dataframe the slice becomes a series

人盡茶涼 提交于 2019-12-12 04:54:34
问题 Why when I slice a pandas dataframe containing only 1 row, the slice becomes a pandas series? How can I keep it a dataframe? df=pd.DataFrame(data=[[1,2,3]],columns=['a','b','c']) df Out[37]: a b c 0 1 2 3 a=df.iloc[0] a Out[39]: a 1 b 2 c 3 Name: 0, dtype: int64 回答1: To avoid the intermediate step of re-converting back to a DataFrame, use double brackets when indexing: a = df.iloc[[0]] print(a) a b c 0 1 2 3 Speed: %timeit df.iloc[[0]] 192 µs per loop %timeit df.loc[0].to_frame().T 468 µs per

“Slice” a number into three random numbers

耗尽温柔 提交于 2019-12-12 04:54:07
问题 I need to generate a file filled with three "random" values per line (10 lines), but those values sum must equal 15. The structure is: "INDEX A B C". Example: 1 15 0 0 2 0 15 0 3 0 0 15 4 1 14 0 5 2 13 0 6 3 12 0 7 4 11 0 8 5 10 0 9 6 9 0 10 7 8 0 回答1: If the numbers can by any just use combinations: from itertools import combinations with open("rand.txt","w") as f: combs = [x for x in combinations(range(16),3) if sum(x ) == 15 ][:10] for a,b,c in combs: f.write("{} {} {}\n".format(a,b,c))

How can a numpy.ufunc.reduceat indices be generated from Python Slice Object

﹥>﹥吖頭↗ 提交于 2019-12-12 04:21:55
问题 Say I have a slice like x[p:-q:n] or x[::n] I want to use this to generate the index to be passed into numpy.ufunc.reduceat(x, [p, p + n, p + 2 * n, ...]) or numpy.ufunc.reduceat(x, [0, n, 2 * n, ...]) . What is the easiest and efficient way to get it done? 回答1: Building on the comments: In [351]: x=np.arange(100) In [352]: np.r_[0:100:10] Out[352]: array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90]) In [353]: np.add.reduceat(x,np.r_[0:100:10]) Out[353]: array([ 45, 145, 245, 345, 445, 545, 645,