外贸建站cms,哈尔滨信息网招聘,廊坊网站建设招聘,资源搜索引擎搜索神器网CheckiO 是面向初学者和高级程序员的编码游戏#xff0c;使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务#xff0c;从而提高你的编码技能#xff0c;本博客主要记录自己用 Python 在闯关时的做题思路和实现代码#xff0c;同时也学习学习其他大神写的代码。
Chec…
CheckiO 是面向初学者和高级程序员的编码游戏使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务从而提高你的编码技能本博客主要记录自己用 Python 在闯关时的做题思路和实现代码同时也学习学习其他大神写的代码。
CheckiO 官网https://checkio.org/
我的 CheckiO 主页https://py.checkio.org/user/TRHX/
CheckiO 题解系列专栏https://itrhx.blog.csdn.net/category_9536424.html
CheckiO 所有题解源代码https://github.com/TRHX/Python-CheckiO-Exercise 题目描述
【Non-unique Elements】你将得到一个含有整数X的非空列表。在这个任务里你应该返回在此列表中的非唯一元素的列表。要做到这一点你需要删除所有独特的元素这是包含在一个给定的列表只有一次的元素。解决这个任务时不能改变列表的顺序。例如[12313] 1和3是非唯一元素结果将是 [1, 3, 1, 3]。
【链接】https://py.checkio.org/mission/non-unique-elements/
【输入】一个含有整数的列表
【输出】去除只出现过一次的元素后的列表
【前提】0 |X| 1000
【范例】
checkio([1, 2, 3, 1, 3]) [1, 3, 1, 3]
checkio([1, 2, 3, 4, 5]) []
checkio([5, 5, 5, 5, 5]) [5, 5, 5, 5, 5]
checkio([10, 9, 10, 10, 9, 8]) [10, 9, 10, 10, 9]解题思路
循环访问列表的每一个元素利用 count() 方法统计元素出现的次数若出现次数大于1就将这些元素添加到一个新列表最后返回该列表即可
代码实现
#Your optional code here
#You can import some modules or create additional functionsdef checkio(data: list) - list:data2 []for i in data:if data.count(i) 1:data2.append(i)return data2#Some hints
#You can use list.count(element) method for counting.
#Create new list with non-unique elements
#Loop over original listif __name__ __main__:#These asserts using only for self-checking and not necessary for auto-testingassert list(checkio([1, 2, 3, 1, 3])) [1, 3, 1, 3], 1st exampleassert list(checkio([1, 2, 3, 4, 5])) [], 2nd exampleassert list(checkio([5, 5, 5, 5, 5])) [5, 5, 5, 5, 5], 3rd exampleassert list(checkio([10, 9, 10, 10, 9, 8])) [10, 9, 10, 10, 9], 4th exampleprint(It is all good. Lets check it now)大神解答 大神解答 NO.1 def checkio(data):return [i for i in data if data.count(i) 1]大神解答 NO.2 from collections import Counterdef checkio(data):counter Counter(data)return [item for item in data if counter[item] 1]