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

时间:2016-11-12 23:10:17

标签: python list

我在编写代码并在列表中查找元素时遇到了困难。

  • 我需要编写一个打印" YES"如果列表包含任意顺序的数字1,2,3(所有这些)和" NO"否则。

    [10, 2, 4, 15, 3, 6, 1]   # YES
    [1, 17, 2, 45]            # NO (because number 3 is missing)
    
  • 另外,我需要修改程序以打印" YES"如果它包含所有数字1,2,3,按照它们列出的顺序发生(但不一定是连续的)和" NO"否则。

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

    显然,该计划还应打印"否"如果缺少1,2,3中的一个。这项任务的棘手部分是多次出现1,2,3的情况 例如程序应该打印"是"如果[3, 2, 1, 2, 3]因为那里 出现的是1,2,3,以正确的顺序出现,而#34; NO"应该 为[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

2 个答案:

答案 0 :(得分:2)

第一个很简单:

function Question() {

  this.getAllByQuizId = function(quizId) {
    return connection.acquire(function(err, con) {
      return con.query('select * from questions where `quizId` = ? ', quizId, function(err, rows) {
        if (err) throw err;

        con.release();

        console.log(JSON.parse(JSON.stringify(rows)));
        return JSON.parse(JSON.stringify(rows));
      });
    });
  };

第二个有点棘手:

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

答案 1 :(得分:1)

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循环完成后运行一次。请注意,如果您{for循环'elsebreak将无法运行。这是一个巧妙的技巧,因为如果我们找到了所有需要找到的东西,我们就会突破for循环,“else”我们找不到我们想要的东西,for循环完成意味着我们通过列表{{1} }。

注意7:列表中的计数器检查,以便我们可以检查清单中的内容。

注8:A基本上是variable += int一种递增计数器的更简洁方法。如果我们的柜台是3,我们通过了我们的清单,所以我们知道它是有序的。由于我们按顺序逐个浏览variable = variable + 1列表,并且我们只在匹配某些内容时递增计数器,我们知道它是否有序。您似乎知道如何通过索引访问列表,这是一种方法。

我知道还有很多其他方法可以提供更好的解决方案,但既然你说你是新手,想要学习,我觉得这是最好的方法。你学到了一些技巧,并没有过于复杂,因为所有的东西在逻辑上都非常简洁。

至于为什么你的代码没有工作你没有迭代(通过你的列表)你只检查0到列表大小的范围,基本上,A。要遍历列表,您必须使用0, 1, 2, 3, 4, 5, ..., n查看我的示例。