繁体   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