Question

Write a function in python, index(arr, value) to find indices of elements equal to some value...

Write a function in python, index(arr, value) to find indices of elements equal to some value in a Numpy array. The input arr is a 1-d numpy array, and input value is the value to search for. The function should return the index of the value. If the value occurs for multiple times, then all the indices should be returned as a 1-d numpy array. For example, if arr=[1 0 2 0 3 0 4 0 5 0 6 7 8 0], then index(arr,3) should return 4, and index(arr,0) should return [1 3 5 7 9 13].

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import numpy as np


def index(arr, value):
    result = []
    for i in range(len(arr)):
        if arr[i] == value:
            result.append(i)
    if len(result) == 0:
        return -1
    elif len(result) == 1:
        return result[0]
    else:
        return np.array(result)


# Testing the function here. ignore/remove the code below if not required
arr = np.array([1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 7, 8, 0])
print(index(arr, 3))
print(index(arr, 0))

[ 1 3 5 7 9 13] Process finished with exit code o

Add a comment
Know the answer?
Add Answer to:
Write a function in python, index(arr, value) to find indices of elements equal to some value...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C language Write a program that includes a function search() that finds the index of...

    In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...

  • Create a function l_mode that finds the index of the value that occurs most often in...

    Create a function l_mode that finds the index of the value that occurs most often in an array. The input to your function is an array of integers A and the number of elements in the array 0 < n <= 10^3. Assuming v is the most frequent element in the array, your function will return the index i of the first (leftmost) occurrence of v in A. You can assume that -500 <= A[i] <= 500, for 0 <=...

  • #include <assert.h> #include <stdio.h> #include <stdlib.h> // initialize_array is given an array "arr" of "n" elements....

    #include <assert.h> #include <stdio.h> #include <stdlib.h> // initialize_array is given an array "arr" of "n" elements. // It initializes the array by setting all elements to be "true" (any non-zero value). void initialize_array(int *arr, int n) { // TODO: Your code here. assert(0); } // mark_multiples is given an array "arr" of size n and a (prime) number "p" less than "n" // It assigns "false" (the zero value) to elements at array indexes 2*p, 3*p, 4*p,.., x*p (where x*p...

  • IN JAVA please Given a sorted array and a target value, return the index if the...

    IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...

  • 1. use python List to Dictionary Write a function that has three parameters: a list of...

    1. use python List to Dictionary Write a function that has three parameters: a list of unsorted numbers with no duplicates, a start number, and an end number. This function should return a dictionary with all integers between the start and end number (inclusive) as the keys and their respective indices in the list as the value. If the integer is not in the list, the corresponding value would be None. Example unsorted list: [2,1,10,0,4,3] two numbers: 3, 10 returned...

  • USING C++ and PYTHON Please Help me: 1. Write a function that takes in an array...

    USING C++ and PYTHON Please Help me: 1. Write a function that takes in an array of integers (and the size of the array in the C/C++ version). The function will reverse the array of integers and return the reversed array. Print the array that is returned from the function. How you "return" the array is up to you, but do not overwrite the original array. Note: in Python, the term list is more appropriate, see https://docs.python.org/3.5/tutorial/datastructures.html a) Example input:...

  • FOR PYTHON: Write a function findMinRow() that takes a two-dimensional list of numbers as a parameter....

    FOR PYTHON: Write a function findMinRow() that takes a two-dimensional list of numbers as a parameter. It returns the index of the minimum row in the list. The minimum row is the row with the smallest sum of elements. If the list has multiple rows that achieve the minimum value, the last such row should be returned. If the list is empty the function should return -1. The following shows several sample runs of the function: Python 3.4.1 Shell File...

  • Using PYTHON: (Find the index of the smallest element) Write a function that returns the index...

    Using PYTHON: (Find the index of the smallest element) Write a function that returns the index of the smallest element in a list of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: def indexOfSmallestElement(lst): Write a test program that prompts the user to enter a list of numbers, invokes this function to return the index of the smallest element, and displays the index. PLEASE USE LISTS!

  • Given an array of integer, please complete a function multiply(). The function accepts the first memory...

    Given an array of integer, please complete a function multiply(). The function accepts the first memory address of an array and the size of an array as parameters and returns the multiplication of all the elements. In order to get a full score, the function must use a loop to iterate through each element of an array. Pseudo code example:   multiply([ 1, 2, 3], 3) → 6   multiply([1, 1, 4 ,2], 4) → 8   multiply([7, 0, 0, 7, 0, 7],...

  • 5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below....

    5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below. All the methods accept the following parameters: Parameters: intar An array of int values. int firstIndex The index of the first element to include in the sum. int lastIndex The index of the last element to include in the sum. Please note that all methods must verify the validity of first Index and lastIndex. public static int sum(int[] arr, int first Index, int lastIndex)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT