Phillip B Oldham schrieb:
On Mon, Jan 19, 2009 at 12:15 PM, Chris Rebert wrote:
Assuming I'm interpreting you correctly (you're going to have to use
something like a getter):
Thanks, but I'm looking for a way to do it *without* using a getter as
I don't have easy access to the class (
James Stroud schrieb:
Ken Pu wrote:
Hi, below is the code I thought should create two generates, it[0] =
0,1,2,3,4,5, and it[1] = 0,10,20,30,..., but they turn out to be the
same!!!
from itertools import *
itlist = [0,0]
for i in range(2):
itlist[i] = (x+(i*10) for x in count())
print "what
garywood schrieb:
def ask_ok(prompt, retries=4, complaint="Yes or no, please!"):
while True:
password = input("enter something")
if password in ('y', 'ye', 'yes'): return True
if password in ('n', 'no', 'nope'): return False
retries = retries - 1
if ret
Mike mentions an important point, and I've been bitten by the
phenomenon Mike mentions---but only when *writing* to files. They
should always be closed explicitly, as in
f = file(filename, 'w')
f.write(somestring)
f.close()
On the other hand, I've never encountered a problem with the "for line
I'd also like to note that both the inputfiles variable and the
readlines() method are superfluous; to iterate through the file line by
line, use either
for line in open(inputfilename):
# do something with line
or (my personal preference, since I like to think of the file as a
thing rather th
I'd like to second the recommendation for the Python Cookbook. As an
experienced programmer, you'll learn a lot by osmosis.
Michael
--
http://mail.python.org/mailman/listinfo/python-list
There are two problems: (1) by quoting 'source', you refer to a string
literal, not the variable you defined via raw_input; (2) you have not
defined the variable 'input', so there's no way to call the 'read'
method on it.
Try this instead:
source_path = raw_input('file path: ')
s = open(source_pa
There are two problems: (1) by quoting 'source', you refer to a string
literal, not the variable you defined via raw_input; (2) you have not
defined the variable 'input', so there's no way to call the 'read'
method on it.
Try this instead:
source_path = raw_input('file path: ')
s = open(source_pa
It's good that you're using Python 2.3, which does have sets available,
as a previous poster mentioned. Users of Python 2.2 or earlier can get
most of the Set functionality using the following class (from Peter
Norvig's utils.py file):
# class Set:
# """This implements the Set class from PEP
> Is it normal?
Yes. The interpreter prints back the repr of a, which reflects the
imprecision inherent in floats. If you want '1.1', use the string
returned by the str function.
>>> a = 1.1
>>> a
1.1001
>>> repr(a)
'1.1001'
>>> str(a)
'1.1'
Michael
--
Michael D. Hartl
I actually find it strange that tuples don't have an index function,
since finding the index doesn't involve any mutation. Anyone know why
Python doesn't allow a statement like t.index('foo')?
In any case, you can use the index method of list objects if you
convert your tuple to a list first:
>>
I use a function isListLike in cases such as this one:
# def isListLike(L):
# """Return True if L is list-like, False otherwise."""
# try:
# L + []
# return True
# except:
# return False
Then you can use a standard if-else construct:
# if isListLike(myvar):
#
Sorry about the code: Google Groups screwed up the formatting, but I
hope you get the picture.
--
http://mail.python.org/mailman/listinfo/python-list
You need to put the "You guessed it..." block inside a conditional (if
guess == number) so that it only executes if the guess is right. (You
also need to break out of the loop in that case.) As written, the "You
guessed it..." block executes the first time through the loop
regardless of the guess
I warmly recommend downloading Peter Norvig's Python utilities file
(http://aima.cs.berkeley.edu/python/utils.py) and putting it on your
Python path. (E.g., in bash, put a line like
export PYTHONPATH="/path/to/utilities_directory"
in your .bashrc file.) The utils.py file defines many useful
fun
Oops, my bad. The utilities file I use gets loaded automatically when
I start my interpreter, so I mistook isnumber for a built-in function.
A battle-tested isnumber function is defined in Peter Norvig's utils.py
(http://aima.cs.berkeley.edu/python/utils.py):
#def isnumber(x):
#"Is x a number
As I mention below, I mistook the function from my utilities file for a
Python built-in; here's the implementation:
#def isnumber(x):
#"Is x a number? We say it is if it has an __int__ method."
#return hasattr(x, '__int__')
--
http://mail.python.org/mailman/listinfo/python-list
As luck would have it, isnumber is a built-in Python function:
>>> isnumber(1)
True
>>> isnumber(1.0)
True
>>> isnumber('1')
False
Michael
--
Michael D. Hartl, Ph.D.
Chief Technology Officer
http://quarksports.com/
--
http://mail.python.org/mailman/listinfo/python-list
Adam brings up a good point: eval is a very general function which
evaluates an arbitrary Python expression. As a result, it (and its
close cousin exec) should be used with caution if security is an issue.
Michael
--
http://mail.python.org/mailman/listinfo/python-list
Use the eval function:
>>> eval("30/(6+9)")
2
Michael
--
http://mail.python.org/mailman/listinfo/python-list
i.e., http://docs.python.org/lib/typesmapping.html
--
http://mail.python.org/mailman/listinfo/python-list
For problems like this I use a partition function defined in a utils.py
file that I use (based on Peter Norvig's utils file at
http://aima.cs.berkeley.edu/python/utils.py). Using partition, the
problem you posed can be solved by writing
#for a, b in partition([1, 2, 3, 4], 2):
#print a, b
Th
That's cool! Of course, walk returns a generator, so using a list
comprehension to turn it into a list seems natural, but I didn't
realize that list() does the same thing (and neither, apparently, did
the original implementor) -- although, with a little reflection, it
obviously must!
Michael
--
There's a great function called "walk" at that iterates over arbitrary
data (and is recursion-proof) at
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/118845
It also supplies a recursion-proof way to flatten a list. (Once you
can iterate over an arbitrary sequence, the flattened version
24 matches
Mail list logo