2014-03-12 38 views
1

這是預先定義的函數編寫一個函數,使得另一個函數返回一個列表,而不是功能

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']]功能實際上返回一個函數lambda t?我真的不知道如何開始寫我的代碼,但是...我是從t開始的嗎?

回答

2

make_service返回帶有一個參數的簡單lambda函數; t。反過來這拉姆達函數返回:

  • service_codet==0);
  • directionst==1);或
  • routes(任何其他值t)。

因此,一旦你有此功能,可以通過提供適當的參數訪問任何這些值,例如

106_service_code = service_106(0) 
+0

謝謝!我知道了 !它應該是任何不是0或1的值,它返回我想要的列表! – user3398505

相關問題