简体   繁体   English

获取列表中小于特定元素的最小元素的最快方法

[英]Fastest way to get minimum elements in a list less than a particular element

I am better off explaining with an example我最好用一个例子来解释
Suppose, I have a list [6,3,5,1,4,2].假设,我有一个列表 [6,3,5,1,4,2]。

Starting from index 0, find all items that are smaller (that have not been marked) than the value at that index.从索引 0 开始,查找所有小于(尚未标记)该索引处的值的项目。

Index 0: [6,3,5,1,4,2]  
Elements less than 6: 5{3,5,1,4,2} 
Visited array: [1 0 0 0 0 0]

Index 1: [6,3,5,1,4,2]  
Elements less than 3: 2 {1,2}
Visited array: [1 1 0 0 0 0]

Index 2: [6,3,5,1,4,2]  
Elements less than 5: 3 {1,4,2}
Visited array: [1 1 1 0 0 0]

Index 3: [6,3,5,1,4,2]  
Elements less than 1: 0 {NULL}
Visited array: [1 1 1 1 0 0]

Index 4: [6,3,5,1,4,2]  
Elements less than 4: 1 {2}
Visited array: [1 1 1 1 1 0]

This yields an array as [5 2 3 0 1 0]

Currently using,目前使用,

def get_diff(lis):
    ans=[]
    for index,elem in enumerate(lis):
        number_of_smaller=len(filter(lambda x:x<elem ,lis[index+1:]))
        ans.append(number_of_smaller)
    return ans

However, I have a feeling that this will not be efficient.但是,我有一种感觉,这不会有效率。 How do I make it worthy of a huge list?我如何使它值得一个巨大的清单? Do I smell prefix sums?.我闻到前缀和了吗? Thanks,谢谢,

You can simply use a list comprehension within a dict comprehension to preserve the item as the key and the items which are less than it as the value (and use collections.OrderedDict to preserve the order):您可以简单地在 dict 推导中使用列表推导将项目保留为键,将小于它的项目保留为值(并使用collections.OrderedDict保留顺序):

>>> from collections import OrderedDict
>>> def get_diff(lis):
...        return OrderedDict((item,[i for i in lis if i<item]) for item in lis)

Since your condition is < there is no need to exclude the item itself, because that the cost of removing it in comparison is greater than including it.由于您的条件<没有必要排除项目本身,因为相比之下,删除它的成本大于包含它的成本。

Also if you want to preserve the indices to you can use enumerate to loop over your list :此外,如果您想保留索引,您可以使用enumerate循环遍历您的列表:

def get_diff(lis):
       return OrderedDict((item,index),[i for i in lis if i<item]) for index,item in enumerate(lis))

And if you want to count the number of that items you can use a generator expression within sum function :如果您想计算这些项目的数量,您可以在sum函数中使用生成器表达式:

>>> from collections import OrderedDict
>>> def get_diff(lis):
...        return OrderedDict((item,sum(1 for i in lis if i<item)) for item in lis)

NOTE: if you want to count the item less than any item after that (with larger index) you can simply use a indexing in your loop :注意:如果您想计算的项目少于此后的任何项目(具有更大的索引),您可以简单地在循环中使用索引:

>>> def get_diff(lis):
...        return OrderedDict((item,sum(1 for i in lis[index:] if i<item)) for index,item in enumerate(lis))
... 
>>> get_diff(l).values()
[5, 2, 3, 0, 1, 0]
my_list = [6,3,5,1,4,2] 

def get_diff(lis):
    result = []
    for visited, i in enumerate(range(len(lis))):
        limit = lis[i]
        elem = filter(None, [x if x < limit else None for x in lis][visited + 1:])
        result.append(len(elem))
    return result

print get_diff(my_list)
#[5, 2, 3, 0, 1, 0]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法在少于 O(n) 的时间内找到集合中的最小元素? - Is there a way to find minimum element in a set in less than O(n) time? 如果与前一个元素的差异小于值,则删除列表中的元素 - Remove elements in a list if difference with previous element less than value 以最小差异大于 Python 列表中的值对大多数数字进行采样的最快方法 - Fastest way to sample most numbers with minimum difference larger than a value from a Python list 从连续元素之间的差异小于特定数字的列表创建嵌套列表 - create a nested list from a list where where the difference between consecutive elements is less than a particular number 获取列表中元素存在的概率的最快方法 - Fastest way to get the probabilties for which an element is present in a list 如何删除 python 列表中小于特定字符数的所有元素 - how to remove all elements in a python list which are less than a particular number of charecters 带有列表的字典:获取与第四个列表项的最小值对应的键的最快方法? - Dictionary with lists: Fastest way to get the key corresponding to the minimum value of the fourth list item? 过滤掉出现次数少于最小阈值的元素 - Filter out elements that occur less times than a minimum threshold python-通过与另一个列表的元素进行比较来查找列表中元素的索引的最快方法 - python - Fastest way to find index of an element in list by comparing to elements of another list Python:检查元素是否小于下一个元素 - Python: Check if element is less than the next elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM