首页 > 解决方案 > 我需要什么语法才能正确遍历此列表?

问题描述

遍历一个列表,尝试在 if 语句中使用该列表,但收到有关语法以及列表如何必须是整数或切片而不是元组的错误消息。试图了解出了什么问题。

我正在处理来自 Hackerrank 的挑战,我即将完成,但我有点卡住了。本质上,我有一个名为“页面”的列表,指的是工作簿中的页面,如此处链接的挑战描述中指​​定的那样:

https://www.hackerrank.com/challenges/lisa-workbook/problem

pages 是一个列表,其中每个元素代表工作簿中的 1 页,这些元素中的元素代表该页面上问题的编号(即:第 1 页有第 1 章的问题 1、2 和 3,而第 2 页有问题 4为那一章)。挑战要求我们计算工作簿中问题编号与找到它们的页码相匹配的问题总数。

我的第一直觉是遍历页面,然后遍历该页面上的问题,并在页面上问题的迭代变量与该页面的迭代变量匹配时添加到计数器 special_probs。这一切都在底部的 4 行代码中完成。但是,在嵌套的 for 循环中调用我们所在的当前页面给我带来了一些问题。这可能是超级简单或愚蠢的事情,但我希望您能帮助我理解为什么我不能按照我的方式去做,以及我需要做些什么不同的事情才能让它按预期工作。如果您需要更多信息或上下文,请告诉我。谢谢!

(在这个特定的例子中,我还对我的代码进行了注释。如果它让人分心,我可以把它们删掉。)


n = 4 #total number of chapters (there are 5, but index "z" starts @ 0)
k = 3 #maximum number of problems allowed per page
arr = [4, 2, 6, 1, 10] #example array listing the # of problems per chapter
pages = [0] #total number of pages in workbook (added 0 so pages start on 1)
z = 0 #chapter index counter
prob_increment = 0 #helps properly number multi-page chapters
special_probs = 0 #counter for all special problems 

while z <= n: #indexing through chapters, filling 1 at a time with problems
    pages_in_chapter = -(-arr[z]//k) #no more than k problems per page 
    if arr[z] <= k: #if all problems in the chapter fit on 1 page...
        new_page_proto = list(range(arr[z])) 
        new_page = [y+1 for y in new_page_proto] 
        pages.append(new_page) #adds completed page to the workbook's pages
    else: #for chapters with more problems than k
        chapter_probs_count = arr[z] 
        while chapter_probs_count > k: #fill pages until we have =<k left 
            new_page = list(range(k)) #create new page, add k problems
            new_page = [x+prob_increment*3 for x in new_page] #pages <1 in ch
            new_page = [y+1 for y in new_page] #fix offset again
            pages.append(new_page) #adds completed page to workbook's pages
            prob_increment = prob_increment + 1 #increase to denote new page
            chapter_probs_count = chapter_probs_count - k 
        new_page = list(range(chapter_probs_count)) #adds remaining probs <k
        new_page = [x+prob_increment*3 for x in new_page] 
        new_page = [y+1 for y in new_page] #fix offset again
        pages.append(new_page) #add the final page of the chapter to pages
    z = z + 1 #increment z & repeat the page-adding process for n chapters
    prob_increment = 0; #reset the incrementer when starting new chapter  

for y in enumerate(pages): #search for special problems 1 page at a time
    for x in enumerate(pages(y)) #compare each problem on page to page # 
        if x == pages(y): #if page 
            special_probs = special_probs + 1 

变量浏览器报告:

页数 = [0, [1,2,3], [4], [1,2], [1,2,3], [4,5,6], [1], [1,2,3] , [4,5,6], [7,8,9], [10]]

arr = [4,2,6,1,10]

新页面 = [10]

new_page_proto = [0]

z = 5

当前错误信息:

文件“C:/Users/the_h/.spyder-py3/temp.py”,第 43 行 for x in enumerate(pages(y)) #compare each problem on page to page # ^ SyntaxError: invalid syntax

标签: python

解决方案


我不确定我是否足够理解上下文,但这就是我会做的。我会尝试有这样的东西:

all_probs = [[[1,2,3],[4],[5]],[[1,2],[3,4,5]],[[1],[2],[3],[4,5]],...]

在这个例子中,第一个元素all_probs[0]是第一章。然后,该元素all_probs[0][0]是第一页。例如,我们看到第一页包含问题 1,2 和 3。

然后,您需要做的是:

counter = 0
for i in all_probs:               #selects a chapter
    for j in range(len(i)):       #goes through the pages
        for k in i[j]:            #for each problem on that page
            if k == j+1:          #if the prob nbr matches the page nbr
                counter+=1        #increment the counter

推荐阅读