Question

Define a function called max_association(), which recieves 2 lists, lst1, which is a list of strings,...

Define a function called max_association(), which recieves 2 lists, lst1, which is a list of strings, and lst2, which is a list of integers. The function must return a list of strings associated to the highest integers in lst1. Assume both lists have the same length

As an example, the following code fragment:

L1 = ['a','b','c','d','e','f','g','h','i','j'] L2 = [1,2,3,1,3,2,3,1,2,1] result = max_association(L1,L2) print(result)

should produce the output:

['c', 'e', 'g']

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def max_association(lst1, lst2):
    if(lst2!=None and len(lst2)>0):
        maxValue = lst2[0]
        for x in lst2:
            if(maxValue < x):
                maxValue = x
        result = []
        for i in range(len(lst1)):
            if(lst2[i]==maxValue):
                result.append(lst1[i])
        return result


# Testing
L1 = ['a','b','c','d','e','f','g','h','i','j']
L2 = [1,2,3,1,3,2,3,1,2,1]
result = max_association(L1,L2)
print(result)

Output

[c, e,g) Process finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
Define a function called max_association(), which recieves 2 lists, lst1, which is a list of strings,...
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
  • PYTHON Define a function reverseNestedList(...) which receives one possibly nested list (lst1) and returns the reverse...

    PYTHON Define a function reverseNestedList(...) which receives one possibly nested list (lst1) and returns the reverse of both the given list and any nested lists inside it. Note that the testing inputs will not include lists that are nested more than twice (a list in a list in a list ++...) [[[...],1], 1] Good luck! As an example, the following code fragment: lst1 = [[1, 2], [3, 4]] print(reverseNestedList(lst1)) should produce the output: [[4, 3], [2, 1]]

  • PYTHON define a function called fav_colours(): which receives a list of strings of lowercasecolours: the list...

    PYTHON define a function called fav_colours(): which receives a list of strings of lowercasecolours: the list some words will repeat, one colour or many, long list or short, Will always be lowercase - return a list of lists containing unique colours and integer numbers describing the amount of times those colours repeated As an example, the following code fragment: colours = ['blue', 'red', 'red', 'red', 'green'] colours , numbers = fav_colours(colours) print(colours) print(numbers) should produce the output: ['blue', 'red', 'green']...

  • . Write a Scheme program for each of the following. Which takes in two lists “lst1” and “lst2”, b...

    . Write a Scheme program for each of the following. Which takes in two lists “lst1” and “lst2”, both of which are individually sorted (in ascending order), and returns a list containing the elements of both lst1 and lst2 in sorted order. Print the final list. Input to the program are “lst1” and “lst2”. For instance, lst1 = (1 3 5 7) and lst2 = (2 4 6 8) output: (1 2 3 4 5 6 7 8)

  • Define a function called collapse() which takes a list as input. Each element of the list...

    Define a function called collapse() which takes a list as input. Each element of the list will either be an integer, or a list of integers. The function should modify the input list by replacing all of the elements which themselves are lists with the sum of their elements. For example: Test Result vals = [1, 2, 3, 4, 5] collapse(vals) print(vals) [1, 2, 3, 4, 5] vals = [1, [2, 3], 4, 5] collapse(vals) print(vals) [1, 5, 4, 5]...

  • 0% of final (S1 2019) Write a function called sum_of_products(x), where x is a list of lists of i...

    0% of final (S1 2019) Write a function called sum_of_products(x), where x is a list of lists of integers, this function returns a number which is the sum of the products of each list of integers. For example: print(sum_of_products ([[1,2], [5], [2,5]])) will result in the output: 17 That is, the result of 2 + 5+ 10 For example: Test Result print(sum_of_products( [2,3]])) print sum_of_products([[2],[3]])) print(sum_of_products([[1,21,[S],[2,5]1)) 17 0% of final (S1 2019) Write a function called sum_of_products(x), where x is...

  • Define a function named how_many_substr_of_string(...) which receives two parameters, the first parameters is a list with...

    Define a function named how_many_substr_of_string(...) which receives two parameters, the first parameters is a list with strings (name it  listst) and the second parameter is a single string (name it st). The function should return a number, indicating how many of the strings in the list listst are substrings in the string st As an example, the following code fragment: listst = ["a","bc","dd"] st = "abc" res = how_many_substr_of_string(listst,st) print (res) should produce the output: 2 language:Python

  • Define a function called AddEvenPosDigs(string), which takes a string (with symbols and characters) as an argument,...

    Define a function called AddEvenPosDigs(string), which takes a string (with symbols and characters) as an argument, and returns an integer. This function should add the digits of a string that are in an even position. If there are no digits, the function should return -1. As an example, the following code fragment: string = "a12b056jk"; result=AddEvenPosDigs(string); print(result) should produce the output: 8

  • Define a function named how_many_substr_of_string(...) which receives two parameters, the first parameters is a list with...

    Define a function named how_many_substr_of_string(...) which receives two parameters, the first parameters is a list with strings (name it  listst) and the second parameter is a single string (name it st). The function should return a number, indicating how many of the strings in the list listst are substrings in the string st As an example, the following code fragment: listst = ["a","bc","dd"] st = "abc" res = how_many_substr_of_string(listst,st) print (res) should produce the output: 2 Language Python

  • PYTHON QUESTION 2. Write a function called first_day_greater that takes two lists, L1 and L2 ,...

    PYTHON QUESTION 2. Write a function called first_day_greater that takes two lists, L1 and L2 , representing the daily measured weights of rat 1 and rat 2, respectively, and returns the index of the first day for which the weight for the fist rat is greater than the weight of the second rat.If there are no such days then the function should return -1. You may NOT assume that L1 and L2 are the same length. Use the following to...

  • Write a function called longest_morse_code_words which takes in a list of strings. The output of longest_morse_code_words...

    Write a function called longest_morse_code_words which takes in a list of strings. The output of longest_morse_code_words should be a list of the strings that were passed in, but ordered by the length of their Morse Code equivalent in descending order. If the length of Morse Code is equal, order the words alphabetically. For convenience, the full table for the 26 letters of the English alphabet is given below: [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] Here's an example: words = ["gin", "zen", "gig", "msg"] longest_morse_code_words(words) #...

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