[Tutor] Re: References in loops

2005-02-12 Thread Brian Beck
aList = [elem+1 for elem in aList] This is probably the best solution, provided that the operation you want to perform on each element is simple enough to fit in an expression. Even then, just put the operation into a function. -- Brian Beck Adventurer ___

[Tutor] Re: count words

2005-02-15 Thread Brian Beck
nary method: # Uses the same list comprehension, maybe there's a better way? count = dict([(word, f.count(word)) for word in words]) for key, value in count.iteritems(): print "%s was found %d times." % (key, value) -- Brian Beck Adventurer of the First Order ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor

[Tutor] Re: tuple/list assignment in 'for-in'

2005-04-27 Thread Brian Beck
'456'] > ValueError: too many values to unpack Here you're in trouble because the strings are the wrong length. Your second example worked, whether intentionally or by coincidence, because the strings were both 2 characters long, and you were unpacking them into two variables. Here you're trying to unpack 3 items ('1', '2', '3', then '4', '5', '6') into 2 variables, which doesn't work. -- Brian Beck Adventurer of the First Order ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor