![](/img/trans.png)
[英]How do I properly iterate through a list of web elements in Selenium Webdriver using Python?
[英]What syntax do I need to properly iterate through this list?
遍歷列表,嘗試在if語句中使用該列表,但收到有關語法以及列表必須為整數或片而不是元組的錯誤消息。 試圖了解出了什么問題。
我正在處理即將完成的Hackerrank挑戰,但我有些困惑。 本質上,我有一個名為“頁面”的列表,指的是工作簿中的頁面,如在此處鏈接的挑戰說明中所指定:
https://www.hackerrank.com/challenges/lisa-workbook/problem
pages是一個列表,其中每個元素代表工作簿中的一頁,這些元素中的元素代表該頁面上的問題編號(即:第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]
new_page = [10]
new_page_proto = [0]
z = 5
當前錯誤信息:
文件“ C:/Users/the_h/.spyder-py3/temp.py”,enumerate(pages(y))中x的第43行#逐頁比較每個問題#^ SyntaxError:語法無效
我不確定我是否足夠了解上下文,但是這是我會做的。 我會嘗試這樣的事情:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.