2017-05-25 49 views
0

,我們可以有,如果elif的其他基於呼叫的方法......例如:如果其他蟒蛇函數調用或方法調用

if line2[1] = '1': 
    a(line2) 
elif line2[1] = '2': 
    b(line2) 
elif line2[1] = '3': 
    c(line2) 

和這樣的例子不勝枚舉。 我們可以使用地圖並調用函數。說的線路輸入

實施例:

線= [ '1', '說', '您好']

線= [ '2', '如何', '是']

代碼:

def g(line) 
    my_map = { '1': a(line), 
      '2': b(line), 
      '3': c(line, b), 
      ...... 
      and the list goes on 
      } 

here if line[0] = '1' call a(line) 
     elif line[0] = '2' call b(line) 

如何調用函數基礎上輸入。

請發送例子的代碼,如果可能

由於 勒凱什


線= [ '1', '說', 'HI', '', '', '', '' ,'','',...繼續5-15次] 在上面的例子中,如果我還必須指定其他變量。我該怎麼做。

if line2[1] == '8': 
      p.plast = line2[3] 
      p.pfirst = line2[4] 
      p.pid = line2[9] 

elif line2[1] == 'I': 
      p.slast = line2[3] 
      p.sfirst = line2[4] 
      p.sid = line2[9] 
elif line2[1] == 'Q': 
      p.tlast = line2[3] 
      p.tfirst = line2[4] 
      p.tid = line2[9] 

這是否也有解決方法。

+0

而是加入一個包裝函數'g',你可以有一個函數值字典。如果你需要傳遞一些默認參數,比如'3',你可以使用'lambda'。 –

+0

行被作爲變量發送到這個函數。 g(行)從其他地方被調用。 。你能給我一個例子嗎?我是新來的。 –

回答

5

更新:一個棘手的方法

def make_change(line, p): 

    # Each value of the dict will be another dict in form property:index. 
    # Where property is key of p object to be set/updated, index is index of line with which property should be updated. 

    another_map = { 
     "V": {'vlast': 3, 'vfirst': 5, 'vnone': 7}, 
     "S": {'slast':2, 'sfirst':9, 'snone':4} 
    } 

    val = another_map.get(line[1], None) 
    if val is not None: 
     # iterating over val items to get which property to set and index if line to set 
     for key, item in val.iteritems(): 
      p[key] = line[item] 

    print p 


new_list = ["V", "S", 1,2,3,4,5,6,7,8,9,10,11,12,13] 
make_change(new_list, {}) 

現在改變new_list[1]末看到的輸出


if line2[1] = '1': 
    a(line2) 
elif line2[1] = '2': 
    b(line2) 
elif line2[1] = '3': 
    c(line2) 

威爾完美的作品。

但是你的第二個方法是行不通的,因爲當你寫my_map = { '1': a(line), ... }my_map['1']None或返回函數的值(因爲你調用的函數)

試試這個

def g(line): 
    # Note that value of dict is only the name of the function. 
    my_dict = { 
     '1': a, 
     '2': b 
     ... 
    } 

    # Assuming line[0] is 1,2,3, etc. Check if corresponding function exists in map_dict 
    # Convert line[0] to string before passing it in dict.get() method 
    call_func = my_dict.get(str(line[0]), None) 
    if call_func is not None: 
     call_func(line) 
+0

p是從其他地方調用的對象。 line2 = ['I','PO BOX 28082','VISION'] a_map = {'I':「id」,'S':「sn」,'4':「commercial」,} setattr(p,a_map [line2 [1]],line2 [2])我在執行這段代碼時很有用。但是,如果line2 [0] =='I',我將如何分配:p.id = line2 [1] p.address = line2 [1]。 #不是地圖的一部分p.status = line2 [2]#不是地圖的一部分。正如你看到的,我不能在地圖上分配它,因爲它不是地圖的一部分。如果可能,請用例子來解釋。謝謝 - –

+0

對不起,我沒明白'但是我將如何分配說,如果2號線[0] == '我':p.id = 2號線[1] p.address = 2號線[1]。 #不是地圖的一部分p.status = line2 [2]#不是地圖的一部分。正如你所看到的,我不能在地圖上分配它,因爲它不是地圖的一部分!你可以用更多的細節來編輯你的問題嗎? –

+0

我編輯了主要問題。如果我還不清楚,請告訴我。由於 –

1

你有正確的想法,但你有點錯 - 你應該傳遞一個函數對象,而不是調用它。

def g(line): 
    my_map = {'1': a, '2': b, '3': c ... } 
    cur_func = my_map.get(line) 
    if cur_func is not None: 
     cur_func(line) 
    else: 
     #No mapping found .. 
+0

p是從其他地方調用的對象。 LINE2 = [ 'I', 'PO BOX 28082', 'VISION'] a_map = { 'I': 「empolyer_id」, 'S': 「SN」, '4': 「商業」 , } SETATTR(p,a_map [LINE2 [1]],LINE2 [2]) 我在執行此代碼sucesfull。但是我怎麼分配說 如果2號線[0] ==「我」 –

+0

p是從別的地方調用的對象。 LINE2 = [ 'I', 'PO BOX 28082', 'VISION'] a_map = { 'I': 「ID」, 'S': 「SN」, '4': 「商業」 , } SETATTR(p,a_map [LINE2 [1]],LINE2 [2]) 我在執行此代碼sucesfull。 但我將如何分配說 如果LINE2 [0] == 'I': p.id = LINE2 [1] p.address = LINE2 [1]。 #不是地圖的一部分 p.status = line2 [2]#不是地圖的一部分。正如你看到的,我不能在地圖上分配它,因爲它不是地圖的一部分。如果可能,請用例子來解釋。謝謝 –