python - 索引错误 : list indexing error and wrongful tracking of counters

标签 python list

该程序的目标是定义一个过程,该过程接收从 1 到 9 的数字字符串并输出具有以下参数的列表: 字符串中的每个数字都应插入到列表中。

如果字符串中的数字 x 小于或等于前面的数字 y,则应将数字 x 插入到子列表中。继续将以下数字添加到子列表中,直到达到大于数字 y 的数字 z。

然后将这个数字z添加到普通列表中并继续。

#testcases
string = '543987'
numbers_in_lists(string)
result = [5,[4,3],9,[8,7]]
def numbers_in_lists(string):
    # Convert the sequence of strings into an array of numbers
    i = 0
    conv_str_e = []
    while i < len(string):
        conv_str_e.append(int(string[i]))
        i += 1

    #official code starts here
    normal_list = []
    list_of_small_nums = [[]]

    # This will help me keep track of elements of the normal_list.
    previous_e_pos = -1
    e_pos = 0

    # this one will be used to decide if the element should go into the 
    #normal_list or list_of_small_nums
    check_point = 0


    for e in conv_str_e:

        #The first element and every elements bigger the element with 
        #check_point as it's index
        #will be put into the normal_list as long as the list inside 
        #list_of_small_nums is empty
        if e_pos == 0 or e > conv_str_e[check_point]:

            #If the list inside list_of_small_nums is not empty
            if list_of_small_nums[0] != []:

                #concatenate the normal_list and list_of_small_nums
                normal_list = normal_list + list_of_small_nums[0]

                #Clear the list inside list_of_small_nums
                list_of_small_nums[0] = []

            #Add the element in the normal_list
            normal_list.append(e)

            # Update my trackers
            e_pos += 1
            previous_e_pos += 1

            # (not sure this might be the error)
            check_point = e_pos

        #The curent element is not bigger then the element with the 
        #check_point as index position
        #Therefor it goes into the sublist.

        list_of_small_nums[0].append(e)
        e_pos += 1
        previous_e_pos += 1

    return list

最佳答案

你做错的正是你在评论中指出的。您一直在增加 e_pos,因此 check_point 最终大于列表的长度。

我冒昧地更改了一些内容以简化流程。简单的程序可以更容易地找出它们出了什么问题。确保您始终尝试首先考虑最简单的方法来解决您的问题!在这里,我使用 enumerate 替换了对 e_posprevious_e_pos 的需求:

string = '543987'

# Convert the sequence of strings into an array of numbers
conv_str_e = [int(i) for i in string]

#official code starts here
normal_list = []
list_of_small_nums = []

# this one will be used to decide if the element should go into the 
#normal_list or list_of_small_nums
check_point = 0


for ind, e in enumerate(conv_str_e):

    #The first element and every elements bigger the element with 
    #check_point as it's index
    #will be put into the normal_list as long as the list inside 
    #list_of_small_nums is empty
    if ind == 0 or e > conv_str_e[check_point]:

        #If the list inside list_of_small_nums is not empty
        if list_of_small_nums != []:

            #concatenate the normal_list and list_of_small_nums
            normal_list.append(list_of_small_nums)

            #Clear the list inside list_of_small_nums
            list_of_small_nums = []

        #Add the element in the normal_list
        normal_list.append(e)

        # Update my trackers
        check_point = ind

    else:
        #The curent element is not bigger then the element with the 
        #check_point as index position
        #Therefore it goes into the sublist.
        list_of_small_nums.append(e)

# If there is anything left, add it to the list
if list_of_small_nums != []:
    normal_list.append(list_of_small_nums)

print(normal_list)

结果:

[5, [4, 3], 9, [8, 7]]

我相信您可以从这里适本地更改它以将其放回您的函数中。

关于python - 索引错误 : list indexing error and wrongful tracking of counters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56918638/

相关文章:

python - "Tuple comprehensions"和 star splat/unpack 运算符 *

python - 使用python对列表进行排序

python - Scipy 稀疏特征求解器 : MemoryError after multiple passes through loop without anything new being written during loop

javascript - 创建动态选择(下拉)列表的最佳方法?

Python网络浏览器打开带有书签的url,例如www.something.com/file.html#top

python - 如何根据条件对数据框应用乘数?

python - 如何从文件中读取数组并将其分配给变量?

java - 如何在java中生成列表/数组的随机排列?

html - 如何打印列中的列表?

python - 获取排序的索引列表,用于按给定键排序的字典列表