Swift中的列表理解 [英] List comprehension in Swift

查看:68
本文介绍了Swift中的列表理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

语言指南显示没有列表理解的痕迹. 在Swift中最简单的方法是什么?我正在寻找类似于以下内容的东西:

The language guide has revealed no trace of list comprehension. What's the neatest way of accomplishing this in Swift? I'm looking for something similar to:

evens = [ x for x in range(10) if x % 2 == 0]

推荐答案

从Swift 2.x开始,您的Python样式列表理解有一些简短的等效内容.

As of Swift 2.x, there are a few short equivalents to your Python-style list comprehension.

Python公式最直接的改编(读取类似将转换应用于要过滤的序列"之类的内容)涉及链接所有SequenceType可用的mapfilter方法,并从Range:

The most straightforward adaptations of Python's formula (which reads something like "apply a transform to a sequence subject to a filter") involve chaining the map and filter methods available to all SequenceTypes, and starting from a Range:

// Python: [ x for x in range(10) if x % 2 == 0 ]
let evens = (0..<10).filter { $0 % 2 == 0 }

// Another example, since the first with 'x for x' doesn't
// use the full ability of a list comprehension:
// Python: [ x*x for x in range(10) if x % 2 == 0 ]
let evenSquared = (0..<10).filter({ $0 % 2 == 0 }).map({ $0 * $0 })

请注意,Range是抽象的-它实际上并不创建您要求的值的完整列表,而只是按需懒惰地提供它们的构造. (从某种意义上讲,它更像是Python的xrange.)但是,filter调用返回一个Array,因此您在此处失去了懒惰"方面.如果您想让收藏一直保持懒散,请这样说:

Note that a Range is abstract — it doesn't actually create the whole list of values you ask it for, just a construct that lazily supplies them on demand. (In this sense it's more like Python's xrange.) However, the filter call returns an Array, so you lose the "lazy" aspect there. If you want to keep the collection lazy all the way through, just say so:

// Python: [ x for x in range(10) if x % 2 == 0 ]
let evens = (0..<10).lazy.filter { $0 % 2 == 0 }
// Python: [ x*x for x in range(10) if x % 2 == 0 ]
let evenSquared = (0..<10).lazy.filter({ $0 % 2 == 0 }).map({ $0 * $0 })

与Python中的列表理解语法(以及某些其他语言中的类似构造)不同,Swift中的这些操作遵循与其他操作相同的语法.也就是说,构造,过滤和操作一系列数字与过滤和操作对象数组具有相同的语法样式—您不必为一种工作使用函数/方法语法并列出另一种的理解语法.

Unlike the list comprehension syntax in Python (and similar constructs in some other languages), these operations in Swift follow the same syntax as other operations. That is, it's the same style of syntax to construct, filter, and operate on a range of numbers as it is to filter and operate on an array of objects — you don't have to use function/method syntax for one kind of work and list comprehension syntax for another.

您可以将其他函数传递给filtermap调用,并链接其他方便的转换,例如sortreduce:

And you can pass other functions in to the filter and map calls, and chain in other handy transforms like sort and reduce:

// func isAwesome(person: Person) -> Bool
// let people: [Person]
let names = people.filter(isAwesome).sort(<).map({ $0.name })

let sum = (0..<10).reduce(0, combine: +)

但是,根据您要做什么,可能会有更简洁的方式表达您的意思.例如,如果您特别想要偶数列表,则可以使用stride:

Depending on what you're going for, though, there may be more concise ways to say what you mean. For example, if you specifically want a list of even integers, you can use stride:

let evenStride = 0.stride(to: 10, by: 2) // or stride(through:by:), to include 10

与范围类似,这将为您提供一个生成器,因此您需要从中生成一个Array或对其进行迭代以查看所有值:

Like with ranges, this gets you a generator, so you'll want to make an Array from it or iterate through it to see all the values:

let evensArray = Array(evenStride) // [0, 2, 4, 6, 8]

为Swift 2.x进行了大幅修订.如果需要Swift 1.x,请查看编辑历史记录.

Heavily revised for Swift 2.x. See the edit history if you want Swift 1.x.

这篇关于Swift中的列表理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆