Remove duplicates · 去除重复项
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
This one builds on a lesson you haven't finished yet. Do that lesson first: · 此任务基于您尚未完成的课程。请先完成以下课程:
Python Basics · Python 基础 · Functions · 函数 →
English
Remove duplicates
Return a new list that keeps each value only once, in the order it first appeared: [1, 1, 2, 3, 3, 1] becomes [1, 2, 3].
A set answers "have I seen this already?" in one fast step. Keep a set of seen values and a list for the result, and only append a value the first time you meet it.
中文
去除重复项
返回一个新列表,每个值只保留一次,并按它首次出现的顺序排列:[1, 1, 2, 3, 3, 1] 变成 [1, 2, 3]。
集合(set)能一步快速回答「我见过这个吗?」。用一个 set 记录见过的值,用一个列表存放结果,只在第一次遇到某个值时把它加入结果。
Write dedupe(xs) that returns a new list with duplicates removed, keeping the order of first appearance. · 编写 dedupe(xs),返回去除重复项后的新列表,保留每个元素首次出现的顺序。
Click Run to see the output here. · 点击“运行”查看此处输出。