[Tutor] Re: References in loops

2005-02-12 Thread Brian Beck
Andrei wrote:
Numbers are immutable, so the element 1 can't change into a 2 inside the 
list. If 1 was not immutable, e.g. a list you could modify it and then 
it would be "updated" in the original list too.
It doesn't have anything to do with mutability, only the scope of i. 
Consider a list of lists (lists are mutable):

py> l = [[1], [2], [3]]
py> for i in l:
i = [0]
py> print l
[[1], [2], [3]]
Notice that even though each i (list) is mutable, rebinding the name i 
in the loop has no effect on the list.

I tend to use list comprehensions for this:
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 maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Re: count words

2005-02-15 Thread Brian Beck
Ron Nixon wrote:
f = open('text.txt').read()
print f.count('word')
Other than using a several print statments to look for
seperate words like this, is there a way to do it so
that I get a individual count of each word:
word1 xxx
word2 xxx
words xxx
etc.
Someone else might offer a better way of finding such information in a 
file, but for now I'll assume that this method is okay and just answer 
your original question. I'd do it like this (untested):

f = open('text.txt').read()
words = ['word', 'beef', 'nachos', 'recipe']
# This makes a list of tuples like [('word', 5), ('beef', 0), ...]
# You might also consider using a dictionary.
count = [(word, f.count(word)) for word in words]
# Now print the results.
for result in count:
   print "%s was found %d times." % result
# Dictionary 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
brian will wrote:
> Hi there,
> 
> I can't explain my experience with what I like to call 'parallel
> assignment' (tuple/list assignment) inside 'for-in' loops and list
> comprehensions:

You're probably confused about what's being assigned to what inside the
comprehension. Maybe this will clear it up:

> 1)
> 
>>>>[f + g for f, g in (1, 2), (3, 4)]
> 
> [3, 7]

[(1, 2), (3, 4)] is your 'source' list, so the first time f = 1 and g =
2, then f = 3 and g = 4. I'm guessing you have no problem with the
output given.

> 2)
> 
>>>>x = 'ab'
>>>>y = 'cd'
>>>>[f + g for f, g in x, y]
> 
> ['ab', 'cd']

[x, y] is your 'source' list. For x, f = 'a' and g = 'b'. For y, f = 'c'
and g = 'd'.

> 3)
> 
>>>>f, g = '123', '456'
>>>>[f + g for f, g in '123', '456']
> 
> Traceback (most recent call last):
>   File "", line 1, in -toplevel-
> [f + g for f, g in '123', '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