[Tutor] Pythonic nested lists

2010-09-27 Thread col speed
Hi all, I've been trying to write a programme that solves sudoku problems for a while now. I'm getting close, but would like to ask a few questions about the most pythonic way of doing some things. I've decided to create nested lists (row order, column order and square order) which correspond with

Re: [Tutor] Python Help

2010-09-27 Thread masawudu bature
Thanks David, But the loop was suppose to produce the count of even divisors an integer has. Like, 6 has 2 "even" divisors, which is 2 and 6, itself. 8 = 3 even divisors, which is 2, 4 ,and 8 10 = 2 even divisors, which is 2, and 10 12 = 4 even divisors, which is 2, 4, 6, and 12 sorry, I just don

Re: [Tutor] list comprehension, efficiency?

2010-09-27 Thread Lie Ryan
On 09/28/10 13:57, Bill Allen wrote: > I can now see that quite a bit of the code I write dealing with lists > can be done with list > comprehensions. My question is this, is the list comprehension styled > code generally > more efficient at runtime? If so, why? Yes, because the looping in list

Re: [Tutor] Python Help

2010-09-27 Thread David Hutto
On Tue, Sep 28, 2010 at 12:15 AM, masawudu bature wrote: > I'm having a hard time finding the count of divisors that are even. Help > anybody? > > Here's my code. > > The output is suppose to count the number of even divisors the range has. > > def evenCount(b) : >     for n in range(x, y+1) : >  

[Tutor] Python Help

2010-09-27 Thread masawudu bature
I'm having a hard time finding the count of divisors that are even. Help anybody? Here's my code. The output is suppose to count the number of even divisors the range has. def evenCount(b) : for n in range(x, y+1) : count = 0 if n % 2 == 0 : count += n/3

[Tutor] list comprehension, efficiency?

2010-09-27 Thread Bill Allen
I have seen list comprehensions used, but have not quite got the hang of it yet. So, I was writing a bit of code to do some work with file directories and decided to give it a try as follows: list_c = os.listdir("c:") #first code written in the way I usually would. dirs = [] for x in list_c:

Re: [Tutor] generating independent random numbers

2010-09-27 Thread Dave Angel
On 2:59 PM, Steven D'Aprano wrote: On Tue, 28 Sep 2010 08:55:36 am Carter Danforth wrote: class Date: c = random.randint(16,30) y = random.randint(0,99) month = random.randint(1,12) Here's your problem: you are creating a class where all the attributes (called "members" in som

Re: [Tutor] filling 2d array with zeros

2010-09-27 Thread Alex Hall
On 9/27/10, Steven D'Aprano wrote: > On Tue, 28 Sep 2010 06:00:41 am Alex Hall wrote: >> > [ [0]*3 ]*4 behaves the same way. There's no problem in the inner >> > list, but the outer list doesn't make four copies of [0,0,0], it >> > has *one* list repeated four times. Modify one, modify them all. >

Re: [Tutor] generating independent random numbers

2010-09-27 Thread Joel Levine
You might try fixing up the following. I did not match the scale to what you are looking for. But the trick is to take advantage of the time module's association of numbers with dates: import time import random """ Sample output Wed Apr 29 14:35:58 1992 Thu Jun 24 12:04:15 1971 Fri Oct 7

Re: [Tutor] unittest testing order...

2010-09-27 Thread Steven D'Aprano
On Tue, 28 Sep 2010 08:07:30 am Modulok wrote: > On 9/27/10, Steven D'Aprano wrote: > > On Tue, 28 Sep 2010 04:03:17 am Modulok wrote: > >> List, > >> > >> When using the unittest module, tests are run in alphanumeric > >> order. What's the suggested way of specifying a test order? > > > > There i

Re: [Tutor] filling 2d array with zeros

2010-09-27 Thread Steven D'Aprano
On Tue, 28 Sep 2010 06:00:41 am Alex Hall wrote: > > [ [0]*3 ]*4 behaves the same way. There's no problem in the inner > > list, but the outer list doesn't make four copies of [0,0,0], it > > has *one* list repeated four times. Modify one, modify them all. > > That makes sense. Basically, the * ope

Re: [Tutor] generating independent random numbers

2010-09-27 Thread Steven D'Aprano
On Tue, 28 Sep 2010 08:55:36 am Carter Danforth wrote: > class Date: > c = random.randint(16,30) > y = random.randint(0,99) > month = random.randint(1,12) Here's your problem: you are creating a class where all the attributes (called "members" in some other languages) belong to the c

[Tutor] IPython: trouble setting an editor

2010-09-27 Thread Richard D. Moores
64-bit Vista Python 2.6 for IPython 0.10 I also have Python 2.7 and 3.1 I'd like to use the version of IDLE that comes with Python 2.6. However, if I have the line ipy_editors.idle() in my ipy_user_conf.py, And enter edit, What opens is the IDLE for Python 3.1. So, in imitation of this line

Re: [Tutor] filling 2d array with zeros

2010-09-27 Thread Alex Hall
On 9/27/10, Sander Sweers wrote: > On 27 September 2010 23:15, Sander Sweers wrote: >>> objects: copying the memory location, not making a deep copy and >>> getting a duplicate object. >> >> It does not copy the object it makes multiple _references_ to the *same* >> object. > > Oops, You already

[Tutor] generating independent random numbers

2010-09-27 Thread Carter Danforth
Hi, I'm writing a program that's testing speed calculation of calendar dates from any date spanning 1600-3000. I want it to generate a random date and then prompt the user to indicate the correct day of the week using Zeller's formula. Included below is just some of the code to show what I'm havin

Re: [Tutor] unittest testing order...

2010-09-27 Thread Steve Willoughby
On 27-Sep-10 15:07, Modulok wrote: In an ideal world, I agree. This is possible for pure functional programming where state is avoided. However, when dealing with object oriented, imperative programming, state changes cannot be avoided. Generally, a unit test should test a single aspect of a si

Re: [Tutor] unittest testing order...

2010-09-27 Thread Modulok
On 9/27/10, Steven D'Aprano wrote: > On Tue, 28 Sep 2010 04:03:17 am Modulok wrote: >> List, >> >> When using the unittest module, tests are run in alphanumeric order. >> What's the suggested way of specifying a test order? > > There isn't one. It shouldn't matter what order the tests run, no test

Re: [Tutor] if value in list of dictionaries

2010-09-27 Thread Emile van Sebille
On 9/27/2010 1:22 PM Norman Khine said... what is the correct way to ensure that {'industry': 'travel', 'name': 'other','value': MSG(u"Other")} is always added to the end of this list after all the items have been sorted? here is the code which returns this list: options.sort(key=itemgette

Re: [Tutor] filling 2d array with zeros

2010-09-27 Thread Sander Sweers
On 27 September 2010 23:15, Sander Sweers wrote: >> objects: copying the memory location, not making a deep copy and >> getting a duplicate object. > > It does not copy the object it makes multiple _references_ to the *same* > object. Oops, You already got the idea and I should have read better.

Re: [Tutor] filling 2d array with zeros

2010-09-27 Thread Sander Sweers
On 27 September 2010 22:00, Alex Hall wrote: > That makes sense. Basically, the * operator in this case acts as a > copying command. For simple data types this is fine, but throw in a > complex type, in this case a list (though I expect that any object > would do this) and you are just doing what

[Tutor] if value in list of dictionaries

2010-09-27 Thread Norman Khine
hello, i have a list which is generated from a csv file: options = [ {'industry': 'travel', 'name': 'owner-director','value': MSG(u"Owner/Director")}, {'industry': 'travel', 'name': 'manager','value': MSG(u"Manager")}, {'industry': 'travel', 'name': 'assistant-manager','val

Re: [Tutor] filling 2d array with zeros

2010-09-27 Thread Alex Hall
On 9/27/10, Steven D'Aprano wrote: > On Tue, 28 Sep 2010 03:54:55 am Alex Hall wrote: >> Hi again everyone, >> I have a 2d array (I guess it is technically a list) which I want to >> fill with zeros. Later I will change some values, but any I do not >> change have to be zeros. I have two complex f

Re: [Tutor] filling 2d array with zeros

2010-09-27 Thread Knacktus
Am 27.09.2010 20:29, schrieb Steven D'Aprano: On Tue, 28 Sep 2010 03:54:55 am Alex Hall wrote: Hi again everyone, I have a 2d array (I guess it is technically a list) which I want to fill with zeros. Later I will change some values, but any I do not change have to be zeros. I have two complex fo

Re: [Tutor] unittest testing order...

2010-09-27 Thread Evert Rol
>> List, >> >> When using the unittest module, tests are run in alphanumeric order. >> What's the suggested way of specifying a test order? > > There isn't one. It shouldn't matter what order the tests run, no test > should *rely* on another test. > > (Although of course, if one test fails, a

Re: [Tutor] filling 2d array with zeros

2010-09-27 Thread Emile van Sebille
On 9/27/2010 10:54 AM Alex Hall said... What is wrong with the following line? self.am=[[(a,b) for a in range(len(self.lines)) a=0] for b in range(len(self.lines)) b=0] The a=0 and b=0 -- what do you think they're doing? Emile ___ Tutor maill

Re: [Tutor] dynamic arrays?

2010-09-27 Thread Joel Levine
I'm guessing you are also using numpy and that you want arrays (or matrices) for the sake of either (a) matrix and array functions or (b) much greater processing speed. Add from numpy import * Then hat you can do is go back and forth: Build what you intend to be an array, but build it as

Re: [Tutor] unittest testing order...

2010-09-27 Thread Steven D'Aprano
On Tue, 28 Sep 2010 04:03:17 am Modulok wrote: > List, > > When using the unittest module, tests are run in alphanumeric order. > What's the suggested way of specifying a test order? There isn't one. It shouldn't matter what order the tests run, no test should *rely* on another test. (Although

Re: [Tutor] filling 2d array with zeros

2010-09-27 Thread Steven D'Aprano
On Tue, 28 Sep 2010 03:54:55 am Alex Hall wrote: > Hi again everyone, > I have a 2d array (I guess it is technically a list) which I want to > fill with zeros. Later I will change some values, but any I do not > change have to be zeros. I have two complex for loops, but I tried to > scale things do

Re: [Tutor] filling 2d array with zeros

2010-09-27 Thread Joel Goldstick
On Mon, Sep 27, 2010 at 1:54 PM, Alex Hall wrote: > Hi again everyone, > I have a 2d array (I guess it is technically a list) which I want to > fill with zeros. Later I will change some values, but any I do not > change have to be zeros. I have two complex for loops, but I tried to > scale things

Re: [Tutor] dynamic arrays?

2010-09-27 Thread Joel Goldstick
On Mon, Sep 27, 2010 at 1:52 PM, Steven D'Aprano wrote: > On Tue, 28 Sep 2010 02:12:55 am Joel Goldstick wrote: > >> a=[] >> i=0 >> for l in open("file.txt", "r"): >>  a[i]=l >>   i+=1 > > Did you try it before posting? > That is a copy of the OP post. and it shows what didn't work I deleted in

[Tutor] unittest testing order...

2010-09-27 Thread Modulok
List, When using the unittest module, tests are run in alphanumeric order. What's the suggested way of specifying a test order? Any code examples would be great! Thanks -Modulok- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscript

[Tutor] function error

2010-09-27 Thread roberto
hello, i have the following error when i call this function: 20 def outOfBounds(): ---> 21 if abs(turtle.position()[0]) > turtle.window_height()/2 or abs(turtle.position()[1]) > turtle.window_width()/2: 22 return "true" 23 else: TypeError: 'function' ob

Re: [Tutor] dynamic arrays?

2010-09-27 Thread Steven D'Aprano
On Tue, 28 Sep 2010 01:32:32 am Alex Hall wrote: > Hi all, > One thing I have never much liked about Python is its need for > specifically sized arrays and lack of a dynamic, array-like data > structure. Python lists are dynamically sized. If you compare Python to languages with fixed-size arrays

[Tutor] filling 2d array with zeros

2010-09-27 Thread Alex Hall
Hi again everyone, I have a 2d array (I guess it is technically a list) which I want to fill with zeros. Later I will change some values, but any I do not change have to be zeros. I have two complex for loops, but I tried to scale things down to a couple list comprehensions and I broke things. What

Re: [Tutor] dynamic arrays?

2010-09-27 Thread Steven D'Aprano
On Tue, 28 Sep 2010 02:12:55 am Joel Goldstick wrote: > a=[] > i=0 > for l in open("file.txt", "r"): > a[i]=l > i+=1 Did you try it before posting? -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription o

Re: [Tutor] dynamic arrays?

2010-09-27 Thread Steven D'Aprano
On Tue, 28 Sep 2010 01:45:56 am Brian Jones wrote: > A python list and a python array are one and the same to my > knowledge. Close, but not quite. Python has an array type. It is almost identical to lists, except the type of data it will hold is constrained to a specific type: >>> import arra

Re: [Tutor] function with multiple checks

2010-09-27 Thread Steven D'Aprano
On Tue, 28 Sep 2010 01:09:54 am Tim Miller wrote: > def complex_password(password): > """Checks password for sufficient complexity.""" > if len(password) < 12: > return False > if len([c for c in password if c in punctuation]) == 0: > return False > if len([c

Re: [Tutor] function with multiple checks

2010-09-27 Thread Tim Miller
On 28/09/10 01:50, Brian Jones wrote: On Mon, Sep 27, 2010 at 11:43 AM, Brian Jones mailto:bkjo...@gmail.com>> wrote: How about this: d = [digits, punctuation, ascii_uppercase, ascii_lowercase] s = 'asdf1234A' for c in d: if not [x for x in s if x in c]: print x, ' not in ', c Ju

Re: [Tutor] function with multiple checks

2010-09-27 Thread Tim Miller
On 28/09/10 01:46, Jerry Hill wrote: The way you've written it obviously works fine. That being said, I'd probably do something like this: from string import ascii_lowercase, ascii_uppercase, digits, punctuation def complex_password(password): '''Checks to make sure a password is complex'

Re: [Tutor] function with multiple checks

2010-09-27 Thread Tim Miller
set does seem to have what you want: isdisjoint() could do the trick. Eg: if set(punctuation).isdisjoint(password) or set(digits).isdisjoint(password) or set(ascii_uppercase).isdisjoint(password) or set(ascii_lowercase).isdisjoint(password): return False return True You co

Re: [Tutor] dynamic arrays?

2010-09-27 Thread Joel Goldstick
> Hi all, >> >> One thing I have never much liked about Python is its need for >> >> specifically sized arrays and lack of a dynamic, array-like data >> >> structure. For example, the following fails with a "list assignment >> >> index out of range" error: a=[] i=0 for l in open("file.txt", "r"):

Re: [Tutor] dynamic arrays?

2010-09-27 Thread Dave Angel
On 2:59 PM, Alex Hall wrote: On 9/27/10, Brian Jones wrote: On Mon, Sep 27, 2010 at 11:32 AM, Alex Hall wrote: Hi all, One thing I have never much liked about Python is its need for specifically sized arrays and lack of a dynamic, array-like data structure. For example, the following fails

Re: [Tutor] dynamic arrays?

2010-09-27 Thread Alex Hall
On 9/27/10, Brian Jones wrote: > On Mon, Sep 27, 2010 at 11:39 AM, Alex Hall wrote: > >> On 9/27/10, Brian Jones wrote: >> > On Mon, Sep 27, 2010 at 11:32 AM, Alex Hall wrote: >> > >> >> Hi all, >> >> One thing I have never much liked about Python is its need for >> >> specifically sized arrays

Re: [Tutor] function with multiple checks

2010-09-27 Thread Evert Rol
> I've got a small function that I'm using to check whether a password is of a > certain length and contains mixed case, numbers and punctuation. > > Originally I was using multiple "if re.search" for the patterns but it looked > terrible so I've read up on list comprehensions and it's slightly

Re: [Tutor] function with multiple checks

2010-09-27 Thread Brian Jones
On Mon, Sep 27, 2010 at 11:43 AM, Brian Jones wrote: > > > On Mon, Sep 27, 2010 at 11:09 AM, Tim Miller wrote: > >> I've got a small function that I'm using to check whether a password is of >> a certain length and contains mixed case, numbers and punctuation. >> >> Originally I was using multip

Re: [Tutor] dynamic arrays?

2010-09-27 Thread Brian Jones
On Mon, Sep 27, 2010 at 11:39 AM, Alex Hall wrote: > On 9/27/10, Brian Jones wrote: > > On Mon, Sep 27, 2010 at 11:32 AM, Alex Hall wrote: > > > >> Hi all, > >> One thing I have never much liked about Python is its need for > >> specifically sized arrays and lack of a dynamic, array-like data >

Re: [Tutor] function with multiple checks

2010-09-27 Thread Jerry Hill
On Mon, Sep 27, 2010 at 11:09 AM, Tim Miller wrote: > I've got a small function that I'm using to check whether a password is of a > certain length and contains mixed case, numbers and punctuation. > > Originally I was using multiple "if re.search" for the patterns but it > looked terrible so I've

Re: [Tutor] dynamic arrays?

2010-09-27 Thread Vince Spicer
On Mon, Sep 27, 2010 at 9:32 AM, Alex Hall wrote: > Hi all, > One thing I have never much liked about Python is its need for > specifically sized arrays and lack of a dynamic, array-like data > structure. For example, the following fails with a "list assignment > index out of range" error: > > a=

Re: [Tutor] function with multiple checks

2010-09-27 Thread Brian Jones
On Mon, Sep 27, 2010 at 11:09 AM, Tim Miller wrote: > I've got a small function that I'm using to check whether a password is of > a certain length and contains mixed case, numbers and punctuation. > > Originally I was using multiple "if re.search" for the patterns but it > looked terrible so I'v

Re: [Tutor] dynamic arrays?

2010-09-27 Thread Alex Hall
On 9/27/10, Brian Jones wrote: > On Mon, Sep 27, 2010 at 11:32 AM, Alex Hall wrote: > >> Hi all, >> One thing I have never much liked about Python is its need for >> specifically sized arrays and lack of a dynamic, array-like data >> structure. For example, the following fails with a "list assign

Re: [Tutor] dynamic arrays?

2010-09-27 Thread Evert Rol
> One thing I have never much liked about Python is its need for > specifically sized arrays and lack of a dynamic, array-like data > structure. For example, the following fails with a "list assignment > index out of range" error: > > a=[] > i=0 > for l in open("file.txt", "r"): > a[i]=l > i+=1

Re: [Tutor] dynamic arrays?

2010-09-27 Thread Brian Jones
On Mon, Sep 27, 2010 at 11:32 AM, Alex Hall wrote: > Hi all, > One thing I have never much liked about Python is its need for > specifically sized arrays and lack of a dynamic, array-like data > structure. For example, the following fails with a "list assignment > index out of range" error: > > a

[Tutor] dynamic arrays?

2010-09-27 Thread Alex Hall
Hi all, One thing I have never much liked about Python is its need for specifically sized arrays and lack of a dynamic, array-like data structure. For example, the following fails with a "list assignment index out of range" error: a=[] i=0 for l in open("file.txt", "r"): a[i]=l i+=1 Is there

[Tutor] function with multiple checks

2010-09-27 Thread Tim Miller
I've got a small function that I'm using to check whether a password is of a certain length and contains mixed case, numbers and punctuation. Originally I was using multiple "if re.search" for the patterns but it looked terrible so I've read up on list comprehensions and it's slightly improved

Re: [Tutor] super.__init__() arguments

2010-09-27 Thread Jojo Mwebaze
Thanks Guys! Works perfect cheers On Mon, Sep 27, 2010 at 1:01 PM, Wayne Werner wrote: > In addition it only works for new style classes. > > -wayne > > On 9/27/10, Lie Ryan wrote: > > On 09/27/10 09:45, Jojo Mwebaze wrote: > >> Hey Tutor, > >> > >> Seems a small issue but this has been p

Re: [Tutor] super.__init__() arguments

2010-09-27 Thread Jojo Mwebaze
Thanks Guys! Works perfect cheers On Mon, Sep 27, 2010 at 1:01 PM, Wayne Werner wrote: > In addition it only works for new style classes. > > -wayne > > On 9/27/10, Lie Ryan wrote: > > On 09/27/10 09:45, Jojo Mwebaze wrote: > >> Hey Tutor, > >> > >> Seems a small issue but this has been p

Re: [Tutor] class method problem

2010-09-27 Thread Joel Goldstick
I looked up the word zoeken -- it means find in English. I looked up the chapter and exercise in the online text "How to think like a computer scientist" Its a good tutorial. I think the OP seems to get confused on basic concepts. Here is my stab at the exercise: #!/usr/bin/env python """This

Re: [Tutor] class method problem

2010-09-27 Thread Dave Angel
On 2:59 PM, Roelof Wobben wrote: Hello, Fine that you are in a arque But can we come back to my problem. How can I take care that test2 can be initialized. Roelof You had this code, and got the following error: class zoeken() : pass def __len__(self): return 0 def __st

Re: [Tutor] super.__init__() arguments

2010-09-27 Thread Wayne Werner
In addition it only works for new style classes. -wayne On 9/27/10, Lie Ryan wrote: > On 09/27/10 09:45, Jojo Mwebaze wrote: >> Hey Tutor, >> >> Seems a small issue but this has been playing for a while now, what am i >> doing wrong here? >> > > super() without argument only works for Python 3.

Re: [Tutor] class method problem

2010-09-27 Thread Roelof Wobben
Hello, Fine that you are in a arque But can we come back to my problem. How can I take care that test2 can be initialized. Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscri

Re: [Tutor] super.__init__() arguments

2010-09-27 Thread Lie Ryan
On 09/27/10 09:45, Jojo Mwebaze wrote: > Hey Tutor, > > Seems a small issue but this has been playing for a while now, what am i > doing wrong here? > super() without argument only works for Python 3. In Python 2.x, you have to pass to super your class name and your class instance, i.e.: Class