【问题标题】:if else python function call or method callif else python函数调用或方法调用
【发布时间】:2017-05-25 21:23:27
【问题描述】:

我们能否有基于 if elif else 的调用方法... 示例:

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

列表还在继续。 我们可以使用地图并调用函数吗?说

线路输入示例:

line = ['1','say','Hi']

line = ['2','How','Are']

代码:

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)

如何根据输入调用函数。

如果可能,请发送示例代码

谢谢 拉克什


line = ['1','say','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]

是否也有解决此问题的方法。

【问题讨论】:

  • 你可以有一个函数值字典,而不是添加一个包装函数g。如果你需要传递一些默认参数,比如3,你可以使用lambda
  • 行作为变量发送到此函数。 g(line) 从其他地方调用。 .你能给我一个例子吗,拜托。我是新手。

标签: python function if-statement methods


【解决方案1】:

更新:一个棘手的方法

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)

【讨论】:

  • p 是从其他地方调用的对象。 line2 = ['I', 'PO BOX 28082','VISION'] a_map = { 'I': "id", 'S': "sn", '4': "commercial", } setattr(p, a_map [line2[1]], line2[2]) 我成功执行了这段代码。但是我将如何分配说 if line2[0] == 'I': p.id = line2[1] p.address = line2[1] 。 # 不属于地图 p.status = line2[2] # 不属于地图。如您所见,我无法在地图中分配它,因为它不是地图的一部分。如果可能,请举例说明。谢谢——
  • 对不起,我没有正确理解But how will I assign say if line2[0] == 'I': p.id = line2[1] p.address = line2[1] . # not part of map p.status = line2[2] # not part of map. As you see i cannot assign it in map as it is not part of map!你能用更多细节编辑你的问题吗?
  • 我编辑了主要问题。如果我也不清楚,请告诉我。谢谢
  • @HyderTom 已更新,如果解决了问题请告诉我
  • 我在输入 iteritems() 时收到此警告。我正在使用 python 3.6 和 Pycharm dict.iterkeys(), dict.iteritems() 和 dict.itervalues() 方法在 py3 中不可用 less... (⌘F1) # 如果您需要您的代码兼容,请启用此检查with a range of Python versions # 代码需要兼容的 Python 版本范围可以在检查设置中指定。 # 类 'dict' 的未解析属性引用 'iteritems' 更少... (⌘F1)
【解决方案2】:

你的想法是对的,但你有点错误——你应该传递一个函数对象,而不是调用它。

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 ..

【讨论】:

  • p 是从其他地方调用的对象。 line2 = ['I', 'PO BOX 28082','VISION'] a_map = { 'I': "empolyer_id", 'S': "sn", '4': "commercial", } setattr(p, a_map [line2[1]], line2[2]) 我成功执行了这段代码。但是如果 line2[0] == 'I' 我将如何分配
  • p 是从其他地方调用的对象。 line2 = ['I', 'PO BOX 28082','VISION'] a_map = { 'I': "id", 'S': "sn", '4': "commercial", } setattr(p, a_map [line2[1]], line2[2]) 我成功执行了这段代码。但是我将如何分配说 if line2[0] == 'I': p.id = line2[1] p.address = line2[1] 。 # 不属于地图 p.status = line2[2] # 不属于地图。如您所见,我无法在地图中分配它,因为它不是地图的一部分。如果可能,请举例说明。谢谢
猜你喜欢
  • 2013-04-20
  • 2017-06-22
  • 1970-01-01
  • 2018-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-21
相关资源
最近更新 更多