Python Question 104 Find the length of the missing list PythonTip Question Bank 300 questions

Mondo Education Updated on 2024-02-20

1. Programming questions:

Consider the following nested list: [[1], 1, 2], 4, 5, 5, 1, 1], 5, 6, 7, 8, 9].

In this case, the length of the nested list varies, and we observe that a list of length 3 is missing from a given nested list.

Write a program to find the length of a missing list in a nested list. Let's say there's only one missing list.

Define the function length of missing list() with the parameter list of lists.

In the function, the length of the missing list is returned from the list of lists.

If there are no missing lists, none is returned. Sample input.

Sample output. Explanation: In the test input, the length of the longest list is 4 and the length of the shortest list is 0. There are lists of lengths 2 and 3, but lists of length 1 are missing. So the output is 1.

Note: The input guarantees that there is only one missing list.

2. Implementation:

Editable ** as follows:

#!/usr/bin/python3.9

# -*coding: utf-8 -*

## copyright (c) 2024 , inc. all rights reserved

## @time : 2024/2/9 9:15

# @author : fangel

# @filename : 104.Find the length of the missing list. py

# @software : pycharm

def length_of_missing_list(list_of_lists):

numlist =

Step 1: Loop the list in the list and put the length of the sublist into a list numlist.

for listtmp in list_of_lists:

numlist.append(len(listtmp))

Step 2: Sort against the numlist list.

numlist.sort()

Step 3: Find the length of the missing list in the sorted list (the title requires that only one list is missing).

for i in range(0,len(numlist)-1):

if numlist[i+1] -numlist[i] >1:

return numlist[i]+1

Step 4: If there is no list missing, return none

return none

Get the list of lists from user input

list_of_lists = eval(input())

Call the function.

print(length_of_missing_list(list_of_lists))

3. Analysis:

In this example, you can add the length of the sublist in the list to the listtmp, then sort the listtmp, and finally check whether there are any missing values.

4. Running result:Input:

Output: none Input:

Output:

Related Pages