如何从100000列表中删除50000个元素? [英] how to remove 50000 elements from a 100000 list?
问题描述
我想从列表中删除大约50000个元素,其中包含100000
元素。
示例代码如下:
a = range(10)
b =范围(4)
对于b中的x:
... .a.remove(x)
.... a
[4,5,6,7,8,9]
时a和b的尺寸很小,它会快速完成,但是当a和b
有很多元素时。
如:a =范围(100000)
b =范围(50000)
对于b中的x:
.... a.remove(x)
....
它会很慢。我应该换成另一个数据结构并选择
a更好算术吗?
欢迎任何建议。
非常感谢!
< Ju />
Ju Hui写道:
我想从列表中删除大约50000个元素,其中有100000个
元素。
示例代码如下:
a =范围(10)
b =范围(4 )
for b in b:... a.remove(x)
... a [4,5,6,7,8,9]
和b的尺寸很小,它会快速完成,但是当a和b
有很多元素时。
如:a =范围(100000)
b =范围(50000)>对于b中的x:
... a.remove(x)
...
它会很慢。我要改成另一个数据结构并选择更好的算术吗?
listcomprehension怎么样?
new_list = [e for old_list if predicate(e)]
#可能你也需要这个:
old_list [:] = new_list
Diez
尝试使用set对象:a = set(range(100000))
b =设定(范围(50000))
a = a - b
< BLOCKQUOTE>>但是当a和b有很多元素时。例如:
a = range(100000)
b = range(50000)
for x in b:
... a.remove(x)
...
它会很慢。
嗯,你的问题很模糊。在你的例子中,
你正在删除连续的范围。因此,你应该能够
来做类似
a = range(100000)
del a [:50000]
可能会有相当大的加速。但是,如果B
包含一组不相交的条目,那么简单的解决方案
将需要A * B检查,这取决于
$ b的大小$ b A和B(如你所发现的那样)。
假设del方法相当快,你可能会使用上述方法对不相交的集合进行某种优化。
。将B分成连续的
件,然后将它们作为切片传递给删除。
或者如果B是某种模式,你可以做
a =范围(100000)
del a [:: 2] #delete偶数
a =范围(100000)
del a [1 :: 2] #delete奇数
a =范围(100000)
del a [2 :: 5]#删除每5个元素以
开头第3个
另一个尝试可能是尝试
a = [x代表x而不是b中的x]
但是,这仍然在进行A * B检查,并且随着尺寸的增加,可能会降低
降级。
只是一些想法。
-tkc
I want to remove about 50000 elements from a list,which has 100000
elements.
sample code like below:
a=range(10)
b=range(4)
for x in b: .... a.remove(x)
.... a [4, 5, 6, 7, 8, 9]
when a and b is small size, it will finished quickly, but when a and b
have many elements.
such as: a=range(100000)
b=range(50000)
for x in b:
.... a.remove(x)
....
it will very slowly. Shall I change to another data structure and choos
a better arithmetic?
any suggestion is welcome.
thanks a lot!
Ju Hui wrote:
I want to remove about 50000 elements from a list,which has 100000
elements.
sample code like below:a=range(10)
b=range(4)
for x in b: ... a.remove(x)
... a [4, 5, 6, 7, 8, 9]
when a and b is small size, it will finished quickly, but when a and b
have many elements.
such as: a=range(100000)
b=range(50000)
for x in b:
... a.remove(x)
...
it will very slowly. Shall I change to another data structure and choos
a better arithmetic?
How about a listcomprehension?
new_list = [e for e in old_list if predicate(e)]
# Possibly you need this, too:
old_list[:] = new_list
Diez
Try to use set objects:a=set(range(100000))
b=set(range(50000))
a = a - b
> but when a and b have many elements. such as:a=range(100000)
b=range(50000)
for x in b:
... a.remove(x)
...
it will very slowly.
Well, your problem is rather ambiguous. In your examples,
you''re removing contiguous ranges. Thus, you should be able
to do something like
a = range(100000)
del a[:50000]
which may have a considerable speedup. However, if B
contains a disjoint sets of entries, the simple solution
will require A*B checks, making it dependant on the size of
A and B (as you''ve discovered).
Assuming the "del" method is considerably faster, you might
be able to do some sort of optimization of the disjoint set,
using the above-mentioned method. Break B into contiguous
pieces, and then pass those as slices to delete.
Or if B is a pattern of some sort, you could do
a = range(100000)
del a[::2] #delete even numbers
a = range(100000)
del a[1::2] #delete odd numbers
a = range(100000)
del a[2::5] # delete every 5th element beginning with the 3rd
Another attempt might be to try
a = [x for x in a if x not in b]
However, this is still doing A*B checks, and will likely
degrade with as their sizes increase.
Just a few ideas.
-tkc
这篇关于如何从100000列表中删除50000个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!