如何检查天气一个元素在Python的列表中仅出现一次?

发布于 2025-02-07 19:12:33 字数 579 浏览 2 评论 0原文

如何编写一个函数来计算[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

め七分饶幸 2025-02-14 19:12:33

collections.counter.counter.counter.counter 是大多数人确定多少人的方式每个项目都在列表中。计数后,您可以仅使用1(如果V == 1 part)过滤这些数字,则进行排序并进行next()< /代码>值。 next()提供了提供默认值的能力,如果没有找到默认值:

from collections import Counter

l = [2, 1, 2, 5, 1, 1, 3]

next((k for k,v in sorted(Counter(l).items()) if v == 1), "Not Found")
# 3

# with no unique elements

l = [2,2,1,1]

next((k for k,v in sorted(Counter(l).items()) if v == 1), "Not Found")
# 'Not Found'

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 the if v == 1 part), sort and take the next() value. next() offers the ability to provide a default if nothing is found:

from collections import Counter

l = [2, 1, 2, 5, 1, 1, 3]

next((k for k,v in sorted(Counter(l).items()) if v == 1), "Not Found")
# 3

# with no unique elements

l = [2,2,1,1]

next((k for k,v in sorted(Counter(l).items()) if v == 1), "Not Found")
# 'Not Found'
涙—继续流 2025-02-14 19:12:33

使用 collections.counter.counter.counter列表元素,然后将键转换为键=原始列表的元素lst and values =它们在该列表中的出现数量。
使用
list clastiension 仅选择一次完全发生的元素。<<<<<< br>
从该列表中选择最小数字。

from collections import Counter

lst = [2, 1, 2, 5, 2, 1, 1, 3]

cnt = dict(Counter(lst))
print(cnt)
# {2: 3, 1: 3, 5: 1, 3: 1}

try:
  smallest_once = min([x for x, c in cnt.items() if c == 1])
  print(smallest_once)
except ValueError:
  print("Not found")
# 3

Use collections.Counter to count the list elements, and convert into a dictionary with keys = elements of the original list lst 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.

from collections import Counter

lst = [2, 1, 2, 5, 2, 1, 1, 3]

cnt = dict(Counter(lst))
print(cnt)
# {2: 3, 1: 3, 5: 1, 3: 1}

try:
  smallest_once = min([x for x, c in cnt.items() if c == 1])
  print(smallest_once)
except ValueError:
  print("Not found")
# 3
倾城花音 2025-02-14 19:12:33

如果是我,我会使用两套:一组存储可能仍然是单身的数字,一个是存储我之前发现的数字。一旦一个数字在“看到”列表中,我就可以从单打中删除它:

def one_time(nums):
    single = set(nums)
    multi = set()
    for n in nums:
        if n in multi and n in single:
            single.remove(n)
        multi.add(n)
    return list(single)

if __name__ == "__main__":
    nums = [2, 1, 2, 5, 2, 1, 1, 3]
    print(min(one_time(nums)))

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:

def one_time(nums):
    single = set(nums)
    multi = set()
    for n in nums:
        if n in multi and n in single:
            single.remove(n)
        multi.add(n)
    return list(single)

if __name__ == "__main__":
    nums = [2, 1, 2, 5, 2, 1, 1, 3]
    print(min(one_time(nums)))
離人涙 2025-02-14 19:12:33

您可以使用带有其他名称的列表创建一个集合(例如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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文