Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have designed the simple function that looks at an inputted list of numbers, identifies a minimum and maximum values, then substitutes both of them for amidpoint value between them, function is here:

def mainfunction(list_of_numbers):

    smallest_number = list_of_numbers[0]

    for a in list_of_numbers:

        if a < smallest_number:

            smallest_number = a

    largest_number = list_of_numbers[0]

    for b in list_of_numbers:

        if b > largest_number:

            largest_number = b

    midpoint = (smallest_number + largest_number)/2

    final_list = [x if (x != largest_number and x != smallest_number) else midpoint for x in list_of_numbers]

    return final_list

print(mainfunction([10, 7, 14, 3, -200, 8, 1, -12, 250]))

Unfortunately, I am not able to get the function to work on TABLES of numbers, is there any way to convert the table of numbers into the list?

1 Answer

0 votes
by (36.8k points)

You can use the itertools.chain

from itertools import chain

a = [[3,4], [15,16], [19,20]]

res = list(chain.from_iterable(a))

print(res)

Output:

[3, 4, 15, 16, 19, 20]

with list comprehension

res = [x for lst in a for x in lst]

 If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch

...