Mark Needham

Thoughts on Software Development

Ruby/Python: Constructing a taxonomy from an array using zip

with 2 comments

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 ']'
	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"]]
Be Sociable, Share!

Written by Mark Needham

May 19th, 2013 at 10:44 pm

Posted in Python,Ruby

Tagged with ,

  • Anonymous

    There is also Enumerable#each_cons which can do what you’re after

  • As per charliesome’s suggestion and David Thompson on twitter – – Enumerable#each_cons does the trick:

    > taxonomy.each_cons(2).map { |x| x }
    => [[“Cat”, “SubCat”], [“SubCat”, “SubSubCat”]]