[英]How to compare two lists (list2 with list1 and find how many elements are <= list1) in Python
I'm stuck with comparing two lists and finding how many elements are <= list1 (list2 <= list1) and putting the result in a new_list.我坚持比较两个列表并找出有多少元素 <= list1(list2 <= list1)并将结果放入 new_list。
Here are the test cases:以下是测试用例:
Test Case 1:测试用例 1:
list1 = [1, 4, 2, 4]
list2 = [3, 5]
Output: [2, 4]
Explanation:解释:
For list2[0] = 3, we have 2 elements in list1 (list1[0] = 1 and list1[2] = 2) that are <= list2[0].对于 list2[0] = 3,list1 中有 2 个元素(list1[0] = 1 和 list1[2] = 2)<= list2[0]。
For list2[1] = 5, we have 4 elements in list1 (list1[0] = 1, list1[1] = 4, list1[2] = 2, and list1[3] = 4) that are <= list2[1]对于 list2[1] = 5,list1 中有 4 个元素(list1[0] = 1、list1[1] = 4、list1[2] = 2 和 list1[3] = 4)<= list2[ 1]
Test Case 2:测试用例 2:
list1 = [1, 2, 3]
list2 = [2, 4]
Output: [2, 3]
Explanation:解释:
For list2[0] = 2, we have 2 elements in list1 (list1[0] = 1 and list1[1] = 2) that are <= list2[0].对于 list2[0] = 2,list1 中有 2 个元素(list1[0] = 1 和 list1[1] = 2)<= list2[0]。
For list2[1] = 4, we have 3 elements in list1 (list1[0] = 1, list1[1] = 2, list1[2] = 3) that are <= list2[1]对于 list2[1] = 4,list1 中有 3 个元素(list1[0] = 1、list1[1] = 2、list1[2] = 3)<= list2[1]
I was thinking to solve with two pointers where I place the first pointer in list1[0] and 2nd pointer in list2[0] then compare each element in both lists and increment a count each time I see an element in list2 is smaller than elements in list1.我想用两个指针来解决,我将第一个指针放在 list1[0] 中,将第二个指针放在 list2[0] 中,然后比较两个列表中的每个元素,并在每次看到 list2 中的元素小于元素时递增计数在清单 1 中。 The implementation is a little challenging to me and I'm sure whether it's working or not.
实施对我来说有点挑战,我确定它是否有效。
Here what I wrote:这是我写的:
def counts(list1, list2):
new_list = []
count = 0
a = list1[0]
b = list2[0]
i, j = 1, 1
while a or b:
if a <= b:
count += 1
new_list.append(a[i])
i += 1
return new_list
You can use Python sum
on boolean to get the number of items that match, eg您可以在 boolean 上使用 Python
sum
来获取匹配的项目数,例如
[sum(l1x <= l2x for l1x in list1) for l2x in list2]
If you need efficiency 1 , you can switch to a numpy
based solution, such as如果你需要效率1 ,你可以切换到基于
numpy
的解决方案,例如
np.sum(np.asarray(list1)[:, None] <= [list2], axis=0)
If you want the version without list comprehension or without sum:如果您想要没有列表理解或没有总和的版本:
res = []
for l2x in list2:
cnt = 0
for l1x in list1:
cnt += l1x <= l2x
res.append(cnt)
1 Disclaimer: I did not test the actual efficiency. 1免责声明:我没有测试实际效率。
The easiest way is:最简单的方法是:
import numpy as np
[(np.array(list1)<=l2).sum() for l2 in list2]
But if you need a more efficient solution, just let me know但如果您需要更有效的解决方案,请告诉我
You could sort both the input lists.您可以对两个输入列表进行排序。 Then binary search(
bisect.bisect_right
from std lib) to get the insertion position of values in list2
in list1
.然后二进制搜索(
bisect.bisect_right
来自 std lib)以获取list1
中list2
中值的插入 position。 The position would eventually tell you how many values are less than the value of the items in list2
. position 最终会告诉您有多少值小于
list2
中项目的值。
from bisect import bisect_right # For binary search.
srt_l1 = sorted(list1) # NlogN
srt_l2 = sorted(list2) # MlogM
out = [bisect_right(srt_l1, val) for val in srt_l2] # MlogN
This brings the time complexity to O(NlogN + MlogM)
with extra space of N+M
(though you could sort in-place using list.sort
).这给
O(NlogN + MlogM)
带来了时间复杂度和额外的N+M
空间(尽管您可以使用list.sort
就地排序)。 The suggested answers above have a time complexity of O(N*M)
.上面建议的答案的时间复杂度为
O(N*M)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.