删除重复的列表元素保留外观顺序 [英] Delete repeating list elements preserving order of appearance

查看:31
本文介绍了删除重复的列表元素保留外观顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在生成包含 10^6 到 10^7 实数的平面列表,其中一些是重复的.

I am producing flat lists with 10^6 to 10^7 Real numbers, and some of them are repeating.

我需要删除重复的实例,只保留第一次出现,并且不修改列表顺序.

I need to delete the repeating instances, keeping the first occurrence only, and without modifying the list order.

这里的关键是效率,因为我有很多列表要处理.

The key here is efficiency, as I have a lot of lists to process.

示例(假的):

输入:

  {.8, .3 , .8, .5, .3, .6}

期望输出

  {.8, .3, .5, .6}  

附注

使用 Union 删除重复元素(不保留顺序)会导致我穷人的笔记本电脑:

Deleting repeating elements with Union (without preserving order) gives in my poor man's laptop:

DiscretePlot[a = RandomReal[10, i]; First@Timing@Union@a, {i, 10^6 Range@10}]

推荐答案

你想要 DeleteDuplicates,保留列表顺序:

You want DeleteDuplicates, which preserves list order:

In[13]:= DeleteDuplicates[{.8, .3, .8, .5, .3, .6}]

Out[13]= {0.8, 0.3, 0.5, 0.6}

它是在 Mathematica 7.0 中添加的.

It was added in Mathematica 7.0.

这篇关于删除重复的列表元素保留外观顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆