Skip to content

Instantly share code, notes, and snippets.

@narutaro
Last active February 12, 2016 22:25
Show Gist options
  • Save narutaro/28cc0ac5a7f44c6f3a9d to your computer and use it in GitHub Desktop.
Save narutaro/28cc0ac5a7f44c6f3a9d to your computer and use it in GitHub Desktop.
Grumbles of a rubyist who tries to learn python

Grumbles of a rubyist who tries to learn python

Create a unique list of lists/array of arrays - the winner is ruby

ruby

[1] pry(main)> a = [[1, 2],[1,2],[3,4]]
=> [[1, 2], [1, 2], [3, 4]]
[2] pry(main)> a.uniq
=> [[1, 2], [3, 4]]

python

>>> a = [[1,2],[1,2],[3,4]]
>>> set(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

Certainly, tuple works.

>>> b = [(1,2),(1,2),(3,4)]
>>> set(b)
set([(1, 2), (3, 4)])

but I want to add/delete items. A tuple does not support it. I had to convert the inner list to tuple. :(

>>> a = [[1,2],[1,2],[3,4]]
>>> b = []
>>> for i in a:
...   b.append(tuple(i))
...
>>> set(b)
set([(1, 2), (3, 4)])

method chain

here

array.flatten

ruby

array.flatten

python

dead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment