Kotlin中Python列表,集合和地图理解的等价物是什么?

Jay*_*ard 6 list-comprehension kotlin dictionary-comprehension

在Python中,有列表推导和类似的地图和集合结构.在Kotlin中,任何具有类似名称的文档都没有任何内容.

这些理解的等价物是什么?例如,Python 3 Patterns,Recipes and Idioms中的那些.其中包括对以下方面的理解:

  • 名单
  • 字典

注意: 这个问题是由作者故意编写和回答的(答案问题),因此对于常见问题的Kotlin主题的惯用答案存在于SO中.

Jay*_*ard 15

Python 3 Patterns,Recipes和Idioms为例,我们可以使用简单的模式将每个模型转换为Kotlin.列表推导的Python版本有3个部分:

  1. 输出表达式
  2. 输入列表/序列和变量
  3. 可选谓词

这些与集合类的Kotlin功能扩展直接相关.输入序列,后跟filterlambda中的可选谓词,后跟lambda中的输出表达式map.所以对于这个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 ]
Run Code Online (Sandbox Code Playgroud)

// === 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 ]  
Run Code Online (Sandbox Code Playgroud)

请注意,Kotlin版本中不需要该变量,因为隐含it变量在每个lambda中使用.在Python中,您可以使用()而不是方括号将它们转换为延迟生成器:

# === PYTHON

even_ints_squared = ( e**2 for e in a_list if e % 2 == 0 )
Run Code Online (Sandbox Code Playgroud)

在Kotlin中,通过函数调用更改输入更明显地转换为延迟序列asSequence():

// === KOTLIN

val evenIntsSquared = aList.asSequence().filter { it % 2 == 0 }.map { it * it }
Run Code Online (Sandbox Code Playgroud)

Kotlin中的嵌套理解是通过在另一个maplambda中嵌套一个来创建的.例如,从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]
Run Code Online (Sandbox Code Playgroud)

变为:

// === 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]
Run Code Online (Sandbox Code Playgroud)

请注意,嵌套的解释会生成一个列表列表,这些列表将转换为平面列表flatMap(),然后使用转换为集合toSet().此外,Kotlin范围是包容性的,而Python范围是独家的,因此您将看到数字在范围内略有不同.

您还可以在Kotlin中使用带有协同例程的生成器来生成值,而无需调用sequence:

// === 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 }
Run Code Online (Sandbox Code Playgroud)

引用的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]] 
Run Code Online (Sandbox Code Playgroud)

在科特林:

// === 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]]  
Run Code Online (Sandbox Code Playgroud)

集合理解的另一个例子是生成一组独特的正确名称:

// === KOTLIN

val matrix2 = Array(3) { row -> 
                          IntArray(3) { col -> if (col == row) 1 else 0 } 
                       }
Run Code Online (Sandbox Code Playgroud)

被翻译成Kotlin:

# === 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'}
Run Code Online (Sandbox Code Playgroud)

地图理解的例子有点奇怪,但也可以在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]
Run Code Online (Sandbox Code Playgroud)

转换后,这里写的有点"罗嗦",以便更清楚地发生了什么:

# === 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}
Run Code Online (Sandbox Code Playgroud)

进一步阅读: