在python列表中删除差异但保持顺序和重复项
使用3.x版-想知道解决以下问题的最简单和本机方法: 示例列表:在python列表中删除差异但保持顺序和重复项,python,python-3.x,Python,Python 3.x,使用3.x版-想知道解决以下问题的最简单和本机方法: 示例列表: listA = [1, 2, 3, 66, 0] listB = [0, 0, 1, 2, 3, 66, 0, 99, 0, 3] 如何消除两个列表之间的差异,使新的listC以完全相同的顺序与listA相同 因此,使用上面的示例,listC应该等于[1,2,3,66,0] 列表A可能比列表B大,另一个条件是列表A永远不会有重复编号,而列表B可能有重复编号 我试图解决的慈善俱乐部练习是: 林黛的大脑测试: 请编写一个程序,如果B
listA = [1, 2, 3, 66, 0]
listB = [0, 0, 1, 2, 3, 66, 0, 99, 0, 3]
如何消除两个列表之间的差异,使新的listC以完全相同的顺序与listA相同
因此,使用上面的示例,listC应该等于[1,2,3,66,0]
列表A可能比列表B大,另一个条件是列表A永远不会有重复编号,而列表B可能有重复编号
我试图解决的慈善俱乐部练习是:
林黛的大脑测试:
请编写一个程序,如果B元素在a中出现的顺序是B元素在B中出现的顺序,但不一定是连续的,则打印“是”。否则程序应打印“否”
林赛奖金测试:
请编写一个程序,如果B元素出现在
它们以B和连续的顺序出现
显然,如果有人喜欢挑战,请发布完整的程序来解决这两个问题。def(l1,l2):
def f(l1, l2):
i1 = 0
i2 = 0
while i1 < len(l1) and i2 < len(l2):
if l1[i1] == l2[i2]:
i1 += 1
i2 += 1
else:
i2 += 1
if i1 == len(l1):
return True
return False
listA = [1, 2, 3, 66, 0]
listB = [0, 0, 1, 2, 3, 66, 0, 99, 0, 3]
print (f(listA, listB))
# will print true
i1=0
i2=0
当i1
另一种作弊很轻微的方法是在操作符中使用带有的字符串。如果将每个列表转换为一个字符串,则可以快速查看a是否是B的子字符串,顺序相同且连续
def aInB(listA, listB):
str_a = "," + ",".join([str(c) for c in listA]) + ","
# ',1,2,3,66,0,'
str_b = "," + ",".join([str(c) for c in listB]) + ","
# ',0,0,1,2,3,66,0,99,0,3,'
return str_a in str_b
# True
现在这只在A的长度小于B的情况下有效,但根据问题的定义,这听起来总是正确的。额外的逗号是必要的,因为@stefanpochmann在评论中指出了问题
打印“是”和“否”非常简单:
if aInB(listA, listB):
print("YES")
else:
print("NO")
对于非连续方法,我相信您必须使用其中一种迭代方法。这个解决方案只是提供了一种思考“B中的A”的替代方法
编辑:我无法控制自己,所以这里有一个互动的方法,可能有点过头了,但也许你会发现它更容易理解(你永远不知道)
好吧,你显然是个聪明人-我如何在不创建此函数的情况下在同一个程序中实际使用它?抱歉,这只是一个解决问题的想法,应该打印“是”或“否”,为什么不创建函数?我想如果你想删除此函数,只需删除第一行,并将返回更改为打印状态谢谢-您的一个看起来太复杂了!我只是复制它就出错了!!失败,例如aInB([1],[11])
。很好。不过有一个解决办法。林迪的大脑测试只是问B是否是a的子序列,对吗?你写道:“列表B永远不会有重复的数字,而列表a惠斯特[sec]可能有重复的数字。”:但您给出的示例违反了此条件:listB在您的示例中有重复项。我已更正了该错误,但Trincot您的解决方案在第一部分起作用。谢谢您的布局非常好,而且非常简单。你能重新发布奖金问题解决方案吗?我正在重写其中的一些。完成后将重新发布;-)但是:Lindsay brain/bonus问题似乎表明A的元素通常比B多。如果B的元素多于A,那么如果我读得好的话,两者都不能返回“是”,因为B的每个元素都必须出现在A中,保持相同的顺序。我误解了吗?另一个问题。Lindey测试显示“.如果B元素出现在A中,则打印“是”。这是否意味着B的所有元素?还是一个就够了?但是如果一个足够了,那么请求它以相同的顺序出现在A中是没有意义的。如果一个足够了,算法可以停止检查任何其他内容,只打印“是”。或者它还应该进一步检查,如果有来自B的第二个元素出现在a中,但顺序错误,它仍然应该打印“否”?我觉得这个描述令人困惑。
def aInB(listA, listB):
# if listA is empty, don't even bother
if not listA:
return False
# build a dictionary where each key is a character in listA
# and the values are a list containing every index where that character
# appears in listB
occurences = {c:[i for i,e in enumerate(listB) if e==c] for c in listA}
# now we are going to walk through listA again
# but this time we are going to use our `occurences` dictionary
# to verify that the elements appear in order
last_index = 0
for i,e in enumerate(listA):
# if the character `e` never appears in listB
# then it will have an empty list
# and we can return False
if not occurences[e]:
return False
# now the next possible index for the next character in listA
# must be *greater* than the index of the last character we found
# if no such index exists, then listA is not contained within listB
# if it is, we update the last seen index
next_possible_index = [x for x in occurences[e] if x > last_index]
if not next_possible_index:
return False
last_index = next_possible_index[0]
# if we make it out of the for loop
# then all is well, and listA is contained in listB
# but not necessarily consequtively
return True