简体   繁体   English

我需要什么语法来正确地遍历此列表?

[英]What syntax do I need to properly iterate through this list?

Looping through a list, trying to use the list in an if statement, but receiving error messages about syntax and how lists must be integers or slices and not tuples. 遍历列表,尝试在if语句中使用该列表,但收到有关语法以及列表必须为整数或片而不是元组的错误消息。 Trying to understand what went wrong. 试图了解出了什么问题。

I'm working on a challenge from Hackerrank that I am just about done with, but I am a bit stuck. 我正在处理即将完成的Hackerrank挑战,但我有些困惑。 Essentially, I have a list called "pages", referring to the pages in a workbook as specified in the description of the challenge linked here: 本质上,我有一个名为“页面”的列表,指的是工作簿中的页面,如在此处链接的挑战说明中所指定:

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

pages is a list where each element represents 1 page in the workbook and the elements inside these elements represent the numbers of the problems on that page (ie: page 1 has problems 1, 2, and 3 for chapter 1 while page 2 has problem 4 for that chapter). pages是一个列表,其中每个元素代表工作簿中的一页,这些元素中的元素代表该页面上的问题编号(即:第1章的第1页有问题1、2和3,而第2页的第4问题是该章)。 The challenge asks us to count the total number of problems in the workbook whose problem numbers match the page number that they are found on. 挑战要求我们计算工作簿中问题总数与找到的页码相匹配的问题总数。

My first instinct was to iterate through the pages, then iterate through the problems on that page and add to a counter special_probs anytime the iterating variable for the problems on the page matched the iterating variable for that page. 我的第一个直觉是遍历页面,然后遍历该页面上的问题,并在该页面上问题的迭代变量与该页面的迭代变量匹配时随时添加到special_probs计数器中。 This is all done in the bottom 4 lines of code. 所有这些都在代码的后4行中完成。 However, calling the current page we are on in the nested for loop is giving me some issues. 但是,在嵌套的for循环中调用我们当前所在的页面会给我一些问题。 It is likely something super easy or silly, but I'd appreciate your help in understanding why I am not allowed to do it the way I have it and what I need to do differently in order to get it to work as intended. 这可能是一件超级容易或愚蠢的事情,但是我很感谢您的理解,以帮助您理解为什么我不能以自己的方式做,以及需要做些什么才能使它按预期工作。 If you need more information or context please let me know. 如果您需要更多信息或背景,请告诉我。 Thank you! 谢谢!

(I also commented the hell out of my code in this particular instance. If it is distracting, I can cut them out.) (在这种情况下,我还从代码中注释了地狱。如果分散了注意力,我可以将其删除。)


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 

Variable explorer reports: 变量浏览器报告:

pages = [0, [1,2,3], [4], [1,2], [1,2,3], [4,5,6], [1], [1,2,3], [4,5,6], [7,8,9], [10]] 页数= [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] arr = [4,2,6,1,10]

new_page = [10] new_page = [10]

new_page_proto = [0] new_page_proto = [0]

z = 5 z = 5

Current error message: 当前错误信息:

File "C:/Users/the_h/.spyder-py3/temp.py", line 43 for x in enumerate(pages(y)) #compare each problem on page to page # ^ SyntaxError: invalid syntax 文件“ C:/Users/the_h/.spyder-py3/temp.py”,enumerate(pages(y))中x的第43行#逐页比较每个问题#^ SyntaxError:语法无效

I'm not sure wheter I understood the context enough but here is what I would do. 我不确定我是否足够了解上下文,但是这是我会做的。 I would try to have something like this : 我会尝试这样的事情:

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

in this example the first element all_probs[0] is the first chapter. 在此示例中,第一个元素all_probs[0]是第一章。 Then, the element all_probs[0][0] is the first page. 然后,元素all_probs[0][0]是第一页。 We see that the first page contains the problems 1,2 and 3 for example. 我们看到第一页包含问题1,2和3。

Then, what you would have to do would be this : 然后,您将要做的是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM