猿问

如何使用定义的参数构建一系列位置?

我有一个非常基本的编程背景,但是我得到了一个快速的任务,让一个程序可以获取源和目标位置,并基本上吐出源和目标之间所有位置的范围。


我构建了输入 GUI,并将所有变量传递给了一个函数,现在我只需要弄清楚如何正确地循环遍历它。


位置是这种形式(没有空格): XXXXXXXXXXX 001 X 01 01


X 是根据我传入的值进行硬编码的。最后一个条目应该首先缩放。我还有最后 2 个字段中每个可能的最大值的条目。


示例来源: XXXXXXXXXXX003X0102


示例目的地: XXXXXXXXXXX004X0201


在这种情况下,程序将根据 003 和 004 之间的范围吐出以下内容,最后 2 个位置的最大范围为 03 和 02(将添加空格以便于阅读):


[使用输入和最大值的范围是"XXXXXXXXXXX 003->004 X 01->03 01->02"]


XXXXXXXXXXX 003 X 01 02

XXXXXXXXXXX 003 X 02 01

XXXXXXXXXXX 003 X 02 02

XXXXXXXXXXX 003 X 03 01

XXXXXXXXXXX 003 X 03 02

XXXXXXXXXXX 004 X 01 01

XXXXXXXXXXX 004 X 01 02

XXXXXXXXXXX 004 X 02 01

现在我能够创建一个 GUI 来输入所有这些值并将它们传递给一个函数。


我认为这将最好使用输入范围的 3 个嵌套 for 循环,但我真的很困惑如何在嵌套在循环中时使用前导 0 加上条件最大值来递增。


伪未完成代码->


def addressGenerator(bMax, cMax, X, sourceA, sourceB, sourceC, destinationA, destinationB, destinationC):

    for x in range(sourceA, destinationA):

        for y in range(01, bMax):

            for z in range(01, cMax):

                # content


翻过高山走不出你
浏览 161回答 3
3回答

青春有我

我认为你在正确的轨道上,但你的addressGenerator函数对我来说参数太多。假设您可以将源字符串和目标字符串拆分为像(XXXXXXXXXXX, 003, X, 01, 02). 然后你有一个函数,它只接受源元组和目标元组。所以这些是元组中的索引: 0            1    2  3   4(XXXXXXXXXXX, 003, X, 01, 02)我认为你想要的是在索引之间循环source[1] -> destination[1];source[3] -> destination[3]; 和source[4] -> destination[4]。这大致上是你已经拥有的。然后你可以用填充来格式化不同的东西。def get_addresses(source, destination):    for a in range(int(source[1]), int(destination[1]):        for b in range(int(source[3]), int(destination[3])):            for c in range(int(source[4]), int(destination[4])):                yield '{}{:03d}{}{:02d}{:02d}'.format(source[0], a, source[2], b, c)

收到一只叮咚

我认为这行得通。这三个范围作为元组传递,每个元组都包含所需的开始和结束值(包括)。我还添加了一个separator参数,因为您的问题中显示的输出中似乎有一个无法解释的“X”。更新:修改为根据您下面关于“来源”和“目的地”含义的评论施加限制,并删除了之前的警告。在我看来,代码是不可读的,可能可以重构——但正如我所说,我相信它(现在)可以实现你的目标。对其可用性的一项改进可能是使其解析src和dst字符串参数并自动确定间隔。def addressGenerator(prefix, separator, src, dst, interval1, interval2, interval3):&nbsp; &nbsp; minval, maxval = int(src), int(dst)&nbsp; &nbsp; for x in range(interval1[0], interval1[1]+1):&nbsp; &nbsp; &nbsp; &nbsp; for y in range(interval2[0], interval2[1]+1):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for z in range(interval3[0], interval3[1]+1):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v = x * 10000 + y * 100 + z&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if minval <= v <= maxval:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pieces = prefix, '%03d' % x, separator, '%02d' % y, '%02d' % z&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield ''.join(pieces)# Sample usage.for address in addressGenerator('XXXXXXXXXXX', 'Y',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '0030102', '0040201', (3, 4), (1, 3), (1, 2)):&nbsp; &nbsp; print(address)输出:XXXXXXXXXXX003Y0102XXXXXXXXXXX003Y0201XXXXXXXXXXX003Y0202XXXXXXXXXXX003Y0301XXXXXXXXXXX003Y0302XXXXXXXXXXX004Y0101XXXXXXXXXXX004Y0102XXXXXXXXXXX004Y0201
随时随地看视频慕课网APP

相关分类

Python
我要回答