【问题标题】:why doesnt my program work as it should? (why does it stop in the middle)为什么我的程序不能正常工作? (为什么会停在中间)
【发布时间】:2015-05-14 09:03:17
【问题描述】:

所以我的任务是:

斯洛文尼亚的邮局经常收到一封带有不可读邮政编码的信件,因为人们的书写方式使某些数字难以阅读。

特别频繁更换

  • 3 到 8 之间,
  • MED 1、4、7 和 5
  • 6 之间

inscriptions 子程序Variants (k),它采用四位邮政编码K 在屏幕上列出所有可能的邮政编码,可以用这些交换数字获得。在屏幕上的涂层变体 (1035) 上出现以下邮政编码,不一定与清洁顺序相反:1035、1036、1085、1086、4035、4036、4085、4086、7035、7036、7085、7086th

蟒蛇:

def versions (k):
    def Variante(x):
       a=['3', '8'] #prva tabela
       b=['1', '4', '7'] #druga tabela
       c=['5', '6'] #treta tabela
       v=[] #mozne variante
       x=[x]
       for s in x:
          if s==a[1] or s==a[0]: #ce je stevilka(s) u prvih tabeli
             v.append(a) 
          elif s==b[0] or s==[1] or s==[2]:#ce je stevilka(s) u drugi tabeli
             v.append(b)
          elif s==c[0] or c==[1]:#ce je stevilka(s) u treti tbabeli
             v.append(c)
          else:#ce stevilka(s) ni u nobeni skupini
             v.append(x)

       d=0
       while d<len(v[0]):
          e=0
          print ("ratal")
          while e<len(v[1]):
             f=0
             while f<len(v[2]):
                g=0
                while g<len(v[3]):
                   print(v[0][d], v[1][e], v[2][f], v[3][g])
                   g=g+1
                f=f+1
             e=e+1
          d=d+1

x=input("vnesi postno stevilko: ")
Variante(x)

【问题讨论】:

  • 从使用有意义的变量名开始,而不是 abc 等。这不是 1960 年代的 BASIC,您可以使用完整的单词作为变量。
  • 使用打印或日志语句来了解正在发生的事情。并查看 ipdb 以向您的代码添加断点。然后就可以检查变量,一步步移动等,对调试很有帮助。

标签: python function if-statement for-loop while-loop


【解决方案1】:

只要您将单个字符映射到别名,您就可以将其视为获取别名列表的笛卡尔积

http://repl.it/dgM/7

from itertools import product

def Variante(zip_code):
    replace_tables = [
        ['3', '8'], #prva tabela
        ['1', '4', '7'], #druga tabela
        ['5', '6'], #treta tabela
    ]

    # Convert to list of individual aliases
    aliased_chars = []
    for char in zip_code:
        aliased = False
        for table in replace_tables:
            if char in table:
                # Convert to possible aliases to this char
                aliased_chars.append(table)
                aliased = True
                break

        if not aliased:
            # Otherwise convert to list for itertools.product
            aliased_chars.append([char])

    # Get the cartesian product of lists
    return ["".join(l) for l in product(*aliased_chars)]

# x=input("vnesi postno stevilko: ")
print(Variante("1035"))

【讨论】:

  • 感谢您的帮助...但我能问一下您为什么要制作列表的笛卡尔积吗?以及#cinvert to list of individuačačoases 究竟是如何工作的。我真的很感谢你的帮助! @tommes
  • 嗨,很高兴为您提供帮助 :-) aliased_chars 列表是字符串中每个位置上可能的字符的列表,即:["3", "2"] -&gt; [["3", "8"], ["2"]] 笛卡尔积可以定义为所有可能的组合选择列表,例如:[["1"], ["2", "7"]] -&gt; [["1", "2], ["1", "7"]] 我建议观看有关笛卡尔积的视频:youtube.com/watch?v=l4j4XgVbuxc&t=58
  • 试试这个使用列表推导的版本是否更有指导意义:repl.it/dgM/9
【解决方案2】:

您的问题是您弄乱了索引。因此,例如,您使用s==[1] 代替s==b[1],使用c==[1] 代替s==c[1]

但是,该区域还有其他问题,尽管它们不是错误。您可以执行s in b 之类的操作,而不是单独检查每个值。您可以使用for 直接循环遍历列表的元素,而不是使用while 循环遍历索引。您的代码的更惯用版本是这样的:

for s in x: 
    if s in a:
        v.append(a)
    elif s in b:
        v.append(b)
    elif s in c: 
        v.append(c)
    else:
        v.append(x)

for vd in v[0]:
    print("ratal")
    for ve in v[1]:
        for vf in v[2]:
          for vg in v[3]:
              print(vd, vem vf, vg)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-18
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多