indices

Python: Finding corresponding indices for an intersection of two lists

﹥>﹥吖頭↗ 提交于 2019-12-01 20:02:48
This is somewhat related to a question I asked not too long ago today. I am taking the intersection of two lists as follows: inter = set(NNSRCfile['datetimenew']).intersection(catdate) The two components that I am taking the intersection of belong to two lengthy lists. Is it possible to get the indices of the intersected values? (The indices of the original lists that is). I'm not quite sure where to start with this one. Any help is greatly appreciated! I would create a dictionary to hold the original indices: ind_dict = dict((k,i) for i,k in enumerate(NNSRCfile['datetimenew'])) Now, build

Randomly extract x items from a list using python

我是研究僧i 提交于 2019-12-01 17:41:29
问题 Starting with two lists such as: lstOne = [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] lstTwo = [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] I want to have the user input how many items they want to extract, as a percentage of the overall list length, and the same indices from each list to be randomly extracted. For example say I wanted 50% the output would be newLstOne = ['8', '1', '3', '7', '5'] newLstTwo = ['8', '1', '3', '7', '5'] I have achieved this using the following

matlab: dividing vector into overlapping chunks of fixed size

杀马特。学长 韩版系。学妹 提交于 2019-12-01 15:11:08
问题 I've a vector that I would like to split into overlapping subvectors of size cs in shifts of sh . Imagine the input vector is: v=[1 2 3 4 5 6 7 8 9 10 11 12 13]; % A=[1:13] given a chunksize of 4 ( cs=4 ) and shift of 2 ( sh=2 ), the result should look like: [1 2 3 4] [3 4 5 6] [5 6 7 8] [7 8 9 10] [9 10 11 12] note that the input vector is not necessarily divisible by the chunksize and therefore some subvectors are discarded. Is there any fast way to compute that, without the need of using e

Access entries in pandas data frame using a list of indices

£可爱£侵袭症+ 提交于 2019-12-01 08:00:01
I facing the issue that I need only a subset of a my original dataframe that is distributed over different rows and columns. E.g.: # My Original dataframe import pandas as pd dfTest = pd.DataFrame([[1,2,3],[4,5,6],[7,8,9]]) Output: 0 1 2 0 1 2 3 1 4 5 6 2 7 8 9 I can provide a list with rows and column indices where my desired values are located: array_indices = [[0,2],[1,0],[2,1]] My desired output is a series: 3 4 8 Can anyone help? Use pd.DataFrame.lookup dfTest.lookup(*zip(*array_indices)) array([3, 4, 8]) Which you can wrap in a pd.Series constructor pd.Series(dfTest.lookup(*zip(*array

Print a matrix without row and column indices

无人久伴 提交于 2019-11-30 18:37:07
If I print a matrix, it is shown with row and column indices in the console. E.g. > print(diag(3)) [,1] [,2] [,3] [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 1 How can I suppress the column and row indices? I.e. something like this: > print(diag(3), indices=FALSE) 1 0 0 0 1 0 0 0 1 I can see that the cwhmisc package should contain a printM function to do this according to the documentation but it is not there when I load cwhmisc. Also, this seems like something you should be able to to in base R. user1981275 The function prmatrix in the base package could work for this, it can take the arguments collab and

Assigning identical array indices at once in Python/Numpy

≡放荡痞女 提交于 2019-11-30 09:45:49
I want to find a fast way (without for loop) in Python to assign reoccuring indices of an array. This is the desired result using a for loop: import numpy as np a=np.arange(9, dtype=np.float64).reshape((3,3)) # The array indices: [2,3,4] are identical. Px = np.uint64(np.array([0,1,1,1,2])) Py = np.uint64(np.array([0,0,0,0,0])) # The array to be added at the array indices (may also contain random numbers). x = np.array([.1,.1,.1,.1,.1]) for m in np.arange(len(x)): a[Px[m]][Py[m]] += x print a %[[ 0.1 1. 2.] %[ 3.3 4. 5.] %[ 6.1 7. 8.]] When I try to add x to a at the indices Px,Py I obviously

numpy function to set elements of array to a value given a list of indices

匆匆过客 提交于 2019-11-29 11:42:30
问题 I'm looking for a numpy function that will do the equivalent of: indices = set([1, 4, 5, 6, 7]) zero = numpy.zeros(10) for i in indices: zero[i] = 42 回答1: You can just give it a list of indices: indices = [1, 4, 5, 6, 7] zero = numpy.zeros(10) zero[indices] = 42 回答2: If you have an ndarry: >>> x = np.zeros((3, 3, 3)) >>> y = [0, 9, 18] >>> x array([[[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]], [[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]], [[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]]]) >>>

NumPy k-th diagonal indices

╄→尐↘猪︶ㄣ 提交于 2019-11-29 11:36:16
问题 I'd like to do arithmetics with k-th diagonal of a numpy.array. I need those indices. For example, something like: >>> a = numpy.eye(2) >>> a[numpy.diag_indices(a, k=-1)] = 5 >>> a array([[ 1., 0.], [ 5., 1.]]) Unfortunately, diag_indices only returns the indices comprising the main diagonal, so at the moment I am doing: a += numpy.diag([5], -1) But that doesn't seem as nice or robust. :-) Is there a way in numpy to get indices for other than the main diagonal? 回答1: A bit late, but this

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

两盒软妹~` 提交于 2019-11-29 09:57:47
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 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 and fundamental stuff. Also I would advise to stick to naming conventions: variables are normally lower-case

compare two lists in python and return indices of matched values

家住魔仙堡 提交于 2019-11-28 10:58:35
For two lists a and b, how can I get the indices of values that appear in both? For example, a = [1, 2, 3, 4, 5] b = [9, 7, 6, 5, 1, 0] return_indices_of_a(a, b) would return [0,4] , with (a[0],a[4]) = (1,5) . The best way to do this would be to make b a set since you are only checking for membership inside it. >>> a = [1, 2, 3, 4, 5] >>> b = set([9, 7, 6, 5, 1, 0]) >>> [i for i, item in enumerate(a) if item in b] [0, 4] def return_indices_of_a(a, b): b_set = set(b) return [i for i, v in enumerate(a) if v in b_set] For larger lists this may be of help: for item in a: index.append(bisect.bisect