slice

slicing behaviour question of a list of lists

余生长醉 提交于 2019-12-05 18:47:12
I got a function like def f(): ... ... return [list1, list2] this returns a list of lists [[list1.item1,list1.item2,...],[list2.item1,list2.item2,...]] now when I do the following: for i in range(0,2):print f()[i][0:10] it works and print the lists sliced but if i do print f()[0:2][0:10] then it prints the lists ignoring the [0:10] slicing. Is there any way to make the second form work or do I have to loop every time to get the desired result? The reason why these two behave differently is because f()[0:2][0:10] works like this: f() gives you a list of lists. [0:2] gives you a list containing

Theano tensor slicing… how to use boolean to slice?

坚强是说给别人听的谎言 提交于 2019-12-05 18:34:24
In numpy, if I have a boolean array, I can use it to select elements of another array: >>> import numpy as np >>> x = np.array([1, 2, 3]) >>> idx = np.array([True, False, True]) >>> x[idx] array([1, 3]) I need to do this in theano. This is what I tried, but I got an unexpected result. >>> from theano import tensor as T >>> x = T.vector() >>> idx = T.ivector() >>> y = x[idx] >>> y.eval({x: np.array([1,2,3]), idx: np.array([True, False, True])}) array([ 2., 1., 2.]) Can someone explain the theano result and suggest how to get the numpy result? I need to know how to do this in order to properly

numpy 3D-image array to 2D

偶尔善良 提交于 2019-12-05 16:43:22
问题 I have a 3D-numpy array of a gray image, which looks something like this: [[[120,120,120],[67,67,67]]...] Obviously I have every R G and B the same because it is a gray image - this is redundent. I want to get a new 2D array which looks like: [[120,67]...] Which means to take every pixel's array [x,x,x] to just the value x How can I do that? 回答1: If the shape of your ndarray is (M, N, 3), then you can get an (M, N) gray-scale image like this: >>> gray = img[:,:,0] 来源: https://stackoverflow

Select multiple sections of rows by index in pandas

假装没事ソ 提交于 2019-12-05 16:07:57
I have large DataFrame with GPS path and some attributes. A few sections of the path are those which I need to analyse. I would like to subset only those sections to a new DataFrame. I can subset one section at the time but the idea is to have them all and to have an original index. The problem is similar to: import pandas as pd df = pd.DataFrame({'A':[0,1,2,3,4,5,6,7,8,9],'B':['a','b','c','d','e','f','g','h','i','j']}, index=range(10,20,)) I want o get something like: cdf = df.loc[[11:13] & [17:20]] # SyntaxError: invalid syntax desired outcome: A B 11 1 b 12 2 c 13 3 d 17 7 h 18 8 i 19 9 j I

Python code for sum with condition

戏子无情 提交于 2019-12-05 14:14:14
The task is following: sum the list elements with even indexes and multiply the result by the last list's elemet. I have this oneliner solution code in Python. array = [-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41] print sum(i for i in array if array.index(i) % 2 == 0)*array[-1] if array != [] else 0 My result is -1476 ( The calculation is: 41*(-37-19+29+3-64+36+26+55+84-65) ) The right result is 1968. I can't figure it out why this code is not working correctly in this particular case. There is a repeated element 84 in the list, thus array.index does not work as you

pandas DataFrame 警告(SettingWithCopyWarning)

两盒软妹~` 提交于 2019-12-05 12:35:49
报警: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy 简单述之: A为一个dataframe, B = A[['a', 'b']] B['a'] = B['a'] + 1将会有上面的警告,提示最后一行有问题,但实际上应该修改上一行,为: B = A.loc[:,['a','b']]详见: https://www.cnblogs.com/pig-fly/p/7875472.html 来源: https://www.cnblogs.com/sxinfo/p/11925357.html

Slicing a nested hash in Perl

扶醉桌前 提交于 2019-12-05 12:06:33
Say I have a hash that I can index as: $hash{$document}{$word} From what I read online (although I could not find this on perlreftut , perldsc or perllol ), I can slice a hash using a list if I use the @ prefix on my hash to indicate that I want the hash to return a list. However, if I try to slice my hash using a list @list : @%hash{$document}{@list} I get several "Scalar values ... better written" errors. How can I slash a nested hash in Perl? The sigill for your hash must be @ , like so: @{$hash{$document}}{@list} Assuming @list contains valid keys for %hash it will return the corresponding

Using a string to define Numpy array slice

旧时模样 提交于 2019-12-05 11:52:32
I have an image array that has an X times Y shape of 2048x2088. The x-axis has two 20 pixel regions, one at the start and one at the end, which are used to calibrate the main image area. To access these regions I can slice the array like so: prescan_area = img[:, :20] data_area = img[:, 20:2068] overscan_area = img[:, 2068:] My question is how to define these areas in a configuration file in order the generalise this slice for other cameras which may have different prescan and overscan areas and therefore require a different slice. Ideally, something like the strings below would allow a simple

Why are you unable convert Slice types?

北城以北 提交于 2019-12-05 11:47:45
I was wondering why you can't do: type Foo struct { A int } type Bar Foo foos := []Foo{Foo{1}, Foo{2}} bars := []Bar(foos) //cannot convert foos (type []Foo) to type []Bar and I found out that this would require the runtime to perform a loop over the slice to convert each of the elements, which would be non-idiomatic Go. This makes sense. However, could this not be solved by the compiler just aliasing Bar as Foo , so internally they're the same and they use the same type header underneath? I'm guessing the answer is no though I'm curious as to why. This: []Bar(foos) is a type conversion .

How do I slice an array from an array of object literals?

雨燕双飞 提交于 2019-12-05 10:40:34
I have this array, in which each index contains an object literal. All of the object literals have the same properties. Some of the object literals have the same value for a given property, and I want to create a new array containing only those object literals. My idea is to sort the array, and slice it into a new array... Here is the array: var arr = []; arr[0] = { country: "United States", num: 27 }; arr[1] = { country: "Australia", num: 5 }; arr[2] = { country: "United States", num: 7 }; So, I want to create a new array containing only those objects where the property country is "United