count()方法计数错误

发布于 2025-02-05 06:59:54 字数 234 浏览 2 评论 0原文

这是我解决的任务。

对于整数列表,查找并打印出现在列表中的项目 只有一次。项目必须按照顺序打印 在来源列表中。

我编写了代码,但它也将两个数字和三位数的数字视为一个数字。有什么问题?

x = []
a = input()
a.split(" ")
for i in a:
    if a.count(i) == 1:
        x.append(i)
print(x)

Here is my task to solve.

For a list of integers, find and print the items that appear in the list
only once. Items must be printed in the order in which they are
are in the incoming list.

I wrote the code but it also counts two and three digit numbers as a single digits. What's the problem?

x = []
a = input()
a.split(" ")
for i in a:
    if a.count(i) == 1:
        x.append(i)
print(x)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

心房的律动 2025-02-12 06:59:55

您可以分配上述变量,也可以直接在循环中使用拆分函数。

x = []
a = input()
for i in a.split(" "):
    if a.count(i) == 1:
        x.append(i)
print(x)

或在阅读输入时分开。

x = []
a = input().split(" ")
for i in a:
    if a.count(i) == 1:
        x.append(i)
print(x)

You can assign the variable as mentioned above or you can directly use the split function in the loop.

x = []
a = input()
for i in a.split(" "):
    if a.count(i) == 1:
        x.append(i)
print(x)

or split when you are reading the input.

x = []
a = input().split(" ")
for i in a:
    if a.count(i) == 1:
        x.append(i)
print(x)
会傲 2025-02-12 06:59:55

collections 模块中使用计数类是正确的方法,但这是另一种方法,而没有任何进口,您有效地实现了自己的计数器:

a = '1 2 3 4 4 5 6 7 7 7'

d = {}

for n in a.split():
    d[n] = d.get(n, 0) + 1

for k, v in d.items():
    if v == 1:
        print(k)

输出:

1
2
3
5
6

Using the Counter class from the collections module is the right way to do this but here's another way without any imports where you effectively have your own limited implementation of a Counter:

a = '1 2 3 4 4 5 6 7 7 7'

d = {}

for n in a.split():
    d[n] = d.get(n, 0) + 1

for k, v in d.items():
    if v == 1:
        print(k)

Output:

1
2
3
5
6
梦行七里 2025-02-12 06:59:54

用户机械猪提供答案,split不适当地工作,因为您要丢弃其结果,而不是字符串中的每个字符的数量,而不是每个空间分隔的数字子弦。

标准库还包含一个为此用例准备的集合: collections.counter 。您可以给它一系列项目,然后对计数进行整理,然后您可以过滤:

print([k for k, v in collections.Counter(a.split()).items() if v == 1])

User Mechanical Pig provides the answer, split does not work in-place, since you're discarding its result what you're looking at is the count of each character in the string, not each space-separated sub-string.

The standard library also contains a collection which is ready-made for this use-case: collections.Counter. You can just give it a sequence of items and it'll collate the counts, which you can then filter:

print([k for k, v in collections.Counter(a.split()).items() if v == 1])
一城柳絮吹成雪 2025-02-12 06:59:54

a.split(“”)不会更改a的值,因此它仍然是字符串而不是列表。因此,当您迭代a时,只会获得单个字符。

str.split返回列表的方法,但不会神奇地将字符串变成列表。因此,您需要将其返回的值分配给变量(例如,如果您不想保留输入字符串)。

a.split(" ") does not change the value of a, so it is still a string and not a list. Hence when you iterate over a, you only get single characters.

str.split is a method that returns a list, but does not magically turn the string into a list. So you need to assign the value it returns to a variable (for example, to a, if you don't want to hold on to the input string).

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