Skip to main content
 首页 » 编程设计

python之以特定方式将元素附加到列表中

2024年11月01日8yxwkf

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

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

一些输入和输出的例子:

Input: 01365400606 My output: [0, 1, ' ', 3, 6, ' ', 5, 4, ' ', 0, 0, 6, 0, 6] Desired Output: [0, 1, ' ', 3, 6, ' ', 5, 4, 0, 0, ' ', 6, 0, 6]

示例,其中组必须从 0 开始,因为 0 出现的次数超过四次。

Input: 011000000011000100111111101011 Desired output: [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 

请您参考如下方法:

试试这个:

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) 或 Next number (A) 不是Zero 我使用 1 : 像 1Pe1A1。我使用 0 作为反例 0PeA0AA
  • 当 e 为 0 时,我使用 0,如 P0A