以Python 3 Patterns, Recipes and Idioms 中的示例为例,我们可以使用简单的模式将每个示例转换为 Kotlin。 Python 版本的列表推导包含 3 个部分:
- 输出表达式
- 输入列表/序列和变量
- 可选谓词
这些与 Kotlin 对集合类的功能扩展直接相关。输入序列,后跟 filter lambda 中的可选谓词,后跟 map lambda 中的输出表达式。所以对于这个 Python 示例:
# === PYTHON
a_list = [1, 2, 3, 4, 5, 6]
# output | var | input | filter/predicate
even_ints_squared = [ e*e for e in a_list if e % 2 == 0 ]
print(even_ints_squared)
# output: [ 4, 16, 36 ]
变成
// === KOTLIN
var aList = listOf(1, 2, 3, 4, 5, 6)
// input | filter | output
val evenIntsSquared = aList.filter { it % 2 == 0 }.map { it * it }
println(evenIntsSquared)
// output: [ 4, 16, 36 ]
请注意,在 Kotlin 版本中不需要该变量,因为在每个 lambda 中都使用了隐含的 it 变量。在 Python 中,您可以使用 () 而不是方括号将它们变成惰性生成器:
# === PYTHON
even_ints_squared = ( e**2 for e in a_list if e % 2 == 0 )
在 Kotlin 中,通过函数调用 asSequence() 更改输入,它更明显地转换为惰性序列:
// === KOTLIN
val evenIntsSquared = aList.asSequence().filter { it % 2 == 0 }.map { it * it }
Kotlin 中的嵌套推导是通过将一个嵌套在另一个的 map lambda 中创建的。例如,从 Python 中的PythonCourse.eu 中获取这个示例,稍微更改为同时使用集合和列表推导:
# === PYTHON
noprimes = {j for i in range(2, 8) for j in range(i*2, 100, i)}
primes = [x for x in range(2, 100) if x not in noprimes]
print(primes)
# output: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
变成:
// === KOTLIN
val nonprimes = (2..7).flatMap { (it*2..99).step(it).toList() }.toSet()
val primes = (2..99).filterNot { it in nonprimes }
print(primes)
// output: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
请注意,嵌套推导会生成一个列表列表,该列表使用flatMap() 转换为平面列表,然后使用toSet() 转换为集合。此外,Kotlin 范围是包容性的,而 Python 范围是专有的,因此您会看到范围中的数字略有不同。
您还可以在 Kotlin 中使用带有协同程序的 sequence 生成器来生成值,而无需调用 flatMap() 或 flatten():
// === KOTLIN
val nonprimes = sequence {
(2..7).forEach { (it*2..99).step(it).forEach { value -> yield(value) } }
}.toSet()
val primes = (2..99).filterNot { it in nonprimes }
引用的 Python 页面中的另一个示例是生成矩阵:
# === PYTHON
matrix = [ [ 1 if item_idx == row_idx else 0 for item_idx in range(0, 3) ] for row_idx in range(0, 3) ]
print(matrix)
# [[1, 0, 0],
# [0, 1, 0],
# [0, 0, 1]]
在 Kotlin 中:
// === KOTLIN
val matrix = (0..2).map { row -> (0..2).map { col -> if (col == row) 1 else 0 }}
println(matrix)
// [[1, 0, 0],
// [0, 1, 0],
// [0, 0, 1]]
或者在 Kotlin 中代替列表,您也可以生成数组:
// === KOTLIN
val matrix2 = Array(3) { row ->
IntArray(3) { col -> if (col == row) 1 else 0 }
}
集合推导的另一个例子是生成一组唯一的正确大小写的名称:
# === PYTHON
names = [ 'Bob', 'JOHN', 'alice', 'bob', 'ALICE', 'J', 'Bob' ]
fixedNames = { name[0].upper() + name[1:].lower() for name in names if len(name) > 1 }
print(fixedNames)
# output: {'Bob', 'Alice', 'John'}
被翻译成 Kotlin:
// === KOTLIN
val names = listOf( "Bob", "JOHN", "alice", "bob", "ALICE", "J", "Bob" )
val fixedNames = names.filter { it.length > 1 }
.map { it.take(1).toUpperCase() + it.drop(1).toLowerCase() }
.toSet()
println(fixedNames)
// output: [Bob, John, Alice]
而且地图理解的例子有点奇怪,但也可以在 Kotlin 中实现。原文:
# === PYTHON
mcase = {'a':10, 'b': 34, 'A': 7, 'Z':3}
mcase_frequency = { k.lower() : mcase.get(k.lower(), 0) + mcase.get(k.upper(), 0) for k in mcase.keys() }
print(mcase_frequency)
# output: {'a': 17, 'z': 3, 'b': 34}
以及转换后的内容,这里写得更“罗嗦”,以使发生的事情更清楚:
// === KOTLIN
val mcase = mapOf("a" to 10, "b" to 34, "A" to 7, "Z" to 3)
val mcaseFrequency = mcase.map { (key, _) ->
val newKey = key.toLowerCase()
val newValue = mcase.getOrDefault(key.toLowerCase(), 0) +
mcase.getOrDefault(key.toUpperCase(), 0)
newKey to newValue
}.toMap()
print(mcaseFrequency)
// output: {a=17, b=34, z=3}
进一步阅读: