Question

In python, I have a list L that contains other lists. The first entry of all...

In python, I have a list L that contains other lists. The first entry of all the lists in L is either a, b or c. How do I partition L and create three new lists of lists, A, B, and C, where A contains all the lists with the first entry a and similar for B and C?
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE

def partitionList(L):

A = []

B = []

C = []

for list in L:

if list[0] == 'a':

A.append(list)

elif list[0] == 'b':

B.append(list)

elif list[0] == 'c':

C.append(list)

return A, B, C

L = [['a', 'hello', 'world'], ['b', 'good', 'morning'], ['c', 'good', 'day']]

A, B, C = partitionList(L)

print(A)

print(B)

print(C)

Add a comment
Know the answer?
Add Answer to:
In python, I have a list L that contains other lists. The first entry of all...
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
  • Lists in python have 2 methods: s.pop([i]) where i is the position in the list to...

    Lists in python have 2 methods: s.pop([i]) where i is the position in the list to be “popped”, and s.remove(x) that are somewhat similar. (Check the notes for a description of what they do.) (10 points total) What are 2 important differences between these 2 functions? (2 points) We looked at a program that implemented the Sieve of Eratosthenes using a list. The program below implements the same algorithm, but uses pop() and remove to do it. The basic idea...

  • 1)Given two lists L1 and L2, create a new list L3 consisting of the first element...

    1)Given two lists L1 and L2, create a new list L3 consisting of the first element of L1 and the last element of L2. For example, if L1=[1,2,3] and L2=["a","b","c"], L3 should be [1,"c"] 2)given a string s, create a new string odd_letters that contains only the odd letters of s (two ways to solve this problem are to use slicing, or a while or for loop), i.e. starting at the first letter or the zeroth index: if s="banana", odd_letters...

  • Python help: Create two lists of 5 elements. The first list should be a series of...

    Python help: Create two lists of 5 elements. The first list should be a series of questions whose answer is either "T" or "F" (true or false). The second list should be the correct answers to those questions ("T" or "F"). Write a program that asks the user each question in the question list and compares their answer to the answer in the answer list. Print out whether or not they got the question correct and print out their final...

  • [Python] I have the following function that removes all non-numerical characters from a string: d...

    [Python] I have the following function that removes all non-numerical characters from a string: def remove_non_numeric(s):    seq_type= type(s) return seq_type().join(filter(seq_type.isdigit, s)) And I need help implementing it into this function: def list_only_numbers( a_list ) : # Create a new empty list. # Using a loop (or a list comprehension) # 1) call remove_non_numeric with a list element # 2) if the return value is not the empty string, convert # the string to either int or float (if it contains...

  • How do you insert an array of linked lists(L) as an argument of a function (in...

    How do you insert an array of linked lists(L) as an argument of a function (in pseudocode)? For example I already have an array of linked lists (L) where each element of this array is the head/ or start of a new linked list. I then want to traverse each list in the algorithm e.g. FindMean(how to represent the list here?) { here I want to visit the first node of each list in turn, then all the second nodes...

  • Python help: Given the lists, lst1 and lst2, create a new sorted list consisting of all...

    Python help: Given the lists, lst1 and lst2, create a new sorted list consisting of all the elements of lst1 that also appears in lst2. For example, if lst1 is [4, 3, 2, 6, 2] and lst2 is [1, 2, 4], then the new list would be [2, 2, 4]. Note that duplicate elements in lst1 that appear in lst2 are also duplicated in the new list. Associate the new list with the variable new_list, and don't forget to sort...

  • use python 2a. Union (1 points) Make a union function which takes in 2 lists as...

    use python 2a. Union (1 points) Make a union function which takes in 2 lists as parameters and returns a list. It should return a list that contain all elements that are in either list. Your results list should contain no duplicates. For example, if the two lists are [1,5,6,5, 2] and [3,5, 1,9] then the result list is (1,5,6,2,3,9). Note that the list sizes don't have to be equal. Note that the input lists could have duplicates, but your...

  • You have the following list of lists: hr = [ [ 72, 75, 71, 73], #...

    You have the following list of lists: hr = [ [ 72, 75, 71, 73], # resting [ 91, 90, 94, 93], # walking slowly [ 130, 135, 139, 142], # running on treadmill [ 120, 118, 110, 105, 100, 98] # after a minute recovery ] Each element is the heart rate of a patient. Each sub-list contains heart rates of patients after resting, walking, running, a minute after recovery. Not all sub-lists are of the same length. Write...

  • Create a Python list (use first three letters of your name followed by the letter L...

    Create a Python list (use first three letters of your name followed by the letter L as the name of the list.) with the following data: 10, 20.0, 50.00, 7000, 7500, 7700, 7800, 8000, 9000, ‘python’. Display the entire list with an appropriate message. Display the 4th element in the list with an appropriate message. Display the first 3 values of the list only. Display all the values starting from the sixth element to the end of the list. Change...

  • I have three possible trajectories. a-d a-b-d c-d How do i write a loop in python...

    I have three possible trajectories. a-d a-b-d c-d How do i write a loop in python using dictionaries and lists and that ends printing all the possible combinations.

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