Question

Define the get_list_of_lists() function which is passed a list of strings as a parameter. An example...

Define the get_list_of_lists() function which is passed a list of strings as a parameter. An example parameter list is:

["4 3 1 12 2 12 3 12 4 3", "4 3 1 12 2 12 3 12 4 3", "4 3 1 12 2 6"]

The function returns a list of lists of tuples. For each element of the parameter list, e.g.,

                       "4 3 1 12 2 12" 

the function creates a list of tuples, e.g.,

                        [("4", 3), ("1", 12), ("2", 12)]

where every tuple in the list of tuples is created from each two consecutive numbers from the parameter string of numbers. Each tuple of the list of tuples comprises of a string followed by an integer. For example, executing the following code with the completed function:

 

list_of_strings = ["4 3 1 12 2 12 3 12 4 3", "4 3 1 12 2 12 3 12 4 3", "4 3 1 12 2 6"] list_of_tuple_lists = get_list_of_lists(list_of_strings) for a_list in list_of_tuple_lists: print(a_list)

prints:

[('4', 3), ('1', 12), ('2', 12), ('3', 12), ('4', 3)]
[('4', 3), ('1', 12), ('2', 12), ('3', 12), ('4', 3)]
[('4', 3), ('1', 12), ('2', 6)]
0 0
Add a comment Improve this question Transcribed image text
Answer #1
def get_list_of_lists(strings):
    result = []
    for s in strings:
        items = s.split()
        lst = []
        for i in range(0, len(items), 2):
            lst.append((items[i], items[i + 1]))
        result.append(lst)
    return result


list_of_strings = ["4 3 1 12 2 12 3 12 4 3", "4 3 1 12 2 12 3 12 4 3", "4 3 1 12 2 6"]
list_of_tuple_lists = get_list_of_lists(list_of_strings)
for a_list in list_of_tuple_lists:
    print(a_list)

[(4, 3), (1, 12), (2, 12), (3, 12 ), (4, 3)] [(4, 3), (1, 12), (2, 12), (3, 12 ), (4, 3

Add a comment
Know the answer?
Add Answer to:
Define the get_list_of_lists() function which is passed a list of strings as a parameter. An example...
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
  • Complete the get_mid_letter() function which is passed a list of strings as a parameter. The function...

    Complete the get_mid_letter() function which is passed a list of strings as a parameter. The function returns a string made up of the concatenation of the middle letter of each word from the parameter list. The string returned by the function should be in lowercase characters. If the parameter list is an empty list, the function should return an empty string For example Test Result print("1.", get mid_letter"Jess", "Cain", Amity", "Raeann"])) 1. siia Answer (penalty regime: 0 %) 1 -Idef...

  • Basic Python code needed Define the get_lines_from_file) function which is passed a filename as a parameter....

    Basic Python code needed Define the get_lines_from_file) function which is passed a filename as a parameter. The function reads the information from the file (corresponding to the filename parameter) and returns a list of strings where each element of the returned list corresponds to one line of the file. The list of strings which is returned by the function should not contain any newline characters. For example, if the file contains the text: 10 440 240 4 42 4 42...

  • Define the is_a_valid_date() function which is passed a string as a parameter. The function returns a...

    Define the is_a_valid_date() function which is passed a string as a parameter. The function returns a boolean indicating whether the parameter string is a valid date or not. The first two lines of the function are: month_names = ["January", "February", "March","April", "May", "June", "July", "August", "September", days_in_month = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) where month_names is a list of valid month names, and days_in_month is a list which contains the maximum day number...

  • Define the is_a_valid_date() function which is passed a string as a parameter. The function returns a...

    Define the is_a_valid_date() function which is passed a string as a parameter. The function returns a boolean indicating whether the parameter string is a valid date or not. The first two lines of the function are: month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] where month_names is a list of valid month names, and days_in_month is a list which contains the maximum day...

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

  • Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for...

    Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for the dictionary are strings and the values are lists of numbers. The function should create a new dictionary where the keys are the same strings as the original dictionary and the values are tuples. The first entry in each tuple should be the weighted average of the non-negative values in the list (where the weight was the number in the 0 position of the...

  • Define a function called get_n_largest(numbers, n) which takes a list of integers and a value n...

    Define a function called get_n_largest(numbers, n) which takes a list of integers and a value n as parameters and returns a NEW list which contains the n largest values in the parameter list. The values in the returned list should be in increasing order. The returned list must always be of length n. If the number of values in the original list is less than n, the value None should be repeated at the end of the returned list to...

  • Write a function valid_integers(strings) that takes a list of strings as a parameter and returns a...

    Write a function valid_integers(strings) that takes a list of strings as a parameter and returns a list of the integers that can be obtained by converting the strings to integers using an expression like int(string). Strings which cannot be converted in this way are ignored. Hint: the statement pass is a statement that does nothing when executed. For example: Test Result strings = ['123', '-39', '+45', 'x', 'COSC121', '123+', '12-3'] print(valid_integers(strings)) [123, -39, 45]

  • Write a python function alt which takes a list of strings myList as a parameter. alt...

    Write a python function alt which takes a list of strings myList as a parameter. alt then returns two strings s1 and s2 as a tuple (s1, s2). Here s1 is the concatenation of the strings in myList in even index positions, and s2 is the concatenation of the strings in myList in odd index positions. (List starts at index 0) For example, if myList = [‘My’, ‘kingdom’, ‘for’, ‘a’ , ‘horse’], then s1 = ‘Myforhorse’ and s2 = ‘kingdoma’.

  • For this task you're going to write a function that joins strings from a list. The function calle...

    I need help with this python programming assignment please double check the indentations For this task you're going to write a function that joins strings from a list. The function called join ) has one formal parameter and one optional parameter. Recall that an optional parameter is like end or sep in the print( function. You don't have to use it because there's a default value. For the print () function, e.g., \'n' (newline) is the default value for end....

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