简体   繁体   English

以给定顺序包含给定元素集的列表

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

I am having difficulties in writing code with finding elements in the list. 我在编写代码以查找列表中的元素时遇到困难。

  • I need to write a program that prints "YES" if the list contains numbers 1, 2, 3 (all of them) in an arbitrary order and "NO" otherwise. 我需要编写一个程序,如果列表包含任意顺序的数字1、2、3(全部),则打印“ YES”,否则显示“ NO”。

     [10, 2, 4, 15, 3, 6, 1] # YES [1, 17, 2, 45] # NO (because number 3 is missing) 
  • Also, I need to modify the program to prints "YES" if it contains all the numbers 1, 2, 3 occurring in the order they are listed (but not necessarily consecutively) and "NO" otherwise. 另外,如果程序包含按列出顺序(但不一定连续)出现的所有数字1、2、3,则需要修改程序以打印“是”,否则打印“否”。

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

    Clearly, the program should also print "NO" if one of 1, 2, 3 is missing. 显然,如果缺少1、2、3之一,则程序还应打印“ NO”。 The tricky part of this task are cases of multiple occurrences of 1, 2, 3. For instance the program should print "YES" if [3, 2, 1, 2, 3] because there are occurrences of 1, 2, 3 appearing in the correct order while "NO" should be printed for [3, 3, 2, 2, 1, 2] because there are no such occurrences. 此任务的棘手部分是多次出现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]因为不存在这种情况。

I am not a pro at programming, so I am a little bit confused, because my source code for the first task doesn't give me the right output: 我不是编程专家,所以有点困惑,因为我第一个任务的源代码没有提供正确的输出:

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

The first one is simple: 第一个很简单:

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")

The second is a little trickier: 第二个有点棘手:

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")

note 1: we are making a list to check if 1,2,3 against. 注意1:我们正在制作清单,以检查是否有1,2,3。 Think of it as a note as you go buying grocery 购买杂货时将其视为便条

note 2: we are using in to check if an object is 'in' a list for example here we are looping through the list we created A and can be thought as for each in A, if each is in list_to_check, then that condition pass and execute what's in the if statement 注意2:例如,我们使用in来检查对象是否在列表中,例如,在这里我们遍历创建的A的列表,并且可以认为每个对象都在A中,如果每个都在list_to_check中,则该条件通过并执行if语句中的内容

note 3: we are removing the item we found and matched from our list_to_check because we've found it, so we don't have to check again. 注意3:因为已找到,所以我们将从list_to_check中删除找到并匹配的项目,因此我们无需再次检查。 remove() function takes an argument and it removes that item from the list you provide. remove()函数接受一个参数,并将其从您提供的列表中删除。

note 4: checking if our list is empty, if it is we know we have found all the items in our list meaning it passes. 注意4:检查清单是否为空,如果知道我们已经找到清单中的所有项目,则表示该清单已通过。

note 5: break breaks out of the for loop with out needing to finish the for loop. 注释5: break脱离for循环而无需完成for循环。 Since we found everything we needed, we don't need to check for further matches. 由于找到了所需的一切,因此我们不需要检查其他匹配项。

note 6: for loops can have an else code block. 注意6:for循环可以包含else代码块。 They can be thought of as if-else blocks but in for loop, the else code block will run once after the for loop completes. 可以将它们视为if-else块,但在for循环中, else代码块将在for循环完成后运行一次。 Note that the else won't run if you break out of the for loop. 请注意, else ,如果你不跑break了的for循环。 This is a neat trick since if we have found everything need to find, we break out of the for loop, "else" we didn't find what we looked for and the for loop finished meaning we went through the list A . 这是一个巧妙的技巧,因为如果我们找到了需要查找的所有内容,我们就会退出for循环,“ else”我们找不到我们想要的东西,并且for循环完成了,这意味着我们遍历了列表A

note 7: a counter checking in the list so we can go in order from checking what's in the list. 注意7:柜台会检查清单,以便我们从检查清单中的顺序开始。

note 8: variable += int is basically variable = variable + 1 a cleaner way of incrementing the counter. 注8: variable += intvariable = variable + 1是一种更干净的递增计数器的方法。 If our counter is 3, we went through our list, so we know it's in order. 如果我们的计数器是3,则我们遍历了列表,因此我们知道它是有序的。 Since we are going through the A list one at a time in order, and we only increment the counter once we matches something, we know it's in order or not. 由于我们一次按顺序遍历A列表,并且仅在匹配某项后才递增计数器,所以我们知道它是否按顺序进行。 It seems like you know how to access lists through index, and this is one way to do it. 似乎您知道如何通过索引访问列表,这是一种实现方法。

I know there are a lot of other ways that are better solutions but since you said you are new, and wanting to learn, I feel this is the best way to go. 我知道还有很多其他更好的方法,但是既然您说您是新手,并且想学习,我认为这是最好的方法。 You learn a few tricks and it's not overly complicated since everything is pretty logically layed out in also plain english. 您会学到一些技巧,并且它并不太复杂,因为所有内容在逻辑上也用普通的英语进行了布局。

As for why your code wasn't working you are not iterating (going through your list) you are only checking in range of 0 to size of list, basically, 0, 1, 2, 3, 4, 5, ..., n . 至于为什么您的代码不起作用,您没有迭代(遍历您的列表),您只检查了0到列表大小的范围,基本上是0, 1, 2, 3, 4, 5, ..., n To iterate through a list you have to use for temp_variable in list_name: see my example. 要遍历列表,您必须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