Skip to main content
 首页 » 编程设计

python之当我认为它是一个列表时,为什么这个对象被认为是整数

2025年05月04日12jirigala

这是一道练习题,内容是:

定义一个过程,该过程接收 1-9 的数字字符串并输出具有以下参数的列表:

  1. 字符串中的每个数字都应该插入到列表中。

  2. 如果字符串中的数字x小于或等于前面的 数字 y,数字 x 应该被插入到一个子列表中。继续 将以下数字添加到子列表中,直到达到一个数字 z 大于数字 y。然后将这个数字 z 添加到 正常列表并继续。

例子:

string = '543987'; result = [5,[4,3],9,[8,7]] 
 
string= '987654321'; result = [9,[8,7,6,5,4,3,2,1]] 
 
string = '455532123266'; result = [4, 5, [5, 5, 3, 2, 1, 2, 3, 2], 6, [6]] 
 
string = '123456789'; result = [1, 2, 3, 4, 5, 6, 7, 8, 9] 

我的代码如下:

def numbers_in_lists(s): 
    n=len(s) 
    p=[] 
    i=0 
    while i<=n-1: 
        p.append(int(s[i])) 
        i=i+1 
 
    r=[p[0]]     
    if p[1]<=p[0]: 
        r.append([p[1]]) 
    else: 
        r.append(p[1]) 
    if n<=2: 
        return r 
    j=2 
    while j<=n-1: 
        if p[j]<=p[j-1]: 
            if p[j-1]<=p[j-2]: 
                r[-1].append(p[j]) 
            else: 
                r.append(p[j]) 
        else: 
            if p[j]<=p[j-2]: 
                r[-1].append(p[j]) 
            else: 
                r.append(p[j]) 
        j=j+1 
    return r 

当我尝试运行 print (numbers_in_lists('543987')) 时,我得到了这个:

Traceback (most recent call last): 
  File "Lesson14Quiz3.py", line 33, in <module> 
    print (numbers_in_lists(string)) 
  File "Lesson14Quiz3.py", line 20, in numbers_in_lists 
    r[-1].append(p[j]) 
AttributeError: 'int' object has no attribute 'append' 

问题似乎出在这一行:r[-1].append(p[j])。但是我觉得当前面的条件都满足的时候,r[-1]不应该是一个整数,而是一个列表。

请您参考如下方法:

这行代码:

r[len(r)-1].append(p[j]) 

分解为:

r: your overall list 
len(r): an integer value based on calculation of the length of r  
-1: also an integer value 

让我们假设 len(r) 是 5。

因此 5 - 1 等于 4。

r[4] 使用 Python 索引来识别列表 r 中的特定元素。具体来说,位置 4 的元素,从索引 0 开始计数。

如果位置 4 的元素是一个数字,如整数(而不是子列表),则 Python 无法向该数字附加任何内容,因为数字没有 .append() 方法。

示例:

假设我们有一个如下所示的列表:

r = [7, 13, 42]  

我们想追加一个 99。

>>> r.append(99) 
[7, 13, 42, 99] 

但是如果我们索引到 r 中的整数之一,而不是 r 中的子列表,我们就会得到错误。

>>> r[1].append(99)     # r[1] is the value 13. 
AttributeError: 'int' object has no attribute 'append' 

最后,如果我们要索引的项目是一个子列表,那么 append() 就可以正常工作。

# presume r now equals: [7, 13, 42, 99, [1]] 
#    and we want to append a 1337 
>>> r[-1].append(1337)     # r[-1] is the last value and is a sublist 
[7, 13, 42, 99, [1, 1337]] 

可能的解决方案:

这可能有助于解决您的问题,并可能更容易对代码进行故障排除。

def numbers_in_lists(s): 
    # use a list comprehension to simplify the process of making a list of 
    #     integers 
    p = [int(x) for x in s] 
 
    # set two sentinel values to help control the code logic/decisions 
    flag = False 
    previous_high = 0 
 
    # create an empty list 
    r = [] 
 
    # iterate over all the digits... 
    for num in p: 
 
        # the code logic is broken into three tests  
        if num <= previous_high and not flag: 
            # if flag is False create a sublist 
            r.append([num]) 
            flag = True 
 
        elif num < previous_high and flag: 
            # if flag is True append to a sublist we previously created 
            r[-1].append(num)  
 
        elif num > previous_high: 
            # Otherwise, append to the main list again    
            r.append(num) 
            previous_high = num 
            flag = False 
 
    return r 

示例输出(使用 OP 提供的字符串):

>>> numbers_in_list('543987') 
[5, [4, 3], 9, [8, 7]] 
 
>>> numbers_in_list('987654321') 
[9, [8, 7, 6, 5, 4, 3, 2, 1]] 
 
>>> numbers_in_list('455532123266') 
[4, 5, [5, 3, 2, 1, 2, 3, 2], 6, [6]] 
 
>>> numbers_in_list('123456789') 
[1, 2, 3, 4, 5, 6, 7, 8, 9]