[_] Python Question
Rick Hurst
rick.hurst at gmail.com
Fri Dec 18 09:45:00 GMT 2009
On Thu, Dec 17, 2009 at 3:51 PM, Andy Gale <andy at mentalist.co.uk> wrote: > Hi _, > > I know there's a few Python goons on here so... is there a better way > of doing this in Python? > > attributes = {} > > attribute_data = mark.fetchall() > > for i in attribute_data: > > if i[1] in attributes: > attributes[i[1]].append(i[0]) > else: > attributes[i[1]] = [i[0]] > > Given my experience with Python so far, there is probably some epic > shortcut for doing this. Just to throw a bit of general programming debate in here, in my mind (as mainly a front-end developer who gets his hands only as dirty as necessary with back-end stuff), there is nothing worse than when a proper programmer replaces your naive but readable logic with an epic shortcut. The amount of times i've done something readable and easily debugged like:- for foo in bar: if blah then: eggs #helpful comment else if gravy then ham #another helpful comment else cheese Then a proper programmer sais "there's no need to write so many lines of code, you can do it in one line of code like this":- foo:bar[alpha in {romeo xfactor/veg}]cheese[x][x].eggs() above example is completely made up, so don't try to debug it - in fact even if it wasn't made up, don't try to debug it, because you won't be able to (being my point). -- Rick Hurst, Freelance Web Developer, Bristol, UK http://olivewoodstudio.com http://www.rickhurst.co.uk http://twitter.com/rickhurst