为什么我的程序不能正常工作?(为什么停在中间)

2024-09-29 19:36:57 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我的任务是:

Post of Slovenia often gets a letter with unreadable ZIP code because people write such a way that some of the figures difficult to read.

Especially frequently make replacement

  • Between 3 to 8 ,
  • MED 1, 4 , 7, and 5
  • Between 6

inscriptions subroutine Variants (k), which adopts a four-digit postcode K on the screen lists all the possible postcode can be obtained with these swaps digits. On the coating Variants (1035) on the screen appears the following zip codes, not necessarily against the order clean: 1035 , 1036 , 1085 , 1086 , 4035 , 4036 , 4085 , 4086 , 7035, 7036 , 7085 , 7086th

Python:

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)

Tags: orofthetolenwithbetweence
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)

只要将单个字符映射到别名,就可以认为这是获得别名列表的笛卡尔积

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"))

相关问题 更多 >

    热门问题