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

问题描述 投票:5回答:1

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

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

  • 名单
  • 字典

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

kotlin list-comprehension dictionary-comprehension
1个回答
12
投票

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

  1. 输出表达式
  2. 输入列表/序列和变量
  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获取此示例稍微改变以使用set和list comprehension:

# === 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 noprimes = (2..7).map { (it*2..99).step(it).map { it } }.flatten().toSet()
val primes = (2..99).filterNot { it 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]

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

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

// === KOTLIN

val noprimes = buildSequence {
    (2..7).map { (it*2..99).step(it).forEach { yield(it) } }
}.toSet()
val primes = (2..99).filterNot { it in noprimes }

引用的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

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]]  

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

# === 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}

进一步阅读:

© www.soinside.com 2019 - 2024. All rights reserved.