Complexity analysis of filter() with a lambda function

后端 未结 2 1907
无人共我
无人共我 2021-01-13 13:13

Given two lists, list1 and list2

list3 = filter(lambda x: x in list1,list2)

This returns the intersection of the

2条回答
  •  执笔经年
    2021-01-13 14:04

    The complexity could be O(nm) where n is the size of list1 and m is the size of list2. However this analysis although plausible depends on the underlying python implementation of intersection (which might use sorting so the complexity could be O(logn+logm) or sth)

    In order to experimentaly test this and verify the result, use this:

    1. generate variious test lists with varying items and sizes (including duplicates)
    2. test the function with the test lists and measure the time taken
    3. plot the results of time vs list sizes to verify the (exprimental) complexity

提交回复
热议问题