Question

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'] [1, 3, 1]

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def fav_colours(colors):
    unique_words = []
    counts = []
    for color in colors:
        if color not in unique_words:
            unique_words.append(color)
    for color in unique_words:
        count = 0
        for c in colors:
            if c == color:
                count += 1
        counts.append(count)
    return [unique_words, counts]


colours = ['blue', 'red', 'red', 'red', 'green']
colours, numbers = fav_colours(colours)
print(colours)
print(numbers)

Add a comment
Know the answer?
Add Answer to:
PYTHON define a function called fav_colours(): which receives a list of strings of lowercasecolours: the 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
  • 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]]

  • 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']

  • 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 add_nums_x_pos_given_list_nums (..) which receives a list that is guaranteed to only have numbers...

    Define a function add_nums_x_pos_given_list_nums (..) which receives a list that is guaranteed to only have numbers (or the empty list) and returns the sum of each number multiplied by the position where the number is. (In the case of the empty list the function it will return 0) For example add_nums_x_pos_given_list_nums ([10,20,30]) will return the value 80 which is the result of adding: 0 * 10 (10 is in position 0 in the list) + 1 * 20 + 2...

  • 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 funD2(...) which receives a list lst that contains single letters, single digits and...

    Define a function funD2(...) which receives a list lst that contains single letters, single digits and single special characters (and the list contains at least one element of each type). The function returns a string that contains the last letter and the last special character of the list, where each is repeated as many times as the sum of all digits in the list. As an example, the following code fragment: lst = ["a","b","c", 1, 2, "$","%"] print (funD2(lst)) should...

  • please use basic programming language and explain with comment lines .for Thonny(python) format. Duplicate elimination Create...

    please use basic programming language and explain with comment lines .for Thonny(python) format. Duplicate elimination Create a function named unique that receives a list and returns a (possibly shorter) list containing only the unique values in sorted order. Test your function with the list of numbers and list of strings that given in the sample output. Sample Output Original list of numbers [11, 11, 2, 2, 7, 7, 5, 5, 3, 3] New list after calling unique function [2, 3,...

  • Python - Write a function called create_basic_pattern(background_colour, size)

    Write a function called create_basic_pattern(background_colour, size) which takes a background colour code and an integer as parameters and returns a list of strings. The list represents the pattern of a pixel art pattern. For example, consider the following code fragment:pattern \(=\) create_basicThen the function should create a list of strings. The size of the list is 8 . Each element is a string with 8 'y' characters:

  • There exists a Python variable (a list of lists) called score_card which stores data representing...

    PYTHON 3.7 EXERCISES part 2 Question 1 Question 2 There exists a Python variable (a list of lists) called score_card which stores data representing player results from a golf competition. Each sublist is a triple (i.e., a list of size three) which stores data for one player as follows (in this order): A string representing the player's initials An integer representing the player's gross score An integer representing the player's golfing handicap. A golf player's net score is calculated by...

  • D.1 [3] Define a function called funD1...) that receives a string and returns a new string...

    D.1 [3] Define a function called funD1...) that receives a string and returns a new string with all the characters in the original string in an EVEN position. As an example, the following code fragment: print (funD1('abcde')) should produce the output: ace D.2 [6] Define a function funD2(...) which receives a list ist that contains single letters, single digits and single special characters and the list contains at least one element of each type). The function returns a string that...

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