编写一个函数,使另一个函数返回List而不是函数

时间:2014-03-12 13:50:59

标签: python

这是以前定义的函数

def make_service(service_data, service_code):
    routes = []

    directions = list(set(map(lambda entry: entry[1], service_data)))  #['1', '2']

    for direction in directions:
        #'1' but if it is '2' then 
        #[('106', '2', '1', '03239'),...('106', '2', '51', '43009')] will be returned for nxt line

        one_direction = filter(lambda entry: entry[1] == direction, service_data)
         #if '1' then [('106', '1', '1', '43009')..('106', '1', '48', '03219')]

        route = map(lambda thing: thing[3], one_direction) #['43009', '43179',..'03218', '03219']

        routes.append(route) #[['43009', '43179',..'03218', '03219']] 

    return lambda t: service_code if t == 0 else (directions if t == 1 else routes)
    # a function is returnrf here ! 


service_106 = make_service(service_106_data, "106")

service_106_data = filter_routes(smrt_routes, "106") 
#[('106', '1', '1', '43009'), ...,('106', '1', '48', '03219'), ('106', '2', '1', '03239'),...('106', '2', '51', '43009')]

所以我想在这里写一个返回

的函数
[['43009', '43179',..'03218', '43009']]

我试过了:

def get_routes(service):
    return map(lambda x: x, service) # I was thinking that 

service_106_routes = get_routes(service_106)
print(service_106_routes)  # I should get  [['43009', '43179',..'03218', '43009']]

如何编写一个从[['43009', '43179',..'03218', '43009']]中提取实际返回函数make_service的{​​{1}}的函数?我不知道如何开始编写我的代码......我是从lambda t开始吗?

1 个答案:

答案 0 :(得分:2)

make_service返回一个带有一个参数的简单lambda函数; t。这个lambda函数又返回:

  • service_codet==0);
  • directionst==1);或
  • routest)的任何其他值。

因此,一旦拥有此功能,您可以通过提供适当的参数来访问这些值中的任何一个,例如

106_service_code = service_106(0)