Question

def get_indices(lst, elm): ''' (list of lists, object) -> tuple of two ints Given a list...

def get_indices(lst, elm):

'''

(list of lists, object) -> tuple of two ints

Given a list of lists and an element, find the first

pair of indices at which that element is found and return

this as a tuple of two ints. The first int would be the

index of the sublist where the element occurs, and the

second int would be the index within this sublist where

it occurs.

>>> get_indices([[1, 3, 4], [5, 6, 7]], 1)

(0, 0)

>>> get_indices([[1, 3, 4], [5, 6, 7]], 7)

(1, 2)

'''

# YOUR CODE HERE #

pass

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def get_indices(lst, elm):
    """
    (list of lists, object) -> tuple of two ints
    Given a list of lists and an element, find the first
    pair of indices at which that element is found and return
    this as a tuple of two ints. The first int would be the
    index of the sublist where the element occurs, and the
    second int would be the index within this sublist where
    it occurs.
    >>> get_indices([[1, 3, 4], [5, 6, 7]], 1)
    (0, 0)
    >>> get_indices([[1, 3, 4], [5, 6, 7]], 7)
    (1, 2)
    """
    for i in range(len(lst)):
        for j in range(len(lst[i])):
            if lst[i][j] == elm:
                return i, j
    return -1, -1


print(get_indices([[1, 3, 4], [5, 6, 7]], 1))
print(get_indices([[1, 3, 4], [5, 6, 7]], 7))

Add a comment
Know the answer?
Add Answer to:
def get_indices(lst, elm): ''' (list of lists, object) -> tuple of two ints Given a list...
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
  • def _merge(lst: list, start: int, mid: int, end: int) -> None: """Sort the items in lst[start:end]...

    def _merge(lst: list, start: int, mid: int, end: int) -> None: """Sort the items in lst[start:end] in non-decreasing order. Precondition: lst[start:mid] and lst[mid:end] are sorted. """ result = [] left = start right = mid while left < mid and right < end: if lst[left] < lst[right]: result.append(lst[left]) left += 1 else: result.append(lst[right]) right += 1 # This replaces lst[start:end] with the correct sorted version. lst[start:end] = result + lst[left:mid] + lst[right:end] def find_runs(lst: list) -> List[Tuple[int, int]]: """Return a...

  • def slice_list(lst: List[Any], n: int) -> List[List[Any]]: """ Return a list containing slices of <lst> in...

    def slice_list(lst: List[Any], n: int) -> List[List[Any]]: """ Return a list containing slices of <lst> in order. Each slice is a list of size <n> containing the next <n> elements in <lst>. The last slice may contain fewer than <n> elements in order to make sure that the returned list contains all elements in <lst>. === Precondition === n <= len(lst) >>> slice_list([3, 4, 6, 2, 3], 2) == [[3, 4], [6, 2], [3]] True >>> slice_list(['a', 1, 6.0, False],...

  • class Node(object): def __init__(self, data, next=None): self.data = data self.next = next class List(object):    def...

    class Node(object): def __init__(self, data, next=None): self.data = data self.next = next class List(object):    def __init__(self): self.head = None self.tail = None Implement the following functions for Linked List in Python and use the constructors above : Copy(lList) Builds and returns a copy of list ItemAt(List,i) Returns the data item at position i in list Pop(List,i=0) Remove item at position i in list. If i is not specified, it removes the first item in list Count(List,x) Returns the number...

  • def list_to_dict(lst: list) -> dict: ''' Given a list L that may have sublists nested within...

    def list_to_dict(lst: list) -> dict: ''' Given a list L that may have sublists nested within it at an arbitrary depth, return a dictionary that has all the values from the list as its values, and the associated keys for each value are the indices that we would need to go through to get to that value. The key is represented as a string as exemplified below. e.g. In the list shown below, the value 'a' is at L[0] so...

  • def calculate_total(price_list: List[list]) -> int: """Return the sum of all second elements in the sublists of...

    def calculate_total(price_list: List[list]) -> int: """Return the sum of all second elements in the sublists of price_list. Precondition: price_list is a list of lists of length 2, and the second element of it sublist is an int. >>> price_list = [["apple", 1], ["sugar", 5], ["mango", 3], ... ["coffee", 9], ["trail mix", 6]] >>> calculate_total(price_list)

  • Python. Write a function that takes in a list and returns the first nonzero entry. def...

    Python. Write a function that takes in a list and returns the first nonzero entry. def nonzero(lst): """ Returns the first nonzero element of a list >>> nonzero([1, 2, 3]) 1 >>> nonzero([0, 1, 2]) 1 >>> nonzero([0, 0, 0, 0, 0, 0, 5, 0, 6]) 5 """ "*** YOUR CODE HERE ***"

  • match_enzymes: (str, List[str], List[str]) -> List[list] The return type is a list of two-item [str, List[int]]...

    match_enzymes: (str, List[str], List[str]) -> List[list] The return type is a list of two-item [str, List[int]] lists The first parameter represents a strand of DNA. The last two parameters are parallel lists: the second parameter is a list of restriction enzyme names, and the third is the corresponding list of recognition sequences. (For example, if the first item in the second parameter is 'BamHI', then the first item in the third parameter would be 'GGATCC', since the restriction enzyme named...

  • IN PYTHON 3 LANGUAGE, please help with function, USE RECURSION ONLY def im(l: 'an int, str,list,tuple,set,or...

    IN PYTHON 3 LANGUAGE, please help with function, USE RECURSION ONLY def im(l: 'an int, str,list,tuple,set,or dict') -> 'an int, str, tuple, or frozenset'      pass    SAMPLE OUTPUT: The following call (with many mutable data structures) imm(1)   returns 1 imm('a') returns 'a' imm( (1, 2, 3))   returns (1, 2, 3) imm( frozenset([1, 2, 3]))   returns frozenset({1, 2, 3}) imm( [1, 2, 3, 4, 5, 6])   returns (1, 2, 3, 4, 5, 6) imm( [1, 2, [3, [4], 5], 6])  ...

  • Python3: Write the function findLongestStreak(lst) which takes a list of numbers, lst, and returns the longest...

    Python3: Write the function findLongestStreak(lst) which takes a list of numbers, lst, and returns the longest streak of numbers which occur in the list. A streak of numbers is a list of numbers where each number is one greater than the one that occurred before it- for example, [4, 5, 6, 7]. So findLongestStreak([3, 4, 8, 2, 3, 4, 5, 7, 8, 9]) would return [2, 3, 4, 5]. If there is more than one streak with the longest length,...

  • def most_expensive_item(price_list: List[list]) -> str: """Return the name of the most expensive item in price_list. Precondition:...

    def most_expensive_item(price_list: List[list]) -> str: """Return the name of the most expensive item in price_list. Precondition: price_list is a list of lists in the following format: [ [str, int], [str, int], ... ] where each 2-element list represents a name (str) and a price (int) of an item. price_list has at least one element. >>> price_list = [["apple", 1], ["sugar", 5], ["mango", 3], ... ["coffee", 9], ["trail mix", 6]] >>> most_expensive_item(price_list) """ please complete the function body in Python

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