簡體   English   中英

以給定順序包含給定元素集的列表

[英]List containing the given set of elements in the given order

我在編寫代碼以查找列表中的元素時遇到困難。

  • 我需要編寫一個程序,如果列表包含任意順序的數字1、2、3(全部),則打印“ YES”,否則顯示“ NO”。

     [10, 2, 4, 15, 3, 6, 1] # YES [1, 17, 2, 45] # NO (because number 3 is missing) 
  • 另外,如果程序包含按列出順序(但不一定連續)出現的所有數字1、2、3,則需要修改程序以打印“是”,否則打印“否”。

     [45, 1, 5, 2, 77, 3] # YES [45, 1, 3, 77, 2] # NO (incorrect order) 

    顯然,如果缺少1、2、3之一,則程序還應打印“ NO”。 此任務的棘手部分是多次出現1、2、3的情況。例如,如果[3, 2, 1, 2, 3] 3、2、1、2、3 [3, 2, 1, 2, 3]出現,則程序應打印“ YES” [3, 2, 1, 2, 3]因為出現了1、2、3以正確的順序打印,而[3, 3, 2, 2, 1, 2] 3,3,2,2,1,1,2]則應打印“否” [3, 3, 2, 2, 1, 2]因為不存在這種情況。

我不是編程專家,所以有點困惑,因為我第一個任務的源代碼沒有提供正確的輸出:

n=int(input("Please enter the list length "))
A=[]

for i in range (0,n):
    print("Entering element ",i)
    CurEl=int(input("Please enter the element "))
    A.append(CurEl)
print(A)
for i in range (0,n):
    if (i==1) or (i==2)or (i==3):
        print("YES")
    else:
         print("NO")

Output:
Please enter the list length 5
('Entering element ', 0)
Please enter the element 1
('Entering element ', 1)
Please enter the element 2
('Entering element ', 2)
Please enter the element 3
('Entering element ', 3)
Please enter the element 4
('Entering element ', 4)
Please enter the element 5
[1, 2, 3, 4, 5]
NO
YES
YES
YES
NO

第一個很簡單:

A = [10, 2, 4, 15, 3, 6, 1]
if (1 in A) and (2 in A) and (3 in A):
    print("YES")
else:
    print("NO")

第二個有點棘手:

def locate(list_var):
    i = 0
    for l in list_var:
        if l == 1 and i == 0:
            i = 1
        elif l == 2 and i == 1:
            i = 2
        elif l == 3 and i == 2:
            return "YES"

    return "NO"


print(locate([45, 1, 5, 2, 77, 3]))
print(locate([45, 1, 3, 77, 2]))
n=int(input("Please enter the list length "))
A=[]

for i in range (0,n):
    print("Entering element ",i)
    CurEl=int(input("Please enter the element "))
    A.append(CurEl)
print(A)

#checking if they are in it
list_to_check = [1,2,3]        #note 1
for each in A:
    if each in list_to_check:  #note 2
        list_to_check.remove(each) #note 3
    if list_to_check == []:    #note 4
        print("yes, 1 2 3 are in it")
        break                  #note 5
else:                          #note 6
    print("no, 1 2 3 are not in it")

#checking the order
list_to_check = [1,2,3]
check_slot_counter = 0         #note 7
for each in A:
    if each == list_to_check[check_slot_counter]:
        check_slot_counter += 1 #note 8
    if check_slot_counter == 3:
        print("yes, 1 2 3 are in order")
        break
else:
    print("no, 1 2 3 are not in order")

注意1:我們正在制作清單,以檢查是否有1,2,3。 購買雜貨時將其視為便條

注意2:例如,我們使用in來檢查對象是否在列表中,例如,在這里我們遍歷創建的A的列表,並且可以認為每個對象都在A中,如果每個都在list_to_check中,則該條件通過並執行if語句中的內容

注意3:因為已找到,所以我們將從list_to_check中刪除找到並匹配的項目,因此我們無需再次檢查。 remove()函數接受一個參數,並將其從您提供的列表中刪除。

注意4:檢查清單是否為空,如果知道我們已經找到清單中的所有項目,則表示該清單已通過。

注釋5: break脫離for循環而無需完成for循環。 由於找到了所需的一切,因此我們不需要檢查其他匹配項。

注意6:for循環可以包含else代碼塊。 可以將它們視為if-else塊,但在for循環中, else代碼塊將在for循環完成后運行一次。 請注意, else ,如果你不跑break了的for循環。 這是一個巧妙的技巧,因為如果我們找到了需要查找的所有內容,我們就會退出for循環,“ else”我們找不到我們想要的東西,並且for循環完成了,這意味着我們遍歷了列表A

注意7:櫃台會檢查清單,以便我們從檢查清單中的順序開始。

注8: variable += intvariable = variable + 1是一種更干凈的遞增計數器的方法。 如果我們的計數器是3,則我們遍歷了列表,因此我們知道它是有序的。 由於我們一次按順序遍歷A列表,並且僅在匹配某項后才遞增計數器,所以我們知道它是否按順序進行。 似乎您知道如何通過索引訪問列表,這是一種實現方法。

我知道還有很多其他更好的方法,但是既然您說您是新手,並且想學習,我認為這是最好的方法。 您會學到一些技巧,並且它並不太復雜,因為所有內容在邏輯上也用普通的英語進行了布局。

至於為什么您的代碼不起作用,您沒有迭代(遍歷您的列表),您只檢查了0到列表大小的范圍,基本上是0, 1, 2, 3, 4, 5, ..., n 要遍歷列表,您必須for temp_variable in list_name:使用for temp_variable in list_name:請參見我的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM