Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List_Set_Transform - Fatal编程技术网

在python中,如何打印将整数列表转换为提供的第二个整数列表的步骤列表?

在python中,如何打印将整数列表转换为提供的第二个整数列表的步骤列表?,python,list,set,transform,Python,List,Set,Transform,鉴于这一投入: [2,2,2,1], [4,3,0,0] 输出: 转换列表的步骤 Move 2 from C_to_A Move 1 from D_to_B 为方便起见,可通过以下方式引用列表: [A, B, C, D] #Just some means to reference the to and from of the move. 到目前为止,我所拥有的: locations = { 0: "A", 1: "B", 2: "C"

鉴于这一投入:

[2,2,2,1], [4,3,0,0]
输出: 转换列表的步骤

Move 2 from C_to_A
Move 1 from D_to_B
为方便起见,可通过以下方式引用列表:

[A, B, C, D] #Just some means to reference the to and from of the move.
到目前为止,我所拥有的:

    locations = {
        0: "A",
        1: "B",
        2: "C",
        3: "D"
    }

def get_steps_for_distribution(c, d):
    print 'current: {} desired: {}'.format(c, d)
    for i in range(0, len(c)):
        diff = c[i] - d[i]        
        while diff > 0:
            for j in range(1, len(c)):
                next_index = (i + j) % len(c)
                importer_diff = c[next_index] - d[next_index]
                if importer_diff < 0:
                    number_to_move = min(diff, (0 - importer_diff))
                    diff -= number_to_move
                    importer_diff += number_to_move
                    c[i] -= number_to_move
                    d[i] += number_to_move
                    print_move_line(number_to_move, i, next_index)
目前,它提供了将所有内容转移到位置A的步骤,因此,这不是所提供的第二个列表的准确表示形式:

>>>Move 2 from C_to_A
>>>Move 0 from C_to_B
>>>Move 1 from D_to_A
>>>Move 0 from D_to_B

我找到了。问题是您没有更新需求(c)和资源(d)的主列表。在代码的底部,添加几行以实现这些更新:

               diff -= number_to_move
               importer_diff += number_to_move
               c[i] -= number_to_move
               d[i] += number_to_move
这会逐渐更新资源列表,直到它与需求列表相等


注意:一旦你开始工作,有很多事情你可以改进:

  • 变量名:使它们更具描述性,并在“导出”和“导入”之间以及在每个组内很好地对应
  • 使用模数简化下一个索引的计算:

    下一个指数=(i+j)%len(c)

  • 您不需要更新导入程序_diff;无论如何,在下一个循环中重新计算它


我的解决方案

这将产生所需的输出:

locations = {
    0: "A",
    1: "B",
    2: "C",
    3: "D"
}

def print_move_line(number_to_move, exporter, importer):
    line = "Move " + str(number_to_move) + " from " + locations.get(exporter, "bad location") + "_to_" + locations.get(importer, "bad location")
    print line


def get_steps(have, want):
    for exporter_index in range(0, len(have)):
        diff = have[exporter_index] - want[exporter_index]

        # Move 'diff' units from bin 'exporter_index' to others;
        #   offer valid only while supplies last.
        while diff > 0:
            for j in range(1, len(have)):
                # Start next to the exporter and wrap around.
                importer_index = (exporter_index + j) % len(have)
                print("  DEBUG: have", have, "want", want, "\t", exporter_index, "to", importer_index)
                importer_diff = have[importer_index] - want[importer_index]

                # If this bin needs units, move what we have.
                if importer_diff < 0:
                    number_to_move = min(diff, (-importer_diff))
                    print("  DEBUG: bin", importer_index, "needs", importer_diff, "donor has", diff)
                    diff -= number_to_move
                    have[exporter_index] -= number_to_move
                    importer_diff -= number_to_move
                    have[importer_index] += number_to_move
                    print_move_line(number_to_move, exporter_index, importer_index)

get_steps([2, 2, 2, 1], [4, 3, 0, 0])

你说的打印步骤是什么意思?您可以执行
打印(“我的步骤1,数据1=“+str(数据1))
是,只需打印步骤就足够了。这将是一套指令,如“将1从a移到B,将2从D移到C”等。你上一个问题的答案是否有用?所以,你必须详细地写下来。示例:
print(“Move 1 from A to B”)
,在执行
B=A
@的指令之前。用户:Kenny在实现我在上一个问题中概述的算法方面做了很好的尝试。他在一个细节上有缺陷,正在寻求帮助。在他给出的例子中,“将1从D移到A”这一步应该是将该单元移到B,而不是A。我做了你建议的更改,并将它们合并到问题中,直到我理解你的回答为止。但奇怪的是,它仍然在错误地分发它们。它在A中添加了一个额外的项,总共是[5,3,0,0];(2个从C到A,1个从D到A)。这是关于以下问题集的参考:当前[2,2,2,1]所需的[4,3,0,0]。谢谢!我喜欢调试分解,这真的很有帮助。希望这对将来的人有所帮助。此外,评论幽默+1.:-)
locations = {
    0: "A",
    1: "B",
    2: "C",
    3: "D"
}

def print_move_line(number_to_move, exporter, importer):
    line = "Move " + str(number_to_move) + " from " + locations.get(exporter, "bad location") + "_to_" + locations.get(importer, "bad location")
    print line


def get_steps(have, want):
    for exporter_index in range(0, len(have)):
        diff = have[exporter_index] - want[exporter_index]

        # Move 'diff' units from bin 'exporter_index' to others;
        #   offer valid only while supplies last.
        while diff > 0:
            for j in range(1, len(have)):
                # Start next to the exporter and wrap around.
                importer_index = (exporter_index + j) % len(have)
                print("  DEBUG: have", have, "want", want, "\t", exporter_index, "to", importer_index)
                importer_diff = have[importer_index] - want[importer_index]

                # If this bin needs units, move what we have.
                if importer_diff < 0:
                    number_to_move = min(diff, (-importer_diff))
                    print("  DEBUG: bin", importer_index, "needs", importer_diff, "donor has", diff)
                    diff -= number_to_move
                    have[exporter_index] -= number_to_move
                    importer_diff -= number_to_move
                    have[importer_index] += number_to_move
                    print_move_line(number_to_move, exporter_index, importer_index)

get_steps([2, 2, 2, 1], [4, 3, 0, 0])
('  DEBUG: have', [2, 2, 2, 1], 'want', [4, 3, 0, 0], '\t', 2, 'to', 3)
('  DEBUG: have', [2, 2, 2, 1], 'want', [4, 3, 0, 0], '\t', 2, 'to', 0)
('  DEBUG: bin', 0, 'needs', -2, 'donor has', 2)
Move 2 from C_to_A
('  DEBUG: have', [2, 2, 4, 1], 'want', [2, 3, 0, 0], '\t', 2, 'to', 1)
('  DEBUG: bin', 1, 'needs', -1, 'donor has', 0)
Move 0 from C_to_B
('  DEBUG: have', [2, 2, 4, 1], 'want', [2, 3, 0, 0], '\t', 3, 'to', 0)
('  DEBUG: have', [2, 2, 4, 1], 'want', [2, 3, 0, 0], '\t', 3, 'to', 1)
('  DEBUG: bin', 1, 'needs', -1, 'donor has', 1)
Move 1 from D_to_B
('  DEBUG: have', [2, 2, 4, 2], 'want', [2, 2, 0, 0], '\t', 3, 'to', 2)
[wdwickar@wdwickar-ws pyside]$ python so.py
('  DEBUG: have', [2, 2, 2, 1], 'want', [4, 3, 0, 0], '\t', 2, 'to', 3)
('  DEBUG: have', [2, 2, 2, 1], 'want', [4, 3, 0, 0], '\t', 2, 'to', 0)
('  DEBUG: bin', 0, 'needs', -2, 'donor has', 2)
Move 2 from C_to_A
('  DEBUG: have', [4, 2, 0, 1], 'want', [4, 3, 0, 0], '\t', 2, 'to', 1)
('  DEBUG: bin', 1, 'needs', -1, 'donor has', 0)
Move 0 from C_to_B
('  DEBUG: have', [4, 2, 0, 1], 'want', [4, 3, 0, 0], '\t', 3, 'to', 0)
('  DEBUG: have', [4, 2, 0, 1], 'want', [4, 3, 0, 0], '\t', 3, 'to', 1)
('  DEBUG: bin', 1, 'needs', -1, 'donor has', 1)
Move 1 from D_to_B
('  DEBUG: have', [4, 3, 0, 0], 'want', [4, 3, 0, 0], '\t', 3, 'to', 2)