Live data from Hacker News

Brilliant or insane code?

stavros.io

51–60 of 114 posts

Re: Brilliant or insane code?

#51
post #17

This is in the zip documentation as the way of solving this problem. Sort of surprised the author didn't look up the documentation before writing what is otherwise a very good post. The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using zip(*[iter(s)]*n). http://docs.python.org/2/library/functions.html#zip

By the way, I was recently surprised by a similar pattern in linux, where you can do:

    (echo -e "one\ntwo\nthree\nfour") | paste -d, - -
to get result of:

    one,two
    three,four
by exploiting a similar trick, i.e. reading two times ('- -') from the same iterator (STDIN of 'paste')

Re: Brilliant or insane code?

#52
post #47

Earlier quoted context omitted.

Exactly, I would be very surprised if the zip version was faster. One of the first steps to optimization in python is moving everything you can to generators and using of itertools. The OP's question of is this genius or bad is clear in that regard: it is bad, due to not being the proper optimization direction, but it is interesting.

That is a common misconception. Moving to iterators adds a function call while list creation in C is quite fast. Every case has to be tested for performance.

I always thought the reason iterators are used in preference to lists was due to the memory advantages, not the performance.

Re: Brilliant or insane code?

#53
post #4

Insane, because it relies on the zip implementation detail. If you cared about a measly factor of 4 in performance you wouldn't be using python anyway.

It's not an implementation detail, the order is guaranteed by the spec.

> by the spec

by the implementation you mean.

See: http://stackoverflow.com/questions/1094961/is-there-a-python...

Re: Brilliant or insane code?

#54
post #22

i = iter(array) return zip(i, i, i) There you go. All but neceessary magic gone with just one line more.

A much clearer solution, I would also add a comment reminding people how zip works.

Really? What's the point in explaining a standard language function that they should know, and can just Google if they don't remember?

Re: Brilliant or insane code?

#56
post #37

I'm glad Clojure has top-level support for this operation... it's quite flexible too, and the presence of partition-all makes it explicit what you should expect if the sequence doesn't evenly partition. user=> (partition 3 [1 2 3 4 5 6]) ((1 2 3) (4 5 6)) user=> (partition 3 [1 2 3 4 5 6 7]) ((1 2 3) (4 5 6)) user=> (partition-all 3 [1 2 3 4 5 6 7]) ((1 2 3) (4 5 6) (7)) user=> (partition 3 3 (repeat 0) [1 2 3 4 5 6…

FAO: iamgopal - your account has been dead for over a year for no discernible reason, only people with showdead on can see your posts.

Re: Brilliant or insane code?

#57

i = iter(array) return zip(i, i, i) There you go. All but neceessary magic gone with just one line more.

If you only ever need to chunk a list into 3-tuples, sure. If you want a general solution for chunking a list with zip and an iterator, this solution doesn't scale (look at hartror's and bjourne's posts for something that does).

Re: Brilliant or insane code?

#59
From Itertools Recipes [6]:

  def grouper(iterable, n, fillvalue=None):
      "Collect data into fixed-length chunks or blocks"
      # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
      args = [iter(iterable)] * n
      return zip_longest(*args, fillvalue=fillvalue)
- What is the most “pythonic” way to iterate over a list in chunks? [1]

- Idiomatic way to take groups of n items from a list in Python? [2]

- Python “Every Other Element” Idiom [3]

- Iterate an iterator by chunks (of n) in Python? [4]

- How do you split a list into evenly sized chunks in Python? [5]

[1]: http://stackoverflow.com/questions/434287/what-is-the-most-p...

[2]: http://stackoverflow.com/questions/2461484/idiomatic-way-to-...

[3]: http://stackoverflow.com/questions/2631189/python-every-othe...

[4]: http://stackoverflow.com/questions/8991506/iterate-an-iterat...

[5]: http://stackoverflow.com/questions/312443/how-do-you-split-a...

[6]: http://docs.python.org/3/library/itertools.html#itertools-re...

Re: Brilliant or insane code?

#60
post #22

Earlier quoted context omitted.

A much clearer solution, I would also add a comment reminding people how zip works.

Really? What's the point in explaining a standard language function that they should know, and can just Google if they don't remember?

It's using a somewhat-obscure guarantee that doesn't come up in normal usage of the function - namely, that it will always get the iterator values in left-right order.
Post reply on HN