[Tutor] Print record x in a file

2005-01-24 Thread David Holland
Kent,
 
Yes you are right.  I looked at your code and that makes sense now.
Thanks for explaining that.
 
		 ALL-NEW 
Yahoo! Messenger 
- all new features - even more fun! 
 ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Print record x in a file

2005-01-23 Thread Kent Johnson
Max Noel wrote:
On Jan 23, 2005, at 22:08, Liam Clarke wrote:
Don't you mean
x=random.randint(0, lenoflist) ?? I'm assuming you want an integer.

random.randrange() returns an item (which can be a float or 
whatever, but by default is an int) in the specified range. In that 
case, an int between 0 and lenoflist.
The advantage over random.randint is that you can specify a step. Thus,
random.randrange(0, 257, 2) will return an even number between 0 and 256 
inclusive.
Also the output of randint() is inclusive of both endpoints - with the 
above statement you will have
0 <= x <= lenoflist. So in that case you would want lenoflist = 
len(listcontents) - 1
Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Print record x in a file

2005-01-23 Thread Max Noel
On Jan 23, 2005, at 22:08, Liam Clarke wrote:
Don't you mean
x=random.randint(0, lenoflist) ?? I'm assuming you want an integer.
	random.randrange() returns an item (which can be a float or whatever, 
but by default is an int) in the specified range. In that case, an int 
between 0 and lenoflist.
	The advantage over random.randint is that you can specify a step. Thus,
random.randrange(0, 257, 2) will return an even number between 0 and 
256 inclusive.

While although this code below does work many times
nothing is printed out.
import random
i = 0
while i < 10:
file = open('test.rantxt')
listcontents = file.readlines()
file.close()
lenoflist = len(listcontents)#-1
x = random.randrange(0,lenoflist)
print listcontents[x], i
i = i +1
	Anyway, the problem with this function is that len returns a number of 
items, but Python, like most good programming languages (and 
mathematicians), counts starting from 0. Thus, the first element in a 
list is foo[0].
	foo[len(foo)] will raise an IndexError.

-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting 
and sweating as you run through my corridors... How can you challenge a 
perfect, immortal machine?"

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Print record x in a file

2005-01-23 Thread Liam Clarke
Don't you mean 

x=random.randint(0, lenoflist) ?? I'm assuming you want an integer.


On Sun, 23 Jan 2005 21:55:27 + (GMT), David Holland
<[EMAIL PROTECTED]> wrote:
>  --- [EMAIL PROTECTED] wrote:
> > Send Tutor mailing list submissions to
> >  tutor@python.org
> >
> > To subscribe or unsubscribe via the World Wide Web,
> > visit
> >  http://mail.python.org/mailman/listinfo/tutor
> > or, via email, send a message with subject or body
> > 'help' to
> >  [EMAIL PROTECTED]
> >
> > You can reach the person managing the list at
> >  [EMAIL PROTECTED]
> >
> > When replying, please edit your Subject line so it
> > is more specific
> > than "Re: Contents of Tutor digest..."
> >
> >
> > Today's Topics:
> >
> >1. Re: Print record x in a file (Kent Johnson)
> >2. How would python messure up in performance?
> > (Kevin)
> >3. Re: How would python messure up in
> > performance? (Max Noel)
> >4. Re: Print record x in a file (David Holland)
> >5. on the way to find pi (Ali Polatel)
> >6. Re: on the way to find pi (Max Noel)
> >7. Re: on the way to find pi (Orri Ganel)
> >
> >
> >
> --
> >
> > Message: 1
> > Date: Sun, 23 Jan 2005 07:46:08 -0500
> > From: Kent Johnson <[EMAIL PROTECTED]>
> > Subject: Re: [Tutor] Print record x in a file
> > Cc: tutor python 
> > Message-ID: <[EMAIL PROTECTED]>
> > Content-Type: text/plain; charset=ISO-8859-1;
> > format=flowed
> >
> >   Since len(listcontents) is one greater than the
> > largest valid index of listcontents, the correct
> > use of randrange() for this problem is
> >x = random.randrange(0, len(listcontents))
> >
> 
> 
> Kent,
> 
> I know that you know far more about python than me but
> is that right ?
> I created file with 4 records and this code did print
> them all randomly:-
> 
> import random
> i = 0
> while i < 10:
> file = open('filename')
> listcontents = file.readlines()
> file.close()
> lenoflist = len(listcontents)-1
> x = random.randrange(0,lenoflist)
> print listcontents[x], i
> i = i +1
> 
> While although this code below does work many times
> nothing is printed out.
> import random
> i = 0
> while i < 10:
> file = open('test.rantxt')
> listcontents = file.readlines()
> file.close()
> lenoflist = len(listcontents)#-1
> x = random.randrange(0,lenoflist)
> print listcontents[x], i
> i = i +1
> 
> 
> ___
> ALL-NEW Yahoo! Messenger - all new features - even more fun! 
> http://uk.messenger.yahoo.com
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Print record x in a file

2005-01-23 Thread David Holland
 --- [EMAIL PROTECTED] wrote: 
> Send Tutor mailing list submissions to
>  tutor@python.org
> 
> To subscribe or unsubscribe via the World Wide Web,
> visit
>  http://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body
> 'help' to
>  [EMAIL PROTECTED]
> 
> You can reach the person managing the list at
>  [EMAIL PROTECTED]
> 
> When replying, please edit your Subject line so it
> is more specific
> than "Re: Contents of Tutor digest..."
> 
> 
> Today's Topics:
> 
>1. Re: Print record x in a file (Kent Johnson)
>2. How would python messure up in performance?
> (Kevin)
>3. Re: How would python messure up in
> performance? (Max Noel)
>4. Re: Print record x in a file (David Holland)
>5. on the way to find pi (Ali Polatel)
>6. Re: on the way to find pi (Max Noel)
>7. Re: on the way to find pi (Orri Ganel)
> 
> 
>
------
> 
> Message: 1
> Date: Sun, 23 Jan 2005 07:46:08 -0500
> From: Kent Johnson <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] Print record x in a file
> Cc: tutor python 
> Message-ID: <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset=ISO-8859-1;
> format=flowed
> 
>   Since len(listcontents) is one greater than the
> largest valid index of listcontents, the correct 
> use of randrange() for this problem is
>x = random.randrange(0, len(listcontents))
> 


Kent,

I know that you know far more about python than me but
is that right ?
I created file with 4 records and this code did print
them all randomly:-

import random
i = 0
while i < 10:
file = open('filename')
listcontents = file.readlines()
file.close()
lenoflist = len(listcontents)-1
x = random.randrange(0,lenoflist)
print listcontents[x], i
i = i +1


While although this code below does work many times
nothing is printed out.
import random
i = 0
while i < 10:
file = open('test.rantxt')
listcontents = file.readlines()
file.close()
lenoflist = len(listcontents)#-1
x = random.randrange(0,lenoflist)
print listcontents[x], i
i = i +1





___ 
ALL-NEW Yahoo! Messenger - all new features - even more fun! 
http://uk.messenger.yahoo.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Print record x in a file

2005-01-23 Thread David Holland
 --- "Jacob S." <[EMAIL PROTECTED]> wrote: 
> > This will get a random record
> > I hope you do not think the comments are
> patronising
> > but you did say you are new so I did not want to
> give
> > naked code.
> >
> > import random
> > #the above gives the program the ability to get a
> > #pseudo random number
> > file = open('test.rantxt')
> > listcontents = file.readlines()
> > #gives you the file as a list of records or it did
> on
> > #(assuming each line is a record)
> > file.close()
> > lenoflist = len(listcontents)-1
> > #find the length of the list and take one of
> because
> > computers count from 0
> 
> Yes, but len returns counting from 1.
> Anyway, you would have to add one to correct that
> anyway, wouldn't you?
As len(list) gives the length and when the elements of
the list are number for element 0 onwards, the last
element is number len(list) -1.   Test yourself
anyway.
Randrange does gives an integer or it did when I
tested this as otherwise it would have errored.
I don't know if it always does, what do other people
think ?
However Kent's idea of random.choice(list) is better
than my idea as it is one less line of code !!

.
If you have a length of 5
> If randrange is start <= x *<=* end, then you don't
> have to add one, you
> just use the length.
> If randrange is start<= x < end like __builtin__
> range, you have to put
> randrange(1,lenoflist+1)
> 
> > x = random.randrange(0,lenoflist)
> 
> I would use randint because list indices need to be
> integers -- unless of
> course I mistaken and
> randrange returns an integer also. (But that would
> be repetitive to have to
> functions do the same thing)
> 
> > print listcontents[x]
> 
> HTH,
> Jacob
> 
>  





___ 
ALL-NEW Yahoo! Messenger - all new features - even more fun! 
http://uk.messenger.yahoo.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Print record x in a file

2005-01-23 Thread Kent Johnson
Jacob S. wrote:
import random
#the above gives the program the ability to get a
#pseudo random number
file = open('test.rantxt')
listcontents = file.readlines()
#gives you the file as a list of records or it did on
#(assuming each line is a record)
file.close()
lenoflist = len(listcontents)-1
#find the length of the list and take one of because
computers count from 0

Yes, but len returns counting from 1.
Anyway, you would have to add one to correct that anyway, wouldn't you?
If randrange is start <= x *<=* end, then you don't have to add one, you
just use the length.
If randrange is start<= x < end like __builtin__ range, you have to put
randrange(1,lenoflist+1)
x = random.randrange(0,lenoflist)

I would use randint because list indices need to be integers -- unless of
course I mistaken and
randrange returns an integer also. (But that would be repetitive to have to
functions do the same thing)
A quick check of the module docs (Jacob, do you know where to find the 
docs?) gives
randrange(  	[start,] stop[, step])
Return a randomly selected element from range(start, stop, step). This is equivalent to 
choice(range(start, stop, step)), but doesn't actually build a range object. New in version 1.5.2.

So the limits on randrange() are the same as for range() - it is start <= x < stop. And the returned 
value is an integer.

 Since len(listcontents) is one greater than the largest valid index of listcontents, the correct 
use of randrange() for this problem is
  x = random.randrange(0, len(listcontents))


print listcontents[x]

HTH,
Jacob
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Print record x in a file

2005-01-22 Thread Orri Ganel
Jacob S. wrote:
This will get a random record
I hope you do not think the comments are patronising
but you did say you are new so I did not want to give
naked code.
import random
#the above gives the program the ability to get a
#pseudo random number
file = open('test.rantxt')
listcontents = file.readlines()
#gives you the file as a list of records or it did on
#(assuming each line is a record)
file.close()
lenoflist = len(listcontents)-1
#find the length of the list and take one of because
computers count from 0

Yes, but len returns counting from 1.
Anyway, you would have to add one to correct that anyway, wouldn't you?
If randrange is start <= x *<=* end, then you don't have to add one, you
just use the length.
If randrange is start<= x < end like __builtin__ range, you have to put
randrange(1,lenoflist+1)
x = random.randrange(0,lenoflist)

I would use randint because list indices need to be integers -- unless of
course I mistaken and
randrange returns an integer also. (But that would be repetitive to 
have to
functions do the same thing)

print listcontents[x]

HTH,
Jacob
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
How about random.choice() ? Ie:
>>> print random.choice(f.readlines())
--
Email: singingxduck AT gmail DOT com
AIM: singingxduck
Programming Python for the fun of it.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Print record x in a file

2005-01-22 Thread Jacob S.
This will get a random record
I hope you do not think the comments are patronising
but you did say you are new so I did not want to give
naked code.
import random
#the above gives the program the ability to get a
#pseudo random number
file = open('test.rantxt')
listcontents = file.readlines()
#gives you the file as a list of records or it did on
#(assuming each line is a record)
file.close()
lenoflist = len(listcontents)-1
#find the length of the list and take one of because
computers count from 0
Yes, but len returns counting from 1.
Anyway, you would have to add one to correct that anyway, wouldn't you?
If randrange is start <= x *<=* end, then you don't have to add one, you
just use the length.
If randrange is start<= x < end like __builtin__ range, you have to put
randrange(1,lenoflist+1)
x = random.randrange(0,lenoflist)
I would use randint because list indices need to be integers -- unless of
course I mistaken and
randrange returns an integer also. (But that would be repetitive to have to
functions do the same thing)
print listcontents[x]
HTH,
Jacob
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Print record x in a file

2005-01-22 Thread David Holland
This will get a random record 
I hope you do not think the comments are patronising
but you did say you are new so I did not want to give
naked code.

import random
#the above gives the program the ability to get a
#pseudo random number
file = open('test.rantxt')
listcontents = file.readlines()
#gives you the file as a list of records or it did on
#(assuming each line is a record)
file.close()
lenoflist = len(listcontents)-1
#find the length of the list and take one of because
computers count from 0
x = random.randrange(0,lenoflist)
print listcontents[x]





___ 
ALL-NEW Yahoo! Messenger - all new features - even more fun! 
http://uk.messenger.yahoo.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor