简体   繁体   English

Golang相当于Python的列表理解

[英]Golang equivalent for Python's list comprehension

I am playing with Go but I am having a very hard time doing things that are very simple in other languages. 我正在玩Go,但我很难做其他语言非常简单的事情。

I'd like to reproduce a similar syntax: 我想重现一个类似的语法:

array = [a for a in anotherArray  if (some condition)]

What is an elegant way to do it in Go? Go的优雅方式是什么? I'd really like to simplify my code especially when using function on array. 我真的很想简化我的代码,尤其是在使用数组上的函数时。 For example: 例如:

min = min(abs(a[i], b[j]) for i in range(n)
                          for j in range(i, n))

Many thanks 非常感谢

Interestingly enough, Rob Pike just proposed (18 hours ago) the library filter which does a bit what you want: 有趣的是, Rob Pike刚刚提出(18小时前)库过滤器 ,它可以满足您的需求:

See for instance Choose() 参见例如选择()

// Choose takes a slice of type []T and a function of type func(T) bool. (If
// the input conditions are not satisfied, Choose panics.) It returns a newly
// allocated slice containing only those elements of the input slice that
// satisfy the function.

Tested here : 在这里测试

func TestChoose(t *testing.T) {
    a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
    expect := []int{2, 4, 6, 8}
    result := Choose(a, isEven)

As twotwotwo points out in the comments , the GoDoc for this library states: 正如twotwotwo 在评论中指出的那样,该库GoDoc指出:

Package filter contains utility functions for filtering slices through the distributed application of a filter function. filter包含实用程序功能,用于通过过滤功能的分布式应用程序过滤切片。

The package is an experiment to see how easy it is to write such things in Go. 该软件包是一个实验,看看在Go中编写这样的东西是多么容易。 It is easy, but for loops are just as easy and more efficient . 这很容易,但是for循环来说同样简单且高效

You should not use this package. 你不应该使用这个包。

This caveat is reflected in the document " Summary of Go Generics Discussions ", section " Functional Code ": 这个警告反映在文档“ Go Generics Discussions ”中,“ 功能代码 ”部分:

These are the usual higher-order functions such as map , reduce ( fold ), filter , zip etc. 这些是通常的高阶函数 ,如mapreducefold ), filterzip等。

Cases : 案例
typesafe data transformations: map , fold , zip 类型安全数据转换: mapfoldzip

Pros for using generics : 使用泛型的优点
Concise way to express data transformations. 表达数据转换的简洁方法。

Cons for using generics : 使用泛型的缺点
The fastest solution needs to take into account when and which order to apply those transformations, and how much data is generated at each step. 最快的解决方案需要考虑应用这些转换的时间和顺序,以及每个步骤生成的数据量。
It is harder to read for beginners. 初学者更难阅读。

Alternative solutions : 替代方案

use for loops and usual language constructs . for循环和通常的语言结构

If what you are looking is indeed python list comprehension, there is no such syntactic equivalent in go AFAIK. 如果您正在寻找的确是python列表理解,那么在AFAIK中就没有这样的语法等价物。

The way to do it is to create a function that take a slice and a function (to test the condition) and return a new slice. 这样做的方法是创建一个函数,该函数采用切片和函数(以测试条件)并返回一个新切片。

EDIT: Well looks like there is already such a feature in Go. 编辑:看起来Go中已经有这样的功能了。 cf VonC cf VonC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM