Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.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 将功能列表应用于可编辑列表_Python - Fatal编程技术网

Python 将功能列表应用于可编辑列表

Python 将功能列表应用于可编辑列表,python,Python,我需要编写一个函数,它接受两个列表作为输入(一个iterables列表,一个函数“len”、“sum”和“type”的子集列表) 函数应该返回与第一个列表长度相同的列表,其中该列表中的每个元素都是一个元组,该元组应用了第二个列表中的函数 这是我当前的代码,但它只返回obj和funcs的第一项的正确答案: def apply (list_items, list_funcs): list1 = () for n in list_items: tup = ()

我需要编写一个函数,它接受两个列表作为输入(一个iterables列表,一个函数“len”、“sum”和“type”的子集列表)

函数应该返回与第一个列表长度相同的列表,其中该列表中的每个元素都是一个元组,该元组应用了第二个列表中的函数


这是我当前的代码,但它只返回obj和funcs的第一项的正确答案:

def apply (list_items, list_funcs):
    list1 = ()
    for n in list_items:
        tup = ()
        for f in list_funcs:
            tup += (f(n),)
        return tup
        list1 += tup

    return list1

您可以简单地在每个项上循环并将函数应用于该项

def apply(items, funcs):

    output = []
    #Loop over the items
    for item in items:
        #Apply function over each item and add it to result
        output.append(tuple(func(item) for func in funcs))

    return output

print(apply([(1,2),[3,4]], [len, sum, type]))
print(apply([(1,2),[1,3,4,5,6,7],[0]], [len,sum]))
输出将是

[(2, 3, <class 'tuple'>), (2, 7, <class 'list'>)]
[(2, 3), (6, 26), (1, 0)]

您可以简单地在每个项上循环并将函数应用于该项

def apply(items, funcs):

    output = []
    #Loop over the items
    for item in items:
        #Apply function over each item and add it to result
        output.append(tuple(func(item) for func in funcs))

    return output

print(apply([(1,2),[3,4]], [len, sum, type]))
print(apply([(1,2),[1,3,4,5,6,7],[0]], [len,sum]))
输出将是

[(2, 3, <class 'tuple'>), (2, 7, <class 'list'>)]
[(2, 3), (6, 26), (1, 0)]
输出:

[(2, 3), (6, 26), (1, 0)]
输出:

[(2, 3), (6, 26), (1, 0)]
请尝试以下代码:

x=[(1,2),[3,4]]
f=[len,sum,type]
z=[map(y,x) for y in f]
result=[tuple(z[j][i] for j in range(len(z))) for i in range(len(z[0]))]
请尝试以下代码:

x=[(1,2),[3,4]]
f=[len,sum,type]
z=[map(y,x) for y in f]
result=[tuple(z[j][i] for j in range(len(z))) for i in range(len(z[0]))]