python - 是否有 Swift 等同于 Python 中的 'Filter' 函数?

标签 python swift unicode lambda swift-playground

在 python 中,使用可以与“lambda”函数结合使用的“filter”函数从字符串/列表中删除不需要的项目非常简单。在 python 中,它很简单:

a = "hello 123 bye-bye !!£$%$%"
b = list(filter(lambda x: x.isalpha(), a))
c = "".join(b)
print(c) #Which would print "hellobyebye"

有什么方法可以在不先转换为 unicode 然后检查 unicode 值是否在特定范围内的情况下轻松快速地复制它?另外,swift 中是否有类似“lambda”的东西?

最佳答案

是的,在 Swift 中有一个等效的 Filter 函数:

Filter

The filter method takes a function (includeElement) which, given an element in the array, returns a Bool indicating whether the element should be included in the resulting array. For example, removing all the odd numbers from the numbers array could be done like this:

let numbers = [ 10000, 10303, 30913, 50000, 100000, 101039, 1000000 ]
let evenNumbers = numbers.filter { $0 % 2 == 0 }
// [ 10000, 50000, 100000, 1000000 ]

更多关于 Map, Filter and Reduce in Swift

关于python - 是否有 Swift 等同于 Python 中的 'Filter' 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32147648/

相关文章:

python - 如何在保持顺序的同时在生成器上使用线程(每个项目多个线程)?

python - 向量化/优化此 python 代码的最佳方法是什么?

python - 如何从颜色条图像中提取颜色图并将其用于热图中

ios - 如何使用GraphicContext快速获得书法线条笔刷效果

python - 打开文本文档时出现问题 - Unicode 错误

python的**kwargs效率

swift - 使用 Timer 发送到实例的无法识别的选择器

swift - 如何在 swift3 中设置 AVMetadataItem 的标识符?

python - unicodedata.normalize 缺少一个字符进行转换

c++ - 我如何能够在 1 字节的普通字符串中存储日文字符?