6 ms·
I really like python's array slices, a[5:] b[2:3], and list comprehensions, [x * 2 for x in array if x > 0]
by fvdessen 8y ago
I really like python's array slices, a[5:] b[2:3], and list comprehensions, [x * 2 for x in array if x > 0]
- artemisyna 8y agoArray slices make array/string-parsing coding interviews so much nicer. @.@
- lotu 8y agoSo much so, I wish more languages would adopt it.
- Pete_D 8y agoI love Python comprehensions. I want to dislike Python - significant whitespace, weak lambdas, other trivial complaints - but the comprehensions are so nice, especially combined with yield and the iteration protocol, that it's usually the first language I turn to. There's syntax for sets, dicts, and generators too: >>> { char.upper() for char in "hello" } set(['H', 'E', 'L', 'O']) >>> { char: ord(char) for char in "world" } {'d': 100, 'r': 114, 'o': 111, 'w': 119, 'l': 108} >>> (char for char in "foobar").next() 'f'
- Tijdreiziger 8y agoWhat I really dislike about comprehensions is that the for syntax comes at the _end_. They would be much easier to parse if it came at the beginning.
- kjeetgill 8y agoI used to feel that way too. When you write out nested for loops the work is at the other end! For what it's worth, it is meant to resemble mathematical set builder notation. I think that familiarity turned me around on it (pun intended). From that perspective it feels more natural. [0]:https://en.m.wikipedia.org/wiki/Set-builder_notation https://en.m.wikipedia.org/wiki/Set-builder_notation
- kjeetgill 8y agoDon't forget negative indexes to wrap around. A[2:-2] trims 2 off each end!