Python奇怪的答案(带有计数函数的元组和列表)

发布于 2025-01-14 08:04:30 字数 236 浏览 2 评论 0原文

这是下面给出的代码。

k = [1, 8, 15]
g = (x for x in k if k.count(x) > 0)
k = [2, 8, 22]
print(list(g))

我得到的输出为 [8],但它应该是 [1,8,15],对吧?因为每个元素至少出现一次。

对答案有什么合理的解释吗?

This is the code given below.

k = [1, 8, 15]
g = (x for x in k if k.count(x) > 0)
k = [2, 8, 22]
print(list(g))

I am getting the output as [8] but it should be [1,8,15], right? since each element is present at least once.

Any plausible explanation for the answer?

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

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

发布评论

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

评论(4

枫林﹌晚霞¤ 2025-01-21 08:04:30

这是一个生成器表达式。它创建一个生成器,而不是一个元组。

生成器表达式的确切一部分是在 genexp 创建时计算的。就是这部分:

g = (x for x in k if k.count(x)>0)
#               ^

其他所有内容,包括这部分:

g = (x for x in k if k.count(x)>0)
#                    ^

都是惰性评估的。

这意味着您循环的 k 是原始的 k,但是您调用的 k count on 是新的k

That's a generator expression. It creates a generator, not a tuple.

Exactly one part of a generator expression is evaluated at genexp creation time. It's this part:

g = (x for x in k if k.count(x)>0)
#               ^

Everything else, including this part:

g = (x for x in k if k.count(x)>0)
#                    ^

is evaluated lazily.

That means the k you're looping over is the original k, but the k you're calling count on is the new k.

时光病人 2025-01-21 08:04:30

这里有一个技巧,可以通过在生成器表达式中打印 xk 来查看哪个 k 使用原始/修改后的列表:

1st k< /code> 指的是原始列表:

>>> k = [1,8,15]
>>> g = (x for x in k if (print(x) == None and k.count(x)>0))
>>> k = [2,8,22]
>>> list(g)
1
8
15

第二个 k 指的是修改后的列表:

>>> k = [1,8,15]
>>> g = (x for x in k if (print(k) == None and k.count(x)>0))
>>> k = [2,8,22]
>>> list(g)
[2, 8, 22]
[2, 8, 22]
[2, 8, 22]

Here's a trick to see which k uses the original/modified list by printing x and k in the generator expression:

1st k refers to the original list:

>>> k = [1,8,15]
>>> g = (x for x in k if (print(x) == None and k.count(x)>0))
>>> k = [2,8,22]
>>> list(g)
1
8
15

2nd k refers to the modified list:

>>> k = [1,8,15]
>>> g = (x for x in k if (print(k) == None and k.count(x)>0))
>>> k = [2,8,22]
>>> list(g)
[2, 8, 22]
[2, 8, 22]
[2, 8, 22]
烟花易冷人易散 2025-01-21 08:04:30

调试技巧类似于 Shawn 的:

def p(label, x):
    print(label, x)
    return x

k = [1,8,15]
g = (x for x in p('iterate over', k) if p('count in', k).count(x)>0)
k = [2,8,22]
print('between generator construction and consumption')
list(g)

Output (在线试用!):

iterate over [1, 8, 15]
between generator construction and consumption
count in [2, 8, 22]
count in [2, 8, 22]
count in [2, 8, 22]

Debugging trick similar to Shawn's:

def p(label, x):
    print(label, x)
    return x

k = [1,8,15]
g = (x for x in p('iterate over', k) if p('count in', k).count(x)>0)
k = [2,8,22]
print('between generator construction and consumption')
list(g)

Output (Try it online!):

iterate over [1, 8, 15]
between generator construction and consumption
count in [2, 8, 22]
count in [2, 8, 22]
count in [2, 8, 22]
我做我的改变 2025-01-21 08:04:30

生成器在进程内存上获取,像惰性运算符一样工作,这样

对于 [1,8,15] 中的 x 会发生“””“”:
if [2,8,22].count(x): """"
这就是解释器获取值的方式。

请参阅此推文以了解更多信息:

https://twitter.com/nedbat/status/1503735148378001410?t=lJLT482Vmt19cBNuP-PAWQ&s=19

https://twitter.com/driscollis/status/1503366898234404866?t=OgCM0E1rzWAEQOOziO9uqA&s=19

#带着希望学习

Generator is fetch on process memory,work like lazy operator so that happen

"""""for x in [1,8,15]:
if [2,8,22].count(x): """"
That is how interpreter fetch values.

Refer this tweet for more understand:

https://twitter.com/nedbat/status/1503735148378001410?t=lJLT482Vmt19cBNuP-PAWQ&s=19

https://twitter.com/driscollis/status/1503366898234404866?t=OgCM0E1rzWAEQOOziO9uqA&s=19

#Learning with hope

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