indices

TypeError: list indices must be integers, not list. How to fix?

五迷三道 提交于 2019-11-28 07:44:13
问题 There's the code with the TypeError in it. "list indices must be integers, not list", though they are integers. I'd appreciate you helping me figure out what's wrong. What I need is to get matrix 7x5 from source 7x5 tab with different values. The error occurs on the last line. lines = [] with open("text.txt") as f: for line in f: line = [int(x) for x in line if (x != ' ') and (x != '\n')] lines.append(line) f.close() What I have after reading file is list of lists with numbers called "lines".

Indexing a numpy array with a list of tuples

微笑、不失礼 提交于 2019-11-28 06:28:56
Why can't I index an ndarray using a list of tuple indices like so? idx = [(x1, y1), ... (xn, yn)] X[idx] Instead I have to do something unwieldy like idx2 = numpy.array(idx) X[idx2[:, 0], idx2[:, 1]] # or more generally: X[tuple(numpy.vsplit(idx2.T, 1)[0])] Is there a simpler, more pythonic way? You can use a list of tuples, but the convention is different from what you want. numpy expects a list of row indices, followed by a list of column values. You, apparently, want to specify a list of (x,y) pairs. http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#integer-array-indexing The

TypeError: list indices must be integers or slices, not list

ぐ巨炮叔叔 提交于 2019-11-28 03:14:15
问题 array = some kind of list with 3 columns and unlimited amount of rows with data inside of it. Volume = array[0][2] counter = 0 for i in array: if Volume == array[i][2]: #<------ why is this line a problem? counter += 1 回答1: This is a classic mistake. i in your case is already an element from array (i.e. another list), not an index of array ( not an int ), so if Volume == i[2]: counter += 1 Please, make sure to go at least through the beginning of Python tutorial, because this is very simple

Can someone please explain the “indices trick”?

99封情书 提交于 2019-11-27 08:54:46
I noticed the "indices trick" being mentioned in the context of pretty-printing tuples. It sounded interesting, so I followed the link . Well, that did not go well. I understood the question, but could really not follow what was going on. Why do we even need indices of anything? How do the different functions defined there help us? What is 'Bare'? etc. Can someone give a play-by-play of that thing for the less-than-experts on parameter packs and variadic tuples? Barry The problem is: we have a std::tuple<T1, T2, ...> and we have some function f that we can to call on each element, where f

Apply a function to a subset of data.table columns, by column-indices instead of name

南楼画角 提交于 2019-11-27 06:41:09
I'm trying to apply a function to a group of columns in a large data.table without referring to each one individually. a <- data.table( a=as.character(rnorm(5)), b=as.character(rnorm(5)), c=as.character(rnorm(5)), d=as.character(rnorm(5)) ) b <- c('a','b','c','d') with the MWE above, this: a[,b=as.numeric(b),with=F] works, but this: a[,b[2:3]:=data.table(as.numeric(b[2:3])),with=F] doesn't work. What is the correct way to apply the as.numeric function to just columns 2 and 3 of a without referring to them individually. (In the actual data set there are tens of columns so it would be

Indexing a numpy array with a list of tuples

六月ゝ 毕业季﹏ 提交于 2019-11-27 05:41:07
问题 Why can't I index an ndarray using a list of tuple indices like so? idx = [(x1, y1), ... (xn, yn)] X[idx] Instead I have to do something unwieldy like idx2 = numpy.array(idx) X[idx2[:, 0], idx2[:, 1]] # or more generally: X[tuple(numpy.vsplit(idx2.T, 1)[0])] Is there a simpler, more pythonic way? 回答1: You can use a list of tuples, but the convention is different from what you want. numpy expects a list of row indices, followed by a list of column values. You, apparently, want to specify a

Is JavaScript array index a string or an integer?

≯℡__Kan透↙ 提交于 2019-11-27 01:52:26
I had a generic question about JavaScript arrays. Are array indices in JavaScript internally handled as strings? I read somewhere that because arrays are objects in JavaScript, the index is actually a string. I am a bit confused about this, and would be glad for any explanation. That is correct so: > var a = ['a','b','c'] undefined > a [ 'a', 'b', 'c' ] > a[0] 'a' > a['0'] 'a' > a['4'] = 'e' 'e' > a[3] = 'd' 'd' > a [ 'a', 'b', 'c', 'd', 'e' ] Formally, all property names are strings. That means that array-like numeric property names really aren't any different from any other property names.

How to get the index of a maximum element in a numpy array along one axis

南笙酒味 提交于 2019-11-26 12:53:10
I have a 2 dimensional NumPy array. I know how to get the maximum values over axes: >>> a = array([[1,2,3],[4,3,1]]) >>> amax(a,axis=0) array([4, 3, 3]) How can I get the indices of the maximum elements? So I would like as output array([1,1,0]) >>> a.argmax(axis=0) array([1, 1, 0]) >>> import numpy as np >>> a = np.array([[1,2,3],[4,3,1]]) >>> i,j = np.unravel_index(a.argmax(), a.shape) >>> a[i,j] 4 SevakPrime argmax() will only return the first occurrence for each row. http://docs.scipy.org/doc/numpy/reference/generated/numpy.argmax.html If you ever need to do this for a shaped array, this

Apply a function to a subset of data.table columns, by column-indices instead of name

筅森魡賤 提交于 2019-11-26 12:06:55
问题 I\'m trying to apply a function to a group of columns in a large data.table without referring to each one individually. a <- data.table( a=as.character(rnorm(5)), b=as.character(rnorm(5)), c=as.character(rnorm(5)), d=as.character(rnorm(5)) ) b <- c(\'a\',\'b\',\'c\',\'d\') with the MWE above, this: a[,b=as.numeric(b),with=F] works, but this: a[,b[2:3]:=data.table(as.numeric(b[2:3])),with=F] doesn\'t work. What is the correct way to apply the as.numeric function to just columns 2 and 3 of a

How to number/label data-table by group-number from group_by?

做~自己de王妃 提交于 2019-11-26 09:02:43
I have a tbl_df where I want to group_by(u, v) for each distinct integer combination observed with (u, v) . EDIT: this was resolved by adding group_indices() back in dplyr 0.4.0 a) I then want to assign each distinct group some arbitrary distinct number label=1,2,3... e.g. the combination (u,v)==(2,3) could get label 1, (1,3) could get 2, and so on. How to do this with one mutate() , without a three-step summarize-and-self-join? dplyr has a neat function n() , but that gives the number of elements within its group, not the overall number of the group . In data.table this would simply be called