【问题标题】:how to use for/ while loop to write a filter function in python?如何使用for/while循环在python中编写过滤函数?
【发布时间】:2019-11-12 00:57:36
【问题描述】:

我想编写一个循环函数,如果所有元素都等于 3,则返回 True,否则返回 False。 不使用抽象函数。

is_same([3,3,3,3,3]) => True
is_same([3,4,6]) => False

【问题讨论】:

  • 这是哪个抽象函数?我知道标准库中没有这样的东西
  • 好吧,我知道你在谈论哪个抽象函数,但是所有这 3 个解决方案都可以工作:all(x==3 for x in your_list)len(filter(lambda x: x!=3, your_list)) == 0len(set(your_list)) == 1 and your_list[0] == 3
  • 抽象函数表示过滤、映射

标签: python for-loop


【解决方案1】:

这可以工作:

all(x == 3 for x in myList)

【讨论】:

    猜你喜欢
    • 2014-11-28
    • 1970-01-01
    • 2016-01-04
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 2022-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多