Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否有与';过滤器';Python中的函数?_Python_Swift_Unicode_Lambda_Swift Playground - Fatal编程技术网

是否有与';过滤器';Python中的函数?

是否有与';过滤器';Python中的函数?,python,swift,unicode,lambda,swift-playground,Python,Swift,Unicode,Lambda,Swift Playground,在python中,使用“filter”函数从字符串/列表中删除不需要的项非常简单,该函数可以与“lambda”函数结合使用。在python中,它非常简单: a = "hello 123 bye-bye !!£$%$%" b = list(filter(lambda x: x.isalpha(), a)) c = "".join(b) print(c) #Which would print "hellobyebye" 有没有什么方法可以在swift中轻松地复制这一点,而无需先转换为unicode

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

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

有没有什么方法可以在swift中轻松地复制这一点,而无需先转换为unicode,然后检查unicode值是否在某个范围内?另外,swift中是否有类似“lambda”的东西?

是的,swift中有一个等价的
过滤器
函数:

过滤器

filter方法采用一个函数(includeElement),给定 元素,返回一个Bool,指示该元素 应包含在结果数组中。例如,删除所有 数字数组中的奇数可以这样做:

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

更多关于

注释可能重复的信息:上面链接中的列表理解包括一个过滤表达式。我不能100%确定这将如何工作。你能详细说明一下吗?我觉得相关的问题和答案非常清楚,我已经学习了大约五分钟的Swift(针对这个问题)。我认为你需要花一些时间在或tutorial.btw,
'.join(如果x.isalpha()中的x代表x)
。在某些情况下,我使用
map
filter
,但如果你将它们与lambda一起使用,那么同等的理解力几乎总是读得更好。“0美元”有什么作用?这是否作为每个列表元素的占位符?请参阅“速记参数名称”部分下的