gpt4 book ai didi

python - 解决一个列表是否充当另一个列表的子列表

转载 作者:行者123 更新时间:2023-12-01 01:42:58 25 4
gpt4 key购买 nike

我已经研究这个问题一段时间了,但感觉完全迷失了。这似乎是非常基本的问题,但似乎无法解决它。我并不是真的在寻找确切的答案,只是想知道我是否走在正确的道路上。我刚刚完成了编程入门的第三周,所以我确信某些格式有点奇怪,对此感到抱歉。

我正在尝试定义一个可以接受两个列表的函数:list1list2 并查看 list1 是否作为以下列表的子列表list2 同时还考虑了排序。

例如,书中的问题是这样说的:

如果list1定义为:[15, 1, 100]

list2定义为:[20, 15, 30, 50, 1, 100]

那么 list1list2 的子列表,因为 list1 中的数字(15、1 和 100)出现在 list2 中 按照相同的顺序。

但是,list [15, 50, 20] 不是 list2 的子列表,因为顺序不同。

我不确定我是否以正确的方式处理这件事,但我想附上我迄今为止所掌握的内容,并且希望任何人对此发表意见。我添加了评论,以便更深入地了解我的思考过程。

<小时/><小时/>
l1 = eval(input('\nPlease enter a list of integers: '))
l2 = eval(input('\nPlease enter a second list of integers: '))

def subList(l1, l2):
'Takes two lists as input from the user and determines'
'True if list1 is a sublist of list2 and false otherwise.'

newLst = []
indexNum = 0
result = subList(l1, l2)

if len(l1) > len(l2):
return False
elif l1 == []:
return True

for num in l1:

#My thinking here is that this while loop should run for as long as ther variable indexNum
#Doesn't exceed the length of lst2, allowing me to compare every num of lst1 with that of lst2
while indexNum < len(l2):

#If I come across a number in lst2, at a certain index, that's the same as lst1 I want
#to execute the following:
if l2[indexNum] == num:

#I've added a blank list at the top, newLst, which I want to append the matching number to.
newLst.append(l2[indexNum])

#I'll also want to still add one to the indexNum variable to compare the next number in lst2
indexNum = indexNum + 1

break

#If the number at lst2[indexNum] isn't equal to that of lst1, I still want to add to the
##indexNum variable to keep going through the loop and comparing the other items.
else:
indexNum = indexNum + 1

## I'm thinking here that if at the end of the outer loop, if my newLst is equal to the lst1, then that
## should mean that it works as a sub list. I could be wrong here and my thinking is way off though.
## If it is equal then the return value should be true, if not false.
if l1 == newLst:
return True
else:
return False
return True

最佳答案

我继续检查了你的代码。我不得不说,这几乎就像是有人故意在一个完美的解决方案中插入了坏行:

  1. 函数的第 6 行有一个递归调用。这不仅没有用,而且会导致函数不断递归。
  2. 那场决赛 if...else也在扼杀它。显然newlst不等于l1 ,因为当您到达这里时,您最多只检查了 l1 中的一个元素- 删除它。
  3. 这使得newlst没用。
  4. 充分利用 else on while。没有像这里一样自然地看到它的许多好的用途 - 很好!

我错过的@Olivier 的补充

  • 正在检查l1为空是多余的 - 循环将跳过,并且返回 True
  • 在中断之前,您仍然需要增加计数器
  • 这是固定的解决方案:

    def subList(l1, l2):
    indexNum = 0
    if len(l1) > len(l2): return False

    for num in l1:
    while indexNum < len(l2):
    if l2[indexNum] == num:
    indexNum = indexNum + 1
    break
    indexNum = indexNum + 1
    else:
    return False
    return True

    l1 = [15,1,100]
    l2 = [20,15,30,50,1,100]
    l3 = [15,50,20]
    print(subList(l1,l2))
    print(subList(l3,l2))

    循环的逻辑:

    1. l1 中获取一项.
    2. 循环直到在 l2 中找到它。 indexNum在迭代之间很好地管理这一点。
    3. else如果没有找到,请返回False .
    4. 返回True如果您设法对 ​​l1 中的所有元素执行此操作.

    您的解决方案不仅简单,而且是最有效的。

    关于python - 解决一个列表是否充当另一个列表的子列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51689412/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com