6147

Zip 3 lists of equal length

<h3>Question</h3>

Is there a standard operation in Kotlin stdlib which would allow to iterate over a zip of 3 (or more) lists?

Effectively it should do:

list1.zip(list2).zip(list3) { (a, b), c -> listOf(a, b, c)}
<h3>Answer1:</h3>

Here are functions in the style of the standard library that do this. I'm not saying these are particularly optimized, but I think they're at least easy to understand.

/** * Returns a list of lists, each built from elements of all lists with the same indexes. * Output has length of shortest input list. */ public inline fun <T> zip(vararg lists: List<T>): List<List<T>> { return zip(*lists, transform = { it }) } /** * Returns a list of values built from elements of all lists with same indexes using provided [transform]. * Output has length of shortest input list. */ public inline fun <T, V> zip(vararg lists: List<T>, transform: (List<T>) -> V): List<V> { val minSize = lists.map(List<T>::size).min() ?: return emptyList() val list = ArrayList<V>(minSize) val iterators = lists.map { it.iterator() } var i = 0 while (i < minSize) { list.add(transform(iterators.map { it.next() })) i++ } return list }

Usage:

val list1 = listOf(1, 2, 3, 4) val list2 = listOf(5, 6) val list3 = listOf(7, 8, 9) println(zip(list1, list2, list3)) // [[1, 5, 7], [2, 6, 8]]

来源:https://stackoverflow.com/questions/50078266/zip-3-lists-of-equal-length

Recommend

  • Wrap R function in python
  • Fine Uploader to S3 Folder
  • Komodo Edit disable autocomple
  • Detect Browsers older than two years
  • How to install firefoxdriver webdriver for python3 selenium on ubuntu?
  • require() node module from Electron renderer process served over HTTP
  • programmatic textblock entry with linebreaks
  • How to use a String variable as key in a JSON object [duplicate]
  • Longest `subsequence` of balanced parentheses
  • web2py: How to execute instructions before delete using SQLFORM.smartgrid
  • Planned Contrasts on glmmTMB
  • Get last 15 lines from a large file in SFTP with phpseclib [duplicate]
  • javax.net.ssl.SSLException: SSL handshake aborted Connection reset by peer while calling webservice
  • Network communication options in Java (Client/Server)
  • mssql script data insert or update
  • read.table returning character matrix, would like numeric
  • How to modify the way a ForeignKey field is rendered in a Django admin page to avoid browser crash?
  • C++11: Why rvalue reference parameter implicitly converted to lvalue
  • playing mp3 from nsbundle
  • Slice assignment to tensorflow variable
  • Create an Office365 mailbox from within C# Web API method
  • how to specify different css for ie
  • Separating definition/instantiation of template classes without 'extern'
  • Year over Year Stats from a Crossfilter Dataset
  • How to create subsets of a single set of elements with XSLT?
  • Rotating Towards Path in OpenGL
  • Problems to understand DXGI DirectX 11 Desktop Duplication to get a Buffer or Array
  • Neo4j…how to get a visual representation of my data?
  • Change cell value based on cell color in google spreadsheet
  • Google App Engine Datastore: Dealing with eventual consistency
  • Debug `Unexpected end of JSON input Error` on content script
  • How to encrypt Connectionstring written in web.config from codebehind?
  • Bitrate JWplayer
  • calling IO Operations from thread in ruby c extension will cause ruby to hang
  • Can someone explain this Java code (formatting the output using System.out.format) to me?
  • Angular FormGroup won't update it's value immediately after patchValue or setValue
  • Firebase: How to read from external DB?
  • media foundation H264 decoder not working properly
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?
  • XSLT Transformation to validate rules in XML document