尝试在长列表中找到唯一的数字,然后按顺序返回。 (上一个版本不起作用)
您可能会认为这个问题是上述的。回答,但最后一个版本与2位数字无效 给定数字列表,查找并打印仅出现一次的元素。这些元素应按照原始列表中出现的顺序打印。
因此,这是我的代码(由其他pythoneer修复),
a = [int(s) for s in input().split()]
sortedLst = sorted(a)
unique = []
uniqueOrd = []
for i in range(len(a) - 2):
if sortedLst[i + 1] != sortedLst[i] and sortedLst[i + 1] != sortedLst[i + 2]:
unique.append(sortedLst[i + 1])
for num in a:
if num in unique:
uniqueOrd.append(num)
print(*uniqueOrd)
但是输出不适合2位数字编号,我该如何修复此错误?
You may consider this question is prev. answered, but the last version doesn't work with 2 digits number
Given a list of numbers, find and print the elements that appear in it only once. Such elements should be printed in the order in which they occur in the original list.
so here's my code (being fixed by other pythoneers)
a = [int(s) for s in input().split()]
sortedLst = sorted(a)
unique = []
uniqueOrd = []
for i in range(len(a) - 2):
if sortedLst[i + 1] != sortedLst[i] and sortedLst[i + 1] != sortedLst[i + 2]:
unique.append(sortedLst[i + 1])
for num in a:
if num in unique:
uniqueOrd.append(num)
print(*uniqueOrd)
but the output does not work with 2 digits number, how can I fix this bug?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不必为从长列表中找到
唯一
而努力工作,只需考虑collections
- 计数器的nice lib即可。它更快,更柔软。尝试一些输入,例如:2 3 1 4 22 33 44 55 33 22
You don't have to labor yourself on finding the
uniques
from a long list, just consider the nice lib fromcollections
- Counter. It's faster and more Pythonic.Try some inputs like: 2 3 1 4 22 33 44 55 33 22