首页 > 解决方案 > 以特定方式将元素附加到列表中

问题描述

我有一个任务,我必须得到一个特定的列表。任务是我得到一个字符串的输入,它只包含0-9的数字。现在,我们必须以特定方式附加这些元素。我们需要剪切字符串,使组的长度只有 2 到 4 位。只有在必须时,组才应以 0 开头。每个字符串的长度为 2 到 30 位。

我设法获得了组不以 0 开头的列表,但我的组太大了。这是我遇到麻烦的地方。我如何管理这些只有 2 到 4 位数长的组。

输入和输出的一些示例:

输入: 01365400606 我的输出:[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, 0, " ", 1, 1, 0, " ", 0, 0, " ", 1, 0, 0, 1, " ", 1, 1, 1, 1, " ", 1, 1, 0, " ", 1, 0, 1, 1]

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

  1. 0136 5400 606
  2. 01 36 5400 606

我的代码:

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

标签: pythonlist

解决方案


试试这个:

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
  • previous number (P)orNext number (A)不是时,Zero我使用1: like 1Pe1A1。我用0相反的情况0PeA0AA
  • 我什么时候e00 likeP0A

推荐阅读