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