Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.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_Function_Dictionary_Pairing - Fatal编程技术网

Python 函数通过字典中的值传递

Python 函数通过字典中的值传递,python,function,dictionary,pairing,Python,Function,Dictionary,Pairing,我正在尝试编写一个程序,检查字典中的操作,并调用与“key”匹配的函数 该程序将根据“键”是什么来解析该行 我的问题是,如何将字符串传递到字典调用的函数中,以便函数能够解析它,因为字符串不总是相同的,字符串的解析方式也不相同 def format1(opline): # r-type: rd, rs, rt opline = opline.split('', 1)[1] # re

我正在尝试编写一个程序,检查字典中的操作,并调用与“key”匹配的函数

该程序将根据“键”是什么来解析该行

我的问题是,如何将字符串传递到字典调用的函数中,以便函数能够解析它,因为字符串不总是相同的,字符串的解析方式也不相同

def format1(opline):                                    # r-type: rd, rs, rt
    opline = opline.split('', 1)[1]                     # removes instruction
    rd, rs, rt = opline.split()                         # splits into 3 registers
    return rd, rs, rt                                   # returns all registers

inst_match = {'add' : format1}                          # dictionary (more.       
                                                        # key/funct pairs
                                                        # will be added

instruction_storage = 'add 9 0 255'

instructions = instruction_storage.split()[0]       # get key from string

inst_match[instructions](instruction_storage)       # passes key to search and string to pass into function

^上面的代码只是一个测试。我最终会从一个包含多行的文件中读取“指令存储”。

我不太清楚您到底想要什么,但这里有一个猜测:

def format1(opline, string):
    print('format1({!r}, {!r})'.format(opline, string))
    opline = opline.split(' ', 1)[1]
    rd, rs, rt = opline.split()
    return rd, rs, rt

def format2(opline, string):
    print('format2({!r}, {!r})'.format(opline, string))
    opline = opline.split(' ', 1)[1]
    rd, rs, rt = opline.split()
    return rd, rs, rt

inst_match = {'add': format1,
              'sub': format2}

instruction_storage = [('add 9 0 255', 'string1'),
                       ('sub 10 1 127', 'string2')]

for instruction in instruction_storage:
    opline, string = instruction
    opcode = opline.split()[0]

    try:
        inst_match[opcode](opline, string)
    except KeyError:
        print('unknown instruction: {!r}'.format(opcode))
输出:

format1('add90255','string1')
格式2('sub 10 1 127','string2')

我不太清楚你到底想要什么,但这里有一个猜测:

def format1(opline, string):
    print('format1({!r}, {!r})'.format(opline, string))
    opline = opline.split(' ', 1)[1]
    rd, rs, rt = opline.split()
    return rd, rs, rt

def format2(opline, string):
    print('format2({!r}, {!r})'.format(opline, string))
    opline = opline.split(' ', 1)[1]
    rd, rs, rt = opline.split()
    return rd, rs, rt

inst_match = {'add': format1,
              'sub': format2}

instruction_storage = [('add 9 0 255', 'string1'),
                       ('sub 10 1 127', 'string2')]

for instruction in instruction_storage:
    opline, string = instruction
    opcode = opline.split()[0]

    try:
        inst_match[opcode](opline, string)
    except KeyError:
        print('unknown instruction: {!r}'.format(opcode))
输出:

format1('add90255','string1')
格式2('sub 10 1 127','string2')

也许只有我一个人,但我不明白这个问题。你能说清楚点吗?也许只是我,但我不明白这个问题。请你说清楚一点好吗?