从布尔列表中获取所有索引

发布于 2025-01-13 01:02:22 字数 176 浏览 3 评论 0原文

我的一段代码有问题。我有一个布尔列表,需要知道哪个索引是真的。

lst = [True, True, False]

for element in lst:
 print(lst.index(element))

为什么输出是0,0,2?

我只需要输出0,1,2

I have a problem with a pice of my code. I have a boolean list and need to know which index is true.

lst = [True, True, False]

for element in lst:
 print(lst.index(element))

why the output is 0,0,2?

I just need output 0,1,2

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

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

发布评论

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

评论(2

停滞 2025-01-20 01:02:22

这样做。

lst = [True, True, False]

for idx, val in enumerate(lst):
    if val == True:
        print(idx)

输出

0
1

Do this.

lst = [True, True, False]

for idx, val in enumerate(lst):
    if val == True:
        print(idx)

The output

0
1
萌辣 2025-01-20 01:02:22

这只会打印真实的,所以我不确定它是否正是您所需要的。如果您提供更多背景信息,我们可能会帮助您为您的具体问题获得更好的答案。

lst = [True, True, False]

for element in lst:
    if element == True:
        print(element)

This will print only the true, so I'm not sure if its exactly what you need. If you provide more context we could probably help get a better answer for your specific problem.

lst = [True, True, False]

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