Python 以特定方式将元素追加到列表中

Python 以特定方式将元素追加到列表中,python,list,Python,List,我有一个任务,我必须得到一个具体的清单。任务是获取字符串的输入,该字符串只包含0-9中的数字。现在,我们必须以特定的方式附加这些元素。我们需要剪切字符串,这样组的长度只有2到4位数。只有在必须的情况下,组才应以0开头。每个字符串的长度为2到30位 我成功地获得了一个列表,其中组不是以0开头的,但我的组太大了。这就是我遇到麻烦的地方。如何管理只有2到4位数的组 输入和输出的一些示例: 输入:0136540066 我的输出:[0,1',3,6',5,4',0,0,6,0,6] 所需输出:[0,1',

我有一个任务,我必须得到一个具体的清单。任务是获取字符串的输入,该字符串只包含0-9中的数字。现在,我们必须以特定的方式附加这些元素。我们需要剪切字符串,这样组的长度只有2到4位数。只有在必须的情况下,组才应以0开头。每个字符串的长度为2到30位

我成功地获得了一个列表,其中组不是以0开头的,但我的组太大了。这就是我遇到麻烦的地方。如何管理只有2到4位数的组

输入和输出的一些示例:

输入:
0136540066
我的输出:
[0,1',3,6',5,4',0,0,6,0,6]
所需输出:
[0,1',3,6',5,4,0,0',6,0,6]

示例,其中组必须以0开头,因为出现了四次以上的0

输入:
011000000011000100111111101011
所需输出:
[0,1,”,1,0,0,0,”,0,0,0,“,1,1,0,”,0,0,“,1,0,0,1,”,1,1,1,1,1,1,0,”,1,0,1,1]

每个字符串都有更多正确的解决方案,即
0136540066
,可以用更多的方式切割:

  • 0136 5400 606
  • 01365400606
  • 我的代码:

    def NumberCutter(number):
        count = 0
        numberList = []
        numberListSolution = []
    
        for e in number:
            e = int(e)
            numberList.append(e)       
    
        for e in numberList:
            count += 1
            numberListSolution.append(e)
            if count % 2 == 0 and e != 0:
                numberListSolution.append(" ")
    
        return numberListSolution
    
    试试这个:

    def NumberCutter(number):
        count = 0
        # use list comprehensive more readable than for loop
        numberList = [int(e) for e in number]
        numberListSolution = []
    
        def break_group():
            """ add space and return 0 to reset the count."""
            numberListSolution.append(' ')
            return 0
    
        # decision depends on the elements around current element we must know the index
        for i, e in enumerate(numberList):
            count += 1
            numberListSolution.append(e)
    
            if i == len(numberList) - 1:
                continue  # last element no need to add a space after it
    
            if count == 2:  # check for good decision when the count is two
                # if you want to short the group that start with 0 to the minimum uncomment this
                # but as you said priority to group length
                # try:
                #     # 0e1AA  == [0e 1AA] better than [0e1 AA]
                #     if not numberListSolution[-2] and numberList[i+1] and len(numberList) - i >= 2:
                #         numberListSolution.append(" ")
                #         count = 0
                # except IndexError:
                #     pass
                try:
                    # Pe100 -->  Pe 100
                    if numberList[i+1] and not numberList[i+2] and not numberList[i+3] and len(numberList) - (i + 1) == 3:
                        count = break_group()
                        continue
                except IndexError:
                    pass
                try:
                    # Pe101 -->  Pe 101
                    if numberList[i+1] and not numberList[i+2] and numberList[i+3] and len(numberList) - (i + 1) == 3:
                        count = break_group()
                except IndexError:
                    pass
    
            if count == 3:  # check for good decision when the count is three
                # if you want to short the group that start with 0 to the minimum uncomment this
                # but as you said priority to group length
                # try:
                #     # 0e1AA  == [0e 1AA] better than [0e1 AA]
                #     if not numberListSolution[-3] and numberList[i+1] and len(numberList) - i >= 2:
                #         numberListSolution.append(" ")
                #         count = 0
                #         continue
                # except IndexError:
                #     pass
                try:
                    # PPeA1A --> PPeA 1A
                    if numberList[i+2] and (len(numberList) - (i + 1) >= 2):
                        # priority to group length
                        continue
                except IndexError:
                    pass
                try:
                    # PP0111  PPe 111
                    if not e and not numberList[i+1] and not numberList[i+2] and numberList[i+3]:
                        count = break_group()
                        continue
                except IndexError:
                    pass
                try:
                    # PPe1A...  PPE 1A....  at least there is two element after e and the first element is not zero
                    # PPeAA] force PPE AA  there is exactly two element force the break
                    if numberList[i + 1] and (len(numberList) - (i + 1) >= 2) or (len(numberList) - (i + 1) == 2):
                        count = break_group()
                        continue
                except IndexError:
                    pass
    
            # we have 4 element force the group
            if count == 4:
                count = break_group()
                continue
    
        return numberListSolution
    
    print(NumberCutter('011000000011000100111111101011'))
    # [0, 1, 1, 0, ' ', 0, 0, 0, 0, ' ', 0, 0, 1, ' ', 1, 0, 0, 0, ' ', 1, 0, 0, 1, ' ', 1, 1, 1, 1, ' ', 1, 1, 0, ' ', 1, 0, 1, 1]
    print(NumberCutter('01365400606'))
    # [0, 1, 3, 6, ' ', 5, 4, 0, 0, ' ', 6, 0, 6]
    
    注意:在评论中,我用特殊信函解释了特殊情况下做出正确决定的原因:

    • P
      是以前的数字
      >=0
    • e
      是当前元素
    • A
      是e之后的数字
      >=0
    • 上一个数字(P)
      下一个数字(A)
      为否
      零时
      我使用
      1
      :如
      1Pe1A1
      。我用
      0
      来表示相反的情况
      0PeA0AA
    • e
      0
      时,我使用0,如
      P0A

    为什么要测试计数%2==0?我看到的第一件事是检查偶数,而不是检查它是否在2到4之间。这对我来说没有多大意义。我有一个想法,遍历列表中的每个元素,因为每个组必须至少有2位数长,这样我得到2个组,然后我想检查第3个元素,如果是0,转到第4个元素,如果仍然是0,然后将这两个零加在前2位上,因为这将是最大的4位数字组,但我没有做到这一点……请你澄清一下,因为我不明白什么是“正确的输出”。
    01365400606是否同样有效?拆分的逻辑是什么?优先级应该是组的长度,然后是以0开头的组,因为它们的长度必须在2到4位之间,并且允许以0开头,但前提是必须以0开头。谢谢!这一直是我的计划,但我不知道enumerate…实际上我在测试,我对代码做了很多修改以满足您的需求,您可以删除if语句,因为它总是正确的,我没有时间进一步重构代码,if语句在前两个if语句之前,当我移动它们以首先处理它们时,我没有注意到它总是正确的,只是进一步测试它,告诉我它在哪里没有给你想要的输出因为你给了两个相同的输入我不知道我是否正确理解你的要求,但我遵循了规则,但我发现最好是在一个更好的序列之后,立即将一个以0开头的组拆分。是的,相同的输入有更多正确的输出,但你的方法可以实现这一点,我need@RaZiiiGG事实上,我忘记了代码中的一些
    continue
    指令,我对代码进行了重构,使之更加清晰。我认为这个版本的代码比上一个版本要好得多。