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
the key for this value in the dictionary returned is just '0'.
The value 'd' is at index L[2][1] so the key for this value is
'2->1'. The value 'e' is at index L[2][2][0], so the key for this
value is '2->2->0' and so on.
Note: Your code MUST be recursive
>> L = ['a', 'b', ['c', 'd', ['e']]]
>> list_to_dict(L)
{'0': 'a', '1': 'b', '2->0': 'c', '2->1': 'd', '2->2->0': 'e'}
'''
# YOUR CODE HERE
pass
`Hey,
Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries
Note: Brother sometimes while uploading on HomeworkLib the indentations change. So, I request you to verify it with screenshot once.
Please find the python function to print the dictionary with list elements and its index.
Note: Sometimes printing the dictionary will not display the values based on the position it is stored. But it will have all the elements stored in it from the given list and position will be different.
It is one of the property of dictionary.
Program:
L = ['a', 'b', ['c', 'd', ['e']]]
def list_to_dict(L):
"""This function takes list as argument and returns the
dictionary which shows list elements index"""
dict1={}
for i in range(len(L)):
if type(L[i]) != list:
dict1[str(i)]=L[i]
else:
#if the element is not list then call the same function again
to
#populate the dictionary
temp=list_to_dict(L[i])
key=str(i)+"->"
key_temp=key
for k in temp:
dict1[key+str(k)]=temp[k]
#return the dictionary
return dict1
print(list_to_dict(L))
Output:
{'0': 'a', '2->1': 'd', '1': 'b', '2->0': 'c', '2->2->0': 'e'}
Screen Shot:

Kindly revert for any queries
Thanks.
def list_to_dict(lst: list) -> dict: ''' Given a list L that may have sublists nested within...
def count_data(d: dict) -> int: ''' Given d where the keys are [...fill this in...] and values are the number of occurrences of each key, return the total number of occurrences being analyzed (including duplicates). >>> count_data({'Jan': 4, 'Feb': 0, 'Mar': 1}) 5 >>> count_data({'Jan': 0, 'Feb': 0, 'Mar': 25}) 25 ''' pass def most_common_key(d: dict) -> list: ''' Given d representing the number of occurrences of each key within the data set analyzed, give a list of the keys...
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)...
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],...
Python recursive function:
Given an ordered list L. A permutation of L is a rearrangement of its elements in some order. For example (1,3, 2) and (3, 2, 1) are two different permutations of L=(1,2,3). Implement the following function: def permutations (lst, low, high) The function is given a list 1st of integers, and two indices: low and high (lows high), which indicate the range of indices that need to be considered The function should return a list containing all...
Hey guys I need help with this assignment. However it contains 7
sub-problems to solve but I figured only 4 of them can be solved in
one post so I posted the other on another question so please check
them out as well :)
Here is the questions in this assignment:
Note: Two helper functions Some of the testing codes for the functions in this assignment makes use of the print_dict in_key_order (a dict) function which prints dictionary keyvalue pairs...
python 3 inheritance
Define a class named bidict (bidirectional dict) derived from
the dict class; in addition to being a regular dictionary (using
inheritance), it also defines an auxiliary/attribute dictionary
that uses the bidict’s values as keys, associated to a set of the
bidict’s keys (the keys associated with that value). Remember that
multiple keys can associate to the same value, which is why we use
a set: since keys are hashable (hashable = immutable) we can store
them in...
Python 12.10 LAB: Sorting TV Shows (dictionaries and lists)
Write a program that first reads in the name of an input file
and then reads the input file using the file.readlines() method.
The input file contains an unsorted list of number of seasons
followed by the corresponding TV show. Your program should put the
contents of the input file into a dictionary where the number of
seasons are the keys, and a list of TV shows are the values (since...
Hey guys I need help with this question with 3 sub-problems.
f test remove short synonyms () Define the remove shorti synonyms function which is passed a dictionary as a parameter- The keys of the parameter dictionary are words and the corresponding values are 1ists of synonyms (synonyms are words which have the same or nearly the same meaning). The function romoves all the eynonyme which have ous than 8 charactors from each corresponding list of synonyms-As well, the funet...
Hey guys I need help with this question:
# 4444444444444444444444444444444444444444444444444 # get-triples-dict() Define the get triples diet) funetion which is passed a string of text as a The function first converts the parameter string to lower case turns a dictionary with keys which are all the unique consecutive tic characters from the text, and the corresponding values are characters appear in parameter. and then re three alphabe the number of times the three consecutive alphabetic text. Use the isalpha() method...
Updating a Dictionary When working with lists, you probably noticed that you can change the value at a given index with a simple assignment statement. So, for example, the following code replaces the first element of lst with 42: lst[0] = 42 You can update the value for a given key in a dictionary in the same way. The general syntax for this is: d[key] = value This is also how you insert new key/value pairs to a dictionary; if...