Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
修改函数以同时接受表(Python 3)_Python_List - Fatal编程技术网

修改函数以同时接受表(Python 3)

修改函数以同时接受表(Python 3),python,list,Python,List,我设计了一个简单的函数,它查看输入的数字列表,确定最小值和最大值,然后用这两个值替换它们之间的中点值,函数如下: 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

我设计了一个简单的函数,它查看输入的数字列表,确定最小值和最大值,然后用这两个值替换它们之间的中点值,函数如下:

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]))
def main函数(编号列表):
最小\u编号=\u编号列表[0]
对于\u编号列表中的\u:
如果是<最小的\u数:
最小_数=a
最大数量=数量列表[0]
对于编号列表中的b:
如果b>最大_数:
最大值=b
中点=(最小值+最大值)/2
最终_列表=[x如果(x!=最大_数和x!=最小_数)在_数列表中x的else中点]
返回最终用户列表
打印(主功能([10,7,14,3,-200,8,1,-12,250]))
不幸的是,我无法让函数处理数字表,有没有简单的方法将数字表转换为列表?欢迎提供任何信息,干杯

您可以使用

输出:

[3, 4, 15, 16, 19, 20]
具有列表理解能力

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

你说的桌子是什么意思?是嵌套列表吗?哦,糟糕,这是一个列表列表,所以是的,这是一个嵌套列表,我的错应该已经澄清了。所以基本上我想把:[[3,4],[15,16],[19,20]]转换成[3,4,15,16,19,20]@programming 55来寻找最小值和最大值你可以使用内置方法
min()
max()
res = [x for lst in a for x in lst]