Re: [Tutor] list, tuple or dictionary

2011-11-29 Thread Steven D'Aprano
ADRIAN KELLY wrote: i am trying to create a program that will allow users to enter items and their prices; should i be looking at a list, tuple or what? many thanksadrian Translated to a version more familiar in the real world: I'm looking to build a wooden wardrobe that will allow

Re: [Tutor] list of objects?

2011-11-16 Thread bob gailer
. *From:* bob gailer bgai...@gmail.com *To:* Elwin Estle chrysalis_reb...@yahoo.com *Cc:* tutor@python.org tutor@python.org *Sent:* Tuesday, November 15, 2011 9:18 PM *Subject:* Re: [Tutor] list of objects? On 11/15/2011 8:40 AM, Elwin Estle wrote: I am attempting to write a text

[Tutor] list of objects?

2011-11-15 Thread Elwin Estle
I am attempting to write a text based spider solitaire game.  I have  a pretty simple card class, and a deck class, which has a list of card objects, which are shuffled, then individual elements are put into self.dealt, which is a 'list of lists' when the cards are dealt. I am trying to

Re: [Tutor] list of objects?

2011-11-15 Thread Walter Prins
Hi Elwin, On 15 November 2011 13:40, Elwin Estle chrysalis_reb...@yahoo.com wrote: I am attempting to write a text based spider solitaire game. I have a pretty simple card class, and a deck class, which has a list of card objects, which are shuffled, then individual elements are put into

Re: [Tutor] list of objects?

2011-11-15 Thread James Reynolds
On Tue, Nov 15, 2011 at 8:40 AM, Elwin Estle chrysalis_reb...@yahoo.comwrote: I am attempting to write a text based spider solitaire game. I have a pretty simple card class, and a deck class, which has a list of card objects, which are shuffled, then individual elements are put into

Re: [Tutor] list of objects?

2011-11-15 Thread bob gailer
On 11/15/2011 8:40 AM, Elwin Estle wrote: I am attempting to write a text based spider solitaire game. What are the rules of your version of Spider? The only spiders I know have 10 dealt piles and 1 draw pile. I think you have greatly complicated things by using classes. Consider: deck =

[Tutor] List of Classes with a dictionary within the Class.

2011-09-21 Thread Mukund Chavan
Hi, I was trying to get a list of Class Objects. The Class itself has string fields and a dictionary that is initialized as a part of the __init__ However the fields of the dictionary for each object in the list of class objects seems to be the same. I took an example from the following

Re: [Tutor] List of Classes with a dictionary within the Class.

2011-09-21 Thread Steven D'Aprano
Mukund Chavan wrote: Hi, I was trying to get a list of Class Objects. The Class itself has string fields and a dictionary that is initialized as a part of the __init__ No it doesn't. It has a dictionary that is initialised *once*, when the class is defined. From that point on, every

Re: [Tutor] List of Classes with a dictionary within the Class.

2011-09-21 Thread Mukund Chavan
. - Mukund Date: Thu, 22 Sep 2011 10:58:09 +1000 From: st...@pearwood.info To: tutor@python.org Subject: Re: [Tutor] List of Classes with a dictionary within the Class. Mukund Chavan wrote: Hi, I was trying to get a list of Class Objects. The Class itself has string fields

[Tutor] List of lists optimization and printing.

2011-07-29 Thread Sergey
Hello. Yes, catb.org and so on. I was searching for some kind of finding max width in a table (a list of two lists) and had found this mailing list. So I want somebody to look at my code and say what can be done better from a programmer point of view. Just SQL like printing and writing. I mean

Re: [Tutor] List problem

2011-07-25 Thread Andre Engels
On Mon, Jul 25, 2011 at 4:19 AM, David Merrick merrick...@gmail.com wrote: def append(self,item): '''Adds an item to the end of the List''' current = self.head previous = None while current.getNext() != None: previous = current

Re: [Tutor] List problem

2011-07-25 Thread Peter Otten
David Merrick wrote: def append(self,item): '''Adds an item to the end of the List''' current = self.head previous = None while current.getNext() != None: previous = current current = current.getNext() if

[Tutor] List problem

2011-07-24 Thread David Merrick
class Node: def __init__(self,initdata): self.data = initdata self.next = None def getData(self): return self.data def getNext(self): return self.next def setdata(self,newData): self.data = newData def setNext(self,newnext):

Re: [Tutor] List problem

2011-07-24 Thread bob gailer
I have no desire to wade through all that code. Please post the entire traceback. On 7/24/2011 10:19 PM, David Merrick wrote: class Node: def __init__(self,initdata): self.data = initdata self.next = None def getData(self): return self.data def

[Tutor] List methods inside a dictionary of list

2011-07-11 Thread Rafael Turner
Hello, I am playing lists and dictionaries and I came across this counter-intuitive result. d = dict(zip(['a', 'q', 'c', 'b', 'e', 'd', 'g', 'j'],8*[[0]])) d Out: {'a': [0], 'b': [0], 'c': [0], 'd': [0], 'e': [0], 'g': [0], 'j': [0], 'q': [0]} d['a'].__setitem__(0,4) d Out: {'a': [4],

Re: [Tutor] List methods inside a dictionary of list

2011-07-11 Thread Walter Prins
Hi, On 11 July 2011 14:26, Rafael Turner steven.rafael.tur...@gmail.com wrote: d = dict(zip(['a', 'q', 'c', 'b', 'e', 'd', 'g', 'j'],8*[[0]])) d Out: {'a': [0], 'b': [0], 'c': [0], 'd': [0], 'e': [0], 'g': [0], 'j': [0], 'q': [0]} d['a'].__setitem__(0,4) d Out: {'a':

Re: [Tutor] List methods inside a dictionary of list

2011-07-11 Thread Brett Ritter
On Mon, Jul 11, 2011 at 9:26 AM, Rafael Turner steven.rafael.tur...@gmail.com wrote: I am playing lists and dictionaries and I came across this counter-intuitive result. d = dict(zip(['a', 'q', 'c', 'b', 'e', 'd', 'g', 'j'],8*[[0]])) ... d['a'].__setitem__(0,4) ... I was not expecting all

Re: [Tutor] List methods inside a dictionary of list

2011-07-11 Thread Rafael Turner
I did not understand the behavior of array multiplication. In fact, I just now learned what it was called thanks to your email. Best wishes, Rafael On Mon, Jul 11, 2011 at 9:33 AM, Brett Ritter swift...@swiftone.org wrote: On Mon, Jul 11, 2011 at 9:26 AM, Rafael Turner

Re: [Tutor] list.__init__ within class definition

2011-04-21 Thread Alan Gauld
James Mills prolo...@shortcircuit.net.au wrote What you actually want is this: class Tomato(list): ... def __init__(self, data): ... super(Tomato, self).__init__(data) ... l = Tomato([1, 2, 3]) l [1, 2, 3] Your example: class Tomato(list): ... def __init__(self,

Re: [Tutor] list.__init__ within class definition

2011-04-21 Thread Alan Gauld
Alex Companioni achom...@gmail.com wrote class Tomato(list): def __init__(self, data): list.__init__(self, data) The list.__init__ method (if it is a method, I'm not clear on what __init__ actually *is*) creates a list, right? Not quite. __init__ (which is a method) is an

[Tutor] list.__init__ within class definition

2011-04-20 Thread Alex Companioni
Hey there, In the following class definition: class Tomato(list): def __init__(self, data): list.__init__(self, data) The list.__init__ method (if it is a method, I'm not clear on what __init__ actually *is*) creates a list, right? In other words, l = Tomato([1,2,3]) will create

Re: [Tutor] list.__init__ within class definition

2011-04-20 Thread James Mills
On Thu, Apr 21, 2011 at 1:21 PM, Alex Companioni achom...@gmail.com wrote: In the following class definition: class Tomato(list):    def __init__(self, data):        list.__init__(self, data) The list.__init__ method (if it is a method, I'm not clear on what __init__ actually *is*) creates

[Tutor] List sorting issues

2011-03-28 Thread Eric Stevens
I am currently designing an address book program, and am trying to design a method for organizing the keys (which are the names of the entries) for displaying purposes. I have created a list by doing sortedKeys = self.addbook.keys() {the self.addbook refers to a dictionary in a custom class}, and

Re: [Tutor] List sorting issues

2011-03-28 Thread Luke Paireepinart
Sort returns none because it changes your list in-place. Print your list out after calling sort and you should see the new one. - Sent from a mobile device. Apologies for brevity and top-posting. - On Mar 28, 2011, at 11:53 AM, Eric

Re: [Tutor] List sorting issues

2011-03-28 Thread Steven D'Aprano
Eric Stevens wrote: I am currently designing an address book program, and am trying to design a method for organizing the keys (which are the names of the entries) for displaying purposes. I have created a list by doing sortedKeys = self.addbook.keys() {the self.addbook refers to a dictionary in

Re: [Tutor] list of dictionary

2011-02-25 Thread Alan Gauld
sunil tech sunil.tech...@gmail.com wrote i have d=[{'qty':0.0},{'qty':0.0}] when all the qty is 0.0, i want to perform some print operation (only at once, after it checks everything in the list of dictionary 'd')... if its not 0.0, print some message... Just so I'm clear on the

Re: [Tutor] list of dictionary

2011-02-25 Thread Patty
- Original Message - From: Pacific Morrowind To: tutor@python.org Sent: Thursday, February 24, 2011 10:21 PM Subject: Re: [Tutor] list of dictionary Hi; On 24/02/2011 9:35 PM, sunil tech wrote: Hi all... i have d=[{'qty':0.0},{'qty':0.0} If there isn't

Re: [Tutor] list of dictionary

2011-02-25 Thread Pacific Morrowind
On 25/02/2011 9:44 AM, Patty wrote: - Original Message - *From:* Pacific Morrowind mailto:pacificmorrow...@gmail.com *To:* tutor@python.org mailto:tutor@python.org *Sent:* Thursday, February 24, 2011 10:21 PM *Subject:* Re: [Tutor] list of dictionary snip

[Tutor] list of dictionary

2011-02-24 Thread sunil tech
Hi all... i have d=[{'qty':0.0},{'qty':0.0}] when all the qty is 0.0, i want to perform some print operation (only at once, after it checks everything in the list of dictionary 'd')... if its not 0.0, print some message... Thank you in advance ___

Re: [Tutor] list of dictionary

2011-02-24 Thread Pacific Morrowind
Hi; On 24/02/2011 9:35 PM, sunil tech wrote: Hi all... i have d=[{'qty':0.0},{'qty':0.0}] If there isn't some pressing reason to dictionaries as the list items (but since I'm not sure how you're generating the list/what you are later using the list I can't tell ofc but if applicable to your

[Tutor] list of strings

2011-02-09 Thread Shawn Matlock
Hello All, I'm doing something wrong. This prints a list of data source names: # List all data sources dataSourceList = AdminConfig.list('DataSource') # .split(ls) if verbose == 'True': print 'DEBUG Data Source List: ' print dataSourceList DEBUG Data Source List:

Re: [Tutor] list of strings

2011-02-09 Thread Hugo Arts
On Wed, Feb 9, 2011 at 11:56 PM, Shawn Matlock matl...@odscompanies.com wrote: Hello All, I’m doing something wrong. This prints a list of data source names:     # List all data sources     dataSourceList = AdminConfig.list('DataSource') # .split(ls)     if verbose == 'True':    

[Tutor] Homework and the Tutor list

2011-02-02 Thread Jerry Hill
   I would have to agree with you Ian.  Coming from an art then computer animation visual effects background, it's not until recently that it became evident to me that in order to push the potential of this medium, I would definitely have to learn to code.  I think the stigma of the homework

Re: [Tutor] Homework and the Tutor list

2011-02-02 Thread Rance Hall
On Wed, Feb 2, 2011 at 10:53 AM, Jerry Hill malaclyp...@gmail.com wrote: snip I don't think that's true at all.  I think people here are happy to help, including by posting working, efficient, code.  What we try to avoid is having students come here with their assignments and have us do their

[Tutor] list of tutors for python

2011-01-21 Thread bruce
Hi guys. Please don't slam me!! I'm working on a project, looking for a pretty good number of pythonistas. Trying to find resources that I should look to to find them, and thought I would try here for suggestions. Any comments would be appreciated. Thanks

Re: [Tutor] list of tutors for python

2011-01-21 Thread Emile van Sebille
On 1/21/2011 12:57 PM bruce said... Hi guys. Please don't slam me!! I'm working on a project, looking for a pretty good number of pythonistas. Trying to find resources that I should look to to find them, and thought I would try here for suggestions. Any comments would be appreciated. You're

Re: [Tutor] list of tutors for python

2011-01-21 Thread Steven D'Aprano
bruce wrote: Hi guys. Please don't slam me!! I'm working on a project, looking for a pretty good number of pythonistas. Trying to find resources that I should look to to find them, and thought I would try here for suggestions. I'm sorry, I don't understand what your question is. If you're

Re: [Tutor] list of tutors for python

2011-01-21 Thread Alan Gauld
bruce badoug...@gmail.com wrote Please don't slam me!! I'm working on a project, looking for a pretty good number of pythonistas. Trying to find resources that I should look to to find them, and thought I would try here for suggestions. OK, This list is not a recruiting forum for Python

[Tutor] [tutor] list order change

2010-11-26 Thread Michael Sprayberry
You can also sort a list so that is ordered in reverse or alphabetically     Cheers, Michael ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] List Changing Order

2010-11-26 Thread Corey Richardson
Steven D'Aprano wrote: Corey Richardson wrote: Tutors, I recall that the keys of dictionaries have arbitrary order, and may change over time. Is this true of lists? I can't find the answer from a simple Google search. Thank you! Only if you re-arrange it yourself. list.sort(),

[Tutor] List Changing Order

2010-11-25 Thread Corey Richardson
Tutors, I recall that the keys of dictionaries have arbitrary order, and may change over time. Is this true of lists? I can't find the answer from a simple Google search. Thank you! ___ Tutor maillist - Tutor@python.org To unsubscribe or change

Re: [Tutor] List Changing Order

2010-11-25 Thread James Mills
On Fri, Nov 26, 2010 at 1:47 PM, Corey Richardson kb1...@aim.com wrote: I recall that the keys of dictionaries have arbitrary order, and may change over time. Is this true of lists? I can't find the answer from a simple Google search. Thank you! items append to a list retain their order.

Re: [Tutor] List Changing Order

2010-11-25 Thread Steven D'Aprano
Corey Richardson wrote: Tutors, I recall that the keys of dictionaries have arbitrary order, and may change over time. Is this true of lists? I can't find the answer from a simple Google search. Thank you! Only if you re-arrange it yourself. list.sort(), list.reverse() and

[Tutor] List help

2010-11-20 Thread george wu
x=0 y=0 w=raw_input(Input: ) w=list(w) for x in range(len(w)): a=w[x] t=0 print a if a==2 or a==4 or a==6 or a==8 or a==10: t=a/2 print hi When I run this program, it doesn't print hi. Can you please tell me why? ___ Tutor

Re: [Tutor] List help

2010-11-20 Thread Emile van Sebille
On 11/20/2010 11:06 AM george wu said... x=0 y=0 w=raw_input(Input: ) w=list(w) for x in range(len(w)): a=w[x] t=0 print a if a==2 or a==4 or a==6 or a==8 or a==10: t=a/2 print hi When I run this program, it doesn't print hi. Can you please tell me why? When you're

Re: [Tutor] List comprehension question

2010-11-13 Thread Steven D'Aprano
Richard D. Moores wrote: On Fri, Nov 12, 2010 at 15:26, Steven D'Aprano st...@pearwood.info wrote: best = t.repeat(number=1, repeat=5) print round(best, 3) t.repeat(number=1, repeat=5) is a list of the 5 times, not the best of the 5. Oops! Good catch. Sorry about that. --

Re: [Tutor] List comprehension question

2010-11-12 Thread Steven D'Aprano
Richard D. Moores wrote: I find using that at the interactive prompt a bit onerous -- lots of copy and pasting. And doubly so when comparing times for 2 or more functions. Does your Python not support readline? Normally, if you press UP ARROW or DOWN ARROW, Python will cycle through the

Re: [Tutor] List comprehension question

2010-11-12 Thread Richard D. Moores
On Fri, Nov 12, 2010 at 02:11, Steven D'Aprano st...@pearwood.info wrote: Richard D. Moores wrote: I find using that at the interactive prompt a bit onerous -- lots of copy and pasting. And doubly so when comparing times for 2 or more functions. Does your Python not support readline?

Re: [Tutor] List comprehension question

2010-11-12 Thread David Hutto
       repeatedly, returning a list of results. ... I'm sorry, Steven, but I could you revise this code to use repeat=5 instead of the for loop? I can't see how to do it. help(timeit.Timer repeat(self, repeat=3, number=100) | Call timeit() a few times. | | This is a

Re: [Tutor] List comprehension question

2010-11-12 Thread Richard D. Moores
On Fri, Nov 12, 2010 at 05:15, David Hutto smokefl...@gmail.com wrote:        repeatedly, returning a list of results. ... I'm sorry, Steven, but I could you revise this code to use repeat=5 instead of the for loop? I can't see how to do it. help(timeit.Timer  repeat(self, repeat=3,

Re: [Tutor] List comprehension question

2010-11-12 Thread David Hutto
Apologies, missed that part. Didn't mean to seem rude. import timeit def anyName(): pass for num in range(10): t = timeit.Timer('anyName()','from __main__ import anyName') print t.repeat(repeat=5) #or import timeit def anyName(): pass t =

Re: [Tutor] List comprehension question

2010-11-12 Thread Richard D. Moores
On Fri, Nov 12, 2010 at 07:13, David Hutto smokefl...@gmail.com wrote: import timeit def anyName():        pass t = timeit.Timer('anyName()','from __main__ import anyName') print t.repeat(repeat=5) If I get the gist of what you're asking. Yes. That's it! Thank you! And if I don't

Re: [Tutor] List comprehension question

2010-11-12 Thread Steven D'Aprano
Richard D. Moores wrote: OK, but why can't I do what the timeit doc suggests, only put 2 or more functions in the file, as I do here: http://tutoree7.pastebin.com/84u1fkgA def test(): Stupid test function L = [] for i in range(100): L.append(i) if __name__=='__main__':

Re: [Tutor] List comprehension question

2010-11-12 Thread Richard D. Moores
On Fri, Nov 12, 2010 at 15:26, Steven D'Aprano st...@pearwood.info wrote: Richard D. Moores wrote: OK, but why can't I do what the timeit doc suggests, only put 2 or more functions in the file, as I do here: http://tutoree7.pastebin.com/84u1fkgA def test():    Stupid test function    L =

Re: [Tutor] List comprehension question

2010-11-12 Thread Richard D. Moores
On Fri, Nov 12, 2010 at 23:25, Richard D. Moores rdmoo...@gmail.com wrote: See http://tutoree7.pastebin.com/ Sorry, forgot to paste. http://tutoree7.pastebin.com/CYAm8arG Dick ___ Tutor maillist - Tutor@python.org To unsubscribe or change

Re: [Tutor] List comprehension question

2010-11-11 Thread Richard D. Moores
On Wed, Nov 10, 2010 at 01:30, Steven D'Aprano st...@pearwood.info wrote: Richard D. Moores wrote: def proper_divisors_sum(n):    return sum(list(divisors(n))) - n There's no need to call list first. sum() will happily operate on any sort of iterable -- lists, sums, iterators, generators,

Re: [Tutor] List comprehension question

2010-11-11 Thread Richard D. Moores
On Wed, Nov 10, 2010 at 02:35, Steven D'Aprano st...@pearwood.info wrote: Richard D. Moores wrote: On Tue, Nov 9, 2010 at 12:54, Steven D'Aprano st...@pearwood.info wrote: Richard D. Moores wrote: See http://tutoree7.pastebin.com/R82876Eg for a speed test with n = 100,000 and 100,000 loops

Re: [Tutor] List comprehension question

2010-11-11 Thread Steven D'Aprano
Richard D. Moores wrote: On Wed, Nov 10, 2010 at 01:30, Steven D'Aprano st...@pearwood.info wrote: P.S. don't take that as a put down -- you should be pleased that your code is around as fast as Tim Peter's code :) Nah. But where is Tim Peter's code? The timeit module was written by Tim

Re: [Tutor] List comprehension question

2010-11-11 Thread Stefan Behnel
Steven D'Aprano, 12.11.2010 06:07: Richard D. Moores wrote: On Wed, Nov 10, 2010 at 01:30, Steven D'Aprano wrote: P.S. don't take that as a put down -- you should be pleased that your code is around as fast as Tim Peter's code :) Nah. But where is Tim Peter's code? The timeit module was

Re: [Tutor] List comprehension question

2010-11-10 Thread Steven D'Aprano
Richard D. Moores wrote: def proper_divisors_sum(n): return sum(list(divisors(n))) - n There's no need to call list first. sum() will happily operate on any sort of iterable -- lists, sums, iterators, generators, range objects. Anything except strings, which would be pointless even if

Re: [Tutor] List comprehension question

2010-11-10 Thread Steven D'Aprano
Richard D. Moores wrote: On Tue, Nov 9, 2010 at 12:54, Steven D'Aprano st...@pearwood.info wrote: Richard D. Moores wrote: See http://tutoree7.pastebin.com/R82876Eg for a speed test with n = 100,000 and 100,000 loops As a general rule, you shouldn't try to roll your own speed tests. There

Re: [Tutor] List comprehension question

2010-11-09 Thread Richard D. Moores
On Mon, Nov 8, 2010 at 23:49, Richard D. Moores rdmoo...@gmail.com wrote: On Mon, Nov 8, 2010 at 22:47, Richard D. Moores rdmoo...@gmail.com wrote: On Mon, Nov 8, 2010 at 21:31, Richard D. Moores rdmoo...@gmail.com wrote: That sqrt(n) works for speeding up the finding of primes, but here we

Re: [Tutor] List comprehension question

2010-11-09 Thread Stefan Behnel
Richard D. Moores, 09.11.2010 12:07: That sqrt(n) works for speeding up the finding of primes, but here we want to use int(n/2) (and why didn't I think of that?), which results in about a 2x speedup. Seehttp://tutoree7.pastebin.com/dyRC8vuX. NO! Use int(n/2)+1 . I'll correct that in

Re: [Tutor] List comprehension question

2010-11-09 Thread Richard D. Moores
On Tue, Nov 9, 2010 at 03:35, Stefan Behnel stefan...@behnel.de wrote: Richard D. Moores, 09.11.2010 12:07: That sqrt(n) works for speeding up the finding of primes, but here we want to use int(n/2) (and why didn't I think of that?), which results in about a 2x speedup.

Re: [Tutor] List comprehension question

2010-11-09 Thread Martin A. Brown
Hello, : def proper_divisors_sum(n): : pd_list = [] : for x in range(1, int(n**.5)+1): : if n % x == 0: : factor = int(x) : pd_list.append(factor) : factor2 = int(n/factor) : pd_list.append(factor2) : pd_list =

Re: [Tutor] List comprehension question

2010-11-09 Thread Richard D. Moores
On Tue, Nov 9, 2010 at 05:51, Martin A. Brown mar...@linux-ip.net wrote: Hello,  : def proper_divisors_sum(n):  :     pd_list = []  :     for x in range(1, int(n**.5)+1):  :         if n % x == 0:  :             factor = int(x)  :             pd_list.append(factor)  :             factor2

Re: [Tutor] List comprehension question

2010-11-09 Thread Albert-Jan Roskam
, and public health, what have the Romans ever done for us? ~~ From: Steven D'Aprano st...@pearwood.info To: tutor@python.org Sent: Tue, November 9, 2010 9:54:24 PM Subject: Re: [Tutor] List

Re: [Tutor] List comprehension question

2010-11-09 Thread Richard D. Moores
On Tue, Nov 9, 2010 at 12:54, Steven D'Aprano st...@pearwood.info wrote: Richard D. Moores wrote: See http://tutoree7.pastebin.com/R82876Eg for a speed test with n = 100,000 and 100,000 loops As a general rule, you shouldn't try to roll your own speed tests. There are various subtleties

Re: [Tutor] List comprehension question

2010-11-09 Thread Alan Gauld
Richard D. Moores rdmoo...@gmail.com wrote Question: When I dump in more that one line of any code (properly indented), after running it once, I've never known how to run it again without a redump. The up arrow just gets me one line at a time. So, how to do it? It depends on your IDE. On

Re: [Tutor] List comprehension question

2010-11-09 Thread C or L Smith
1. Re: List comprehension question (Richard D. Moores) ?: def proper_divisors_sum(n): A few questions--noting, of course, that I'm not reading this with an eye toward performance, which it seems you are, but these occur to me: Tim Peters had a beautiful little version of divisors at

Re: [Tutor] List comprehension question

2010-11-09 Thread Richard D. Moores
On Tue, Nov 9, 2010 at 18:29, C or L Smith smi...@worksmail.net wrote: From sympy you simply do: from sympy import divisors list(divisors(256)) [1, 2, 4, 8, 16, 32, 64, 128, 256] So your proper divisors would just be sum(divisors(n)) - n. The divisors function there is in the

Re: [Tutor] List comprehension question

2010-11-09 Thread Stefan Behnel
Richard D. Moores, 10.11.2010 08:24: def proper_divisors_sum(n): pd = set((1,)) for x in range(2, int(n**.5)+1): if n % x == 0: pd.update((x, n//x)) return sum(list(pd)) You keep using redundant operations. What sum(list(s)) does, for s being a set, is:

Re: [Tutor] List comprehension question

2010-11-08 Thread Steven D'Aprano
Richard D. Moores wrote: So this version of my function uses a generator, range(), no? def proper_divisors(n): sum_ = 0 for x in range(1,n): if n % x == 0: sum_ += x return sum_ I'm going to be pedantic here... but to quote the Middleman, specificity is the

Re: [Tutor] List comprehension question

2010-11-08 Thread Alan Gauld
Steven D'Aprano st...@pearwood.info wrote I'm going to be pedantic here... but to quote the Middleman, specificity is the soul of all good communication. Be pedantic! :-) I really liked the explanation although I already sort of knew most of it. But there were a few nuggets in there I'd

Re: [Tutor] List comprehension question

2010-11-08 Thread Stefan Behnel
Alan Gauld, 08.11.2010 17:28: Steven D'Aprano wrote def proper_divisors(n): return sum(x for x in range(1, int(math.sqrt(n))) if n%x == 0) Why use math.sqrt() instead of just using the ** operator? return sum(x for x in range(1, int(n**0.5)) if n%x == 0) I'd have expected ** to be

Re: [Tutor] List comprehension question

2010-11-08 Thread Stefan Behnel
Hugo Arts, 08.11.2010 00:53: On Mon, Nov 8, 2010 at 12:36 AM, Richard D. Moores wrote: def proper_divisors(n): Return the sum of the proper divisors of positive integer n return sum([x for x in range(1,n) if int(n/x) == n/x]) The list comprehension is this function is

Re: [Tutor] List comprehension question

2010-11-08 Thread Alan Gauld
Stefan Behnel stefan...@behnel.de wrote Why use math.sqrt() instead of just using the ** operator? return sum(x for x in range(1, int(n**0.5)) if n%x == 0) Since this operation is only evaluated once in the whole runtime of the loop, I think readability beats the likely very tiny

Re: [Tutor] List comprehension question

2010-11-08 Thread Alan Gauld
Stefan Behnel stefan...@behnel.de wrote On another note, getting rid of the list comprehension and using a generator expression will be even faster, since you won't have to build the list. I gave this suggestion a try. It is true for me when run in CPython 3.2: However, it is no longer

Re: [Tutor] List comprehension question

2010-11-08 Thread Steven D'Aprano
Alan Gauld wrote: Steven D'Aprano st...@pearwood.info wrote I'm going to be pedantic here... but to quote the Middleman, specificity is the soul of all good communication. Be pedantic! :-) I really liked the explanation although I already sort of knew most of it. But there were a few

Re: [Tutor] List comprehension question

2010-11-08 Thread Steven D'Aprano
Stefan Behnel wrote: Hugo Arts, 08.11.2010 00:53: [...] On another note, getting rid of the list comprehension and using a generator expression will be even faster, since you won't have to build the list. I gave this suggestion a try. It is true for me when run in CPython 3.2: [...]

Re: [Tutor] List comprehension question

2010-11-08 Thread Richard D. Moores
On Mon, Nov 8, 2010 at 03:43, Steven D'Aprano st...@pearwood.info wrote: Richard D. Moores wrote: Coming back to your function: def proper_divisors(n):    sum_ = 0    for x in range(1,n):        if n % x == 0:            sum_ += x    return sum_ we can write that much more simply:

Re: [Tutor] List comprehension question

2010-11-08 Thread Stefan Behnel
Richard D. Moores, 09.11.2010 06:31: On Mon, Nov 8, 2010 at 03:43, Steven D'Aprano wrote: Richard D. Moores wrote: Coming back to your function: def proper_divisors(n): sum_ = 0 for x in range(1,n): if n % x == 0: sum_ += x return sum_ we can write that much

Re: [Tutor] List comprehension question

2010-11-08 Thread Richard D. Moores
On Mon, Nov 8, 2010 at 21:31, Richard D. Moores rdmoo...@gmail.com wrote: That sqrt(n) works for speeding up the finding of primes, but here we want to use int(n/2) (and why didn't I think of that?), which results in about a 2x speedup. See http://tutoree7.pastebin.com/dyRC8vuX. NO! Use

Re: [Tutor] List comprehension question

2010-11-08 Thread Richard D. Moores
On Mon, Nov 8, 2010 at 22:47, Richard D. Moores rdmoo...@gmail.com wrote: On Mon, Nov 8, 2010 at 21:31, Richard D. Moores rdmoo...@gmail.com wrote: That sqrt(n) works for speeding up the finding of primes, but here we want to use int(n/2) (and why didn't I think of that?), which results in

[Tutor] List comprehension question

2010-11-07 Thread Richard D. Moores
def proper_divisors(n): Return the sum of the proper divisors of positive integer n return sum([x for x in range(1,n) if int(n/x) == n/x]) The list comprehension is this function is inefficient in that it computes n/x twice. I'd like to do an a = n/x and use a in if int(a) ==

Re: [Tutor] List comprehension question

2010-11-07 Thread Hugo Arts
On Mon, Nov 8, 2010 at 12:36 AM, Richard D. Moores rdmoo...@gmail.com wrote: def proper_divisors(n):         Return the sum of the proper divisors of positive integer n         return sum([x for x in range(1,n) if int(n/x) == n/x]) The list comprehension is this function is inefficient in

Re: [Tutor] List comprehension question

2010-11-07 Thread Richard D. Moores
On Sun, Nov 7, 2010 at 15:53, Hugo Arts hugo.yo...@gmail.com wrote: On Mon, Nov 8, 2010 at 12:36 AM, Richard D. Moores rdmoo...@gmail.com wrote: def proper_divisors(n):         Return the sum of the proper divisors of positive integer n         return sum([x for x in range(1,n) if int(n/x)

Re: [Tutor] List comprehension question

2010-11-07 Thread Wayne Werner
On Sun, Nov 7, 2010 at 6:31 PM, Hugo Arts hugo.yo...@gmail.com wrote: snip here's a list comprehension a = [x*2 for x in range(10)] a [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] here's the equivalent generator expression: a = (x*2 for x in range(10)) snip Since you're talking about

Re: [Tutor] List comprehension question

2010-11-07 Thread Alan Gauld
Hugo Arts hugo.yo...@gmail.com wrote Yes. A cast or typecast means converting some data to a different type, like converting floats to integers, strings to integers, The term cast can be misleading however since in some languages - those decended from C it means treating a piece of data as

Re: [Tutor] List comprehension question

2010-11-07 Thread Richard D. Moores
On Sun, Nov 7, 2010 at 16:41, Wayne Werner waynejwer...@gmail.com wrote: On Sun, Nov 7, 2010 at 6:31 PM, Hugo Arts hugo.yo...@gmail.com wrote: snip here's a list comprehension a = [x*2 for x in range(10)] a [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] here's the equivalent generator expression:

Re: [Tutor] List comprehension question

2010-11-07 Thread Wayne Werner
On Sun, Nov 7, 2010 at 7:15 PM, Richard D. Moores rdmoo...@gmail.comwrote: On Sun, Nov 7, 2010 at 16:41, Wayne Werner waynejwer...@gmail.com wrote: On Sun, Nov 7, 2010 at 6:31 PM, Hugo Arts hugo.yo...@gmail.com wrote: snip I should have mentioned that I'm using 3.1 . So this version of my

Re: [Tutor] List comprehension question

2010-11-07 Thread Richard D. Moores
On Sun, Nov 7, 2010 at 17:47, Wayne Werner waynejwer...@gmail.com wrote: On Sun, Nov 7, 2010 at 7:15 PM, Richard D. Moores rdmoo...@gmail.com wrote: On Sun, Nov 7, 2010 at 16:41, Wayne Werner waynejwer...@gmail.com wrote: On Sun, Nov 7, 2010 at 6:31 PM, Hugo Arts hugo.yo...@gmail.com wrote:

Re: [Tutor] list of dict question

2010-10-12 Thread Francesco Loffredo
On 11/10/2010 19.23, Alan Gauld wrote: ... HTH, Sure it did! Very enlightening, Alan. THANK YOU! Nessun virus nel messaggio in uscita. Controllato da AVG - www.avg.com Versione: 9.0.862 / Database dei virus: 271.1.1/3190 - Data di rilascio: 10/11/10 08:34:00

Re: [Tutor] list of dict question

2010-10-11 Thread Francesco Loffredo
Thank you, Alan and Dave, for your spotting this weak point in my understanding of Python! On 11/10/2010 2.11, Dave Angel wrote: On 2:59 PM, Alan Gauld wrote: Francesco Loffredo f...@libero.it wrote did, Roelof's code would work perfectly, and you could store in a list all the subsequent

Re: [Tutor] list of dict question

2010-10-11 Thread Alan Gauld
Francesco Loffredo f...@libero.it wrote lst = [] for n in range(3): obj = {} I didn't know that this creates a new obj if obj already exists, I thought it would just update it. That's my mistake. Yes you have to remember that in Python, unlike C etc, names are not aliases for memory

Re: [Tutor] list of dict question

2010-10-10 Thread Francesco Loffredo
On 09/10/2010 10.25, Alan Gauld wrote: Francesco Loffredo f...@libero.it wrote On the next iteration you overwrite those two dictionaries with new values then append them to the list again. So you wind up with 2 copies of the updated dictionaries. ... This is difficult for me too: why does

Re: [Tutor] list of dict question

2010-10-10 Thread Alan Gauld
Francesco Loffredo f...@libero.it wrote did, Roelof's code would work perfectly, and you could store in a list all the subsequent changes of a dictionary without calling them with different names. You don;'t need dfifferent names. Provided the name creates a new object inside the loop you

Re: [Tutor] list of dict question

2010-10-10 Thread Dave Angel
On 2:59 PM, Alan Gauld wrote: Francesco Loffredo f...@libero.it wrote did, Roelof's code would work perfectly, and you could store in a list all the subsequent changes of a dictionary without calling them with different names. You don;'t need dfifferent names. Provided the name creates a

[Tutor] list of dict question

2010-10-09 Thread Francesco Loffredo
Alan's answer to Roelof made me think... On 08/10/2010 13.40, Francesco Loffredo wrote: Il 08/10/2010 10.02, Alan Gauld ha scritto: Roelof Wobben rwob...@hotmail.com wrote I have this programm : tournooi = [{'thuis': 'A','uit': B,'thuisscore': 20, 'uitscore': ... This was your answer to

Re: [Tutor] list of dict question

2010-10-09 Thread Steven D'Aprano
On Sat, 9 Oct 2010 06:05:57 pm Francesco Loffredo wrote: Alan's answer to Roelof made me think... I'm sorry, I don't know what your question is. You seem to have quoted various bits and pieces of text from earlier emails (text beginning with signs). Apart from the sentence beginning with

<    1   2   3   4   5   6   7   >