根据它们的相互关系过滤掉列表中的元素

我正在处理一个有趣的 python 问题:


给定一个整数、细菌和一个正整数常数 K 的列表,如果有两个元素 i 和 j 满足以下条件:


j < i <= j + K

设计一个函数,使 i 留下并移除 j,并返回列表中元素的最小可能数量。


功能: micro_world(bacteria, K)


例如,


bacteria = [101, 53, 42, 102, 101, 55, 54]

K = 1

最终结果应该是


 [42,102,55] 

因此应返回 3。


相似地


micro_world([101, 53, 42, 102, 101, 55, 54], 1) 会给我一个结果 3


micro_world([20, 15, 10, 15, 20, 25], 5) 将给我一个结果 1


我正在考虑使用列表理解来过滤掉不符合上述标准的元素,从而获得我想要的元素。但我不确定如何继续,因为它涉及列表中每个元素之间的关系。


result = [ i for i in bacteria if ... ]

我的 if 语句应该使用什么?


如果这不是一个好方法,我该怎么办?


谢谢。


编辑:这两个答案都为我提供了非常好的指导。但是关于细菌列表中的重复值只有一件小事。例如,如果输入


bacteria = [3, 3]

即使这只是一个块,结果应该是 2,因为 3 !> 3 因此两者都不会被删除。


繁星点点滴滴
浏览 93回答 2
2回答

慕婉清6462132

您实际上是在尝试将您的数字列表分组到块中,其中每个k数字与该块中另一个数字的距离小于。因为我不擅长解释事情,让我们看一个例子:bacteria = [101, 53, 42, 102, 101, 55, 54]K = 1首先,我们要对该列表进行排序,以便数字按大小排列:[102, 101, 101, 55, 54, 53, 42]现在我们遍历列表并在每次较大的细菌无法吞下较小的细菌时创建一个新的数字块:[[102, 101, 101], [55, 54, 53], [42]]最后我们计算chunk的数量,从而得到想要的结果:3。代码:def micro_world(bacteria, k):&nbsp; &nbsp; # sort the list descendingly&nbsp; &nbsp; bacteria = sorted(bacteria, reverse=True)&nbsp; &nbsp; # loop over the list but skip all the "swallowed" bacteria&nbsp; &nbsp; i = 0&nbsp; &nbsp; result = 0&nbsp; &nbsp; while i < len(bacteria):&nbsp; &nbsp; &nbsp; &nbsp; bacterium_size = bacteria[i]&nbsp; &nbsp; &nbsp; &nbsp; # while the difference between the numbers is <= k, the smaller&nbsp; &nbsp; &nbsp; &nbsp; # bacterium is swallowed&nbsp; &nbsp; &nbsp; &nbsp; bigger_bacterium_exists = False&nbsp; &nbsp; &nbsp; &nbsp; while i+1 < len(bacteria):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; difference = bacterium_size - bacteria[i+1]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # if the difference is too big, it can't be swallowed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if difference > k:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # if there are two bacteria of the same size, they can't swallow&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # each other. But if a bigger bacterium exists, it can swallow&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # them both&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if difference == 0 and not bigger_bacterium_exists:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # all conditions are met, the bacterium is swallowed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bacterium_size = bacteria[i+1]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i += 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bigger_bacterium_exists = True&nbsp; &nbsp; &nbsp; &nbsp; # at this point, the bacterium has swallowed everything it can.&nbsp; &nbsp; &nbsp; &nbsp; # Increment the result counter and move on to the next bacterium.&nbsp; &nbsp; &nbsp; &nbsp; result += 1&nbsp; &nbsp; &nbsp; &nbsp; i += 1&nbsp; &nbsp; return result

凤凰求蛊

这是一个使用的解决方案numpy:import numpy as npdef micro_world(bacteria, K):&nbsp; &nbsp; # convert bacteria list to a numpy array:&nbsp; &nbsp; bacteria = np.asarray(bacteria)&nbsp; &nbsp; # sort array and remember the indices used for sorting:&nbsp; &nbsp; sarg = np.argsort(bacteria)&nbsp; &nbsp; sortedbac = bacteria[sarg]&nbsp; &nbsp; # compute differences between adjacent elements:&nbsp; &nbsp; diff = np.ediff1d(sortedbac, K + 1)&nbsp; &nbsp; # throw away elements that are too close to neighbors&nbsp; &nbsp; # (find indices of the elements to keep):&nbsp; &nbsp; idx = np.flatnonzero(diff > K)&nbsp; &nbsp; # create a new list that meets selection criteria:&nbsp; &nbsp; return bacteria[np.sort(sarg[idx])]这是一个“纯”的 Python 实现:def micro_world(bacteria, K):&nbsp; &nbsp; # sort array and remember the indices used for sorting:&nbsp; &nbsp; sarg = [i[0] for i in sorted(enumerate(bacteria), key=lambda x: x[1])]&nbsp; &nbsp; sortedbac = [bacteria[i] for i in sarg]&nbsp; &nbsp; # compute differences between adjacent elements:&nbsp; &nbsp; diff = [j - i for i, j in zip(sortedbac[:-1], sortedbac[1:])] + [K + 1]&nbsp; &nbsp; # throw away elements that are too close to neighbors&nbsp; &nbsp; # (find indices of the elements to keep):&nbsp; &nbsp; idx = [i for i, v in enumerate(diff) if v > K]&nbsp; &nbsp; # create a new list that meets selection criteria:&nbsp; &nbsp; return [bacteria[i] for i in sorted([sarg[i] for i in idx])]如果你只对元素的数量感兴趣,而不对元素本身感兴趣,那么你可以修改 ie 第二个版本,如下所示:def micro_world(bacteria, K):&nbsp; &nbsp; sortedbac = sorted(bacteria)&nbsp; &nbsp; diff = [j - i for i, j in zip(sortedbac[:-1], sortedbac[1:])] + [K + 1]&nbsp; &nbsp; return sum(1 for v in diff if v > K)在numpy随后的版本将成为:def micro_world(bacteria, K):&nbsp; &nbsp; return np.sum(np.ediff1d(np.sort(bacteria), K + 1) > K)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python