Ruby/Python: Constructing a taxonomy from an array using zip
Ruby/Python: Constructing a taxonomy from an array using zip
Join the DZone community and get the full member experience.
Join For FreeDevOps involves integrating development, testing, deployment and release cycles into a collaborative process. Learn more about the 4 steps to an effective DevSecOps infrastructure.
As I mentioned in my previous blog post I’ve been hacking on a product taxonomy and I wanted to create a ‘CHILD’ relationship between a collection of categories.
For example, I had the following array and I wanted to transform it into an array of ‘SubCategory, Category’ pairs:
taxonomy = ["Cat", "SubCat", "SubSubCat"] # I wanted this to become [("Cat", "SubCat"), ("SubCat", "SubSubCat")
In order to do this we need to zip the first 2 items with the last which I found reasonably easy to do using Python:
>>> zip(taxonomy[:-1], taxonomy[1:]) [('Cat', 'SubCat'), ('SubCat', 'SubSubCat')]
Here we using the python array slicing notation to get all but the last item of ‘taxonomy’ and then all but the first item of ‘taxonomy’ and zip them together.
I wanted to achieve that effect in Ruby though because my import job was written in that!
We can’t achieve the open ended slicing as far as I can tell so the following gives us an error:
> taxonomy[..-1] SyntaxError: (irb):10: syntax error, unexpected tDOT2, expecting ']' taxonomy[..-1] ^ from /Users/markhneedham/.rbenv/versions/1.9.3-p327/bin/irb:12:in `<main>'
The way negative indexing works is a bit different so to remove the last item of the array we use ‘-2′ rather than ‘-1′:
> taxonomy[0..-2].zip(taxonomy[1..-1]) => [["Cat", "SubCat"], ["SubCat", "SubSubCat"]]
Read the 4-part DevOps testing eBook to learn how to detect problems earlier in your DevOps testing processes.
Published at DZone with permission of Mark Needham , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}