简体   繁体   English

如何在 Python 中比较两个列表(list2 与 list1 并找出有多少元素 <= list1)

[英]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:解释:

  1. 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]。

  2. 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:解释:

  1. 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]。

  2. 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)以获取list1list2中值的插入 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.

相关问题 Python:比较两个列表并根据list1中的值更新list2中的值 - Python: Compare two lists and update value in list2 based on value in list1 如何通过搜索 List1 中的子字符串来查找 List2 中的完整字符串? - How to find the full strings in List2 by searching with the substrings in List1? 将 2 个 python 列表转换为 1 个 Numpy 数组,格式为 ((list1, list2)) - Convert 2 python lists into 1 Numpy Array with the format ((list1, list2)) 在开始 Position 的 List1 中找到 List2 - Find List2 in List1 at starting Position 如何继续将元素从 list1 添加到 list2 直到 list2 结束? - How to keep adding elements from list1 to list2 until list2 is over? 对于 list1 的任何项目,如果它是 python 中的 list2 - for any item of list1 if it is list2 in python 我需要比较两个列表并检查 list1 中的字符串是否在 list2 中 - I need to compare two lists and check if a string from list1 is in list2 Python:如何使用列表在列表之间进行复制,例如 list1[index1] = list2[index2] - Python: how to copy between lists using lists, e.g. list1[index1] = list2[index2] 在python中list1 = [] list2 = []和list1 = list2 = []之间有什么区别? - What's the difference between list1 = [] list2 = [] and list1 = list2 = [] in python? 在python中,如何在没有任何共享元素的情况下在list2的元素上均匀分布list1的元素? - In python, how can I evenly distribute elements of list1 over elements of list2 without any shared elements?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM