如何检查天气一个元素在Python的列表中仅出现一次?
如何编写一个函数来计算[2,1,2,5,2,1,1,1,3]
之类的列表中仅出现一次的数字。如果建立了两个以上的数字,则应返回最小的数字,并且如果没有数字具有一次性外观,则应返回“未找到”。
这是我编写的代码:
def one_time(nums, n):
nums.sort()
if nums[0] != nums[1]:
print(nums[0], end = " ")
for i in range(1, n - 1):
if (nums[i] != nums[i + 1] and
nums[i] != nums[i - 1]):
print( nums[i])
if nums[n - 2] != nums[n - 1]:
print(nums[n - 1], end = " ")
if __name__ == "__main__":
nums = [2, 1, 2, 5, 2, 1, 1, 3]
n = len(nums)
one_time(nums, n)
How can I write a function to compute the numbers that appear only once in a list like [2, 1, 2, 5, 2, 1, 1, 3]
. If more than two numbers are founded, it should return the smallest number and if there is no number with the one-time appearance it should return "Not found".
This is the code that I have written:
def one_time(nums, n):
nums.sort()
if nums[0] != nums[1]:
print(nums[0], end = " ")
for i in range(1, n - 1):
if (nums[i] != nums[i + 1] and
nums[i] != nums[i - 1]):
print( nums[i])
if nums[n - 2] != nums[n - 1]:
print(nums[n - 1], end = " ")
if __name__ == "__main__":
nums = [2, 1, 2, 5, 2, 1, 1, 3]
n = len(nums)
one_time(nums, n)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
collections.counter.counter.counter.counter 是大多数人确定多少人的方式每个项目都在列表中。计数后,您可以仅使用
1
(如果V == 1 part)过滤这些数字,则进行排序并进行next()< /代码>值。
next()
提供了提供默认值的能力,如果没有找到默认值:collections.Counter is the way most people determine how many of each item are in a list. Once you have counts, you can filter by those numbers with only
1
(that theif v == 1
part), sort and take thenext()
value.next()
offers the ability to provide a default if nothing is found:使用
collections.counter.counter.counter
列表元素,然后将键转换为键=原始列表的元素lst
and values =它们在该列表中的出现数量。使用 list clastiension 仅选择一次完全发生的元素。<<<<<< br>
从该列表中选择最小数字。
Use
collections.Counter
to count the list elements, and convert into a dictionary with keys = elements of the original listlst
and values = their number of occurrences in that list.Use list comprehension to select only the elements that occurred exactly once.
Select the minimum number from that list.
如果是我,我会使用两套:一组存储可能仍然是单身的数字,一个是存储我之前发现的数字。一旦一个数字在“看到”列表中,我就可以从单打中删除它:
If it were me, I'd use two sets: one to store the numbers that still might be singles, and one to store the numbers I've found before. As soon as a number is in the "seen before" list, I can remove it from the singles:
您可以使用带有其他名称的列表创建一个集合(例如myset = set(myList))
然后循环穿过该集合并使用内置
.count()在集合中的每个元素上函数。
制作一个称为计数器的变量或有意义并将其初始化为零的变量。
对于每次迭代,如果只有一个数字出现一次,并且计数器等于零,请在计数器中保存thr号(或者如果0是列表中的一个数字,则一个不同的变量)
如果有一次出现的数字,请检查其值与先前保存的数字。
我希望这会有所帮助,希望这有意义,我现在很累。祝你好运。
You can create a set using the list with a different name (e.g. myset = set(mylist))
Then loop through the set and use the built in
.count() function on each element in the set.
Make a variable called counter or something that makes sense and initialize it to zero.
For each iteration, if there is a number that only appears once and counter is equal to zero, save thr number in counter (or a different variable if 0 is a possible number in the list)
If there is another number that appears once, check it's value against the previously saved number.
I hope this helps, and I hope it makes sense, I'm quite tired right now. Good luck.