[Tutor] Looking for duplicates within a list

2010-06-11 Thread Ken G.
I have been working on this problem for several days and I am not making 
any progress.  I have a group of 18 number, in ascending order, within a 
list.  They ranged from 1 to 39.  Some numbers are duplicated as much as 
three times or as few as none.


I started with one list containing the numbers.  For example, they are 
listed as like below:


a = [1, 2, 3, 3, 4]

I started off with using a loop:

   for j in range (0, 5):
   x = a[0] # for example, 1

How would I compare '1' with 2, 3, 3, 4? 

Do I need another duplicated list such as b = a and compare a[0] with 
either b[0], b[1], b[2], b[3], b[4]?


Or do I compare a[0] with a[1], a[2], a[3], a[4]?

In any event, if a number is listed more than once, I would like to know 
how many times, such as 2 or 3 times.  For example, '3' is listed twice 
within a list.


TIA,

Ken








___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list

2010-06-11 Thread Alex Hall
On 6/11/10, Ken G.  wrote:
> I have been working on this problem for several days and I am not making
> any progress.  I have a group of 18 number, in ascending order, within a
> list.  They ranged from 1 to 39.  Some numbers are duplicated as much as
> three times or as few as none.
FYI, Python's "set" data type will let you have a list and never have
a repeat. I know that is not your goal now, but if you want to remove
duplicates, it seems like a good choice.
>
> I started with one list containing the numbers.  For example, they are
> listed as like below:
>
> a = [1, 2, 3, 3, 4]
>
> I started off with using a loop:
>
> for j in range (0, 5):
> x = a[0] # for example, 1
>
> How would I compare '1' with 2, 3, 3, 4?
>
> Do I need another duplicated list such as b = a and compare a[0] with
> either b[0], b[1], b[2], b[3], b[4]?
>
> Or do I compare a[0] with a[1], a[2], a[3], a[4]?
A couple points here. First, you will want to make life easier by
saying range(0, len(a)) so that the loop will work no matter the size
of a.
Second, for comparing a list to itself, here is a rather inefficient,
though simple, way:

for i in range(0, len(a)):
 x=a[i]
 for j in range(0, len(a)):
  y=a[j]
  if(x==y and i!=j): #match since a[i]==a[j] and i and j are not the
same index of a
>
> In any event, if a number is listed more than once, I would like to know
> how many times, such as 2 or 3 times.  For example, '3' is listed twice
> within a list.
Do not quote me here, but I think sets may be able to tell you that as well.
>
> TIA,
>
> Ken
>
>
>
>
>
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list

2010-06-11 Thread Ken G.

vijay wrote:

Check out this code
 l= [1, 2, 3, 3, 4]
 d={}
 for item in l:
 d.setdefaut(item,0)
 d[item] +=1
print d
{1: 1, 2: 1, 3: 2, 4: 1}


with regard's
vijay



Thanks.  Very interesting concept.

Ken
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list

2010-06-11 Thread Ken G.

Alex Hall wrote:

On 6/11/10, Ken G.  wrote:
  

I have been working on this problem for several days and I am not making
any progress.  I have a group of 18 number, in ascending order, within a
list.  They ranged from 1 to 39.  Some numbers are duplicated as much as
three times or as few as none.


FYI, Python's "set" data type will let you have a list and never have
a repeat. I know that is not your goal now, but if you want to remove
duplicates, it seems like a good choice.
  

I started with one list containing the numbers.  For example, they are
listed as like below:

a = [1, 2, 3, 3, 4]

I started off with using a loop:

for j in range (0, 5):
x = a[0] # for example, 1

How would I compare '1' with 2, 3, 3, 4?

Do I need another duplicated list such as b = a and compare a[0] with
either b[0], b[1], b[2], b[3], b[4]?

Or do I compare a[0] with a[1], a[2], a[3], a[4]?


A couple points here. First, you will want to make life easier by
saying range(0, len(a)) so that the loop will work no matter the size
of a.
Second, for comparing a list to itself, here is a rather inefficient,
though simple, way:

for i in range(0, len(a)):
 x=a[i]
 for j in range(0, len(a)):
  y=a[j]
  if(x==y and i!=j): #match since a[i]==a[j] and i and j are not the
same index of a
  

In any event, if a number is listed more than once, I would like to know
how many times, such as 2 or 3 times.  For example, '3' is listed twice
within a list.


Do not quote me here, but I think sets may be able to tell you that as well.
  

TIA,

Ken


Thank you for your contribution.  As seen here, I have much to learn.

Ken

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list

2010-06-11 Thread Sander Sweers
On 11 June 2010 15:57, Ken G.  wrote:
> In any event, if a number is listed more than once, I would like to know how
> many times, such as 2 or 3 times.  For example, '3' is listed twice within a
> list.

If you do not have top keep the order of the number this will work.

>>> a = [1, 2, 3, 3, 4]
>>> counted = {}
>>> for n in a:
if not n in counted:
counted[n] = 1
else:
counted[n] += 1

>>> counted
{1: 1, 2: 1, 3: 2, 4: 1}

>>> for x, y in counted.items():
if y > 1:
print "Number %s was found %s times" % (x, y)
else:
print "Number %s was found %s time" % (x, y)

Number 1 was found 1 time
Number 2 was found 1 time
Number 3 was found 2 times
Number 4 was found 1 time

Greets
Sander
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list

2010-06-11 Thread Jose Amoreira
On Friday, June 11, 2010 02:57:34 pm Ken G. wrote:
> I have been working on this problem for several days and I am not making
> any progress.  I have a group of 18 number, in ascending order, within a
> list.  They ranged from 1 to 39.  Some numbers are duplicated as much as
> three times or as few as none.
> 
> I started with one list containing the numbers.  For example, they are
> listed as like below:
> 
> a = [1, 2, 3, 3, 4]
> 
> I started off with using a loop:
> 
> for j in range (0, 5):
> x = a[0] # for example, 1
> 
> How would I compare '1' with 2, 3, 3, 4?
> 
> Do I need another duplicated list such as b = a and compare a[0] with
> either b[0], b[1], b[2], b[3], b[4]?
> 
> Or do I compare a[0] with a[1], a[2], a[3], a[4]?
> 
> In any event, if a number is listed more than once, I would like to know
> how many times, such as 2 or 3 times.  For example, '3' is listed twice
> within a list.
> 
> TIA,
>

I would do it with a dictionary:
def reps(lst):
dict = {}
for item in lst:
if item in dict:
dict[item] += 1
else:
dict[item] = 1
return dict

This function returns a dictionary with of the number of times each value in 
the list is repeated. Even shorter using dict.setdefault:

def reps(lst):
dict={}
for item in lst:
dict[item] = dict.setdefault(item,0) + 1
return dict

For instance, if lst=[1,2,2,2,4,4,5], then reps(lst) returns
{1: 1, 2: 3, 4: 2, 5: 1}

Using the fact that the list is ordered, one can design a more efficient 
solution (go through the list; if this item is equal to the previous, then it 
is repeated, else, it is a new value). But you list is short enough for this 
direct approach to work.
Hope this helps. Cheers,
Jose
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list

2010-06-11 Thread Alan Gauld


"Ken G."  wrote

In any event, if a number is listed more than once, I would like to 
know how many times, such as 2 or 3 times.  For example, '3' is 
listed twice within a list.


Have you looked at the count method of lists?

Something like:

counts = set(( item, mylist.count(item)) for item in mylist if 
mylist.count(item) > 1)


Seems to work...

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list [SOLVED]

2010-06-11 Thread Ken G.

Sander Sweers wrote:

On 11 June 2010 15:57, Ken G.  wrote:
  

In any event, if a number is listed more than once, I would like to know how
many times, such as 2 or 3 times.  For example, '3' is listed twice within a
list.



If you do not have top keep the order of the number this will work.

  

a = [1, 2, 3, 3, 4]
counted = {}
for n in a:


if not n in counted:
counted[n] = 1
else:
counted[n] += 1

  

counted


{1: 1, 2: 1, 3: 2, 4: 1}

  

for x, y in counted.items():


if y > 1:
print "Number %s was found %s times" % (x, y)
else:
print "Number %s was found %s time" % (x, y)

Number 1 was found 1 time
Number 2 was found 1 time
Number 3 was found 2 times
Number 4 was found 1 time

Greets
Sander
  

That works great!  Thanks!

Ken
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list [SOLVED]

2010-06-11 Thread Ken G.

Jose Amoreira wrote:

On Friday, June 11, 2010 02:57:34 pm Ken G. wrote:
  

I have been working on this problem for several days and I am not making
any progress.  I have a group of 18 number, in ascending order, within a
list.  They ranged from 1 to 39.  Some numbers are duplicated as much as
three times or as few as none.

I started with one list containing the numbers.  For example, they are
listed as like below:

a = [1, 2, 3, 3, 4]

I started off with using a loop:

for j in range (0, 5):
x = a[0] # for example, 1

How would I compare '1' with 2, 3, 3, 4?

Do I need another duplicated list such as b = a and compare a[0] with
either b[0], b[1], b[2], b[3], b[4]?

Or do I compare a[0] with a[1], a[2], a[3], a[4]?

In any event, if a number is listed more than once, I would like to know
how many times, such as 2 or 3 times.  For example, '3' is listed twice
within a list.

TIA,




I would do it with a dictionary:
def reps(lst):
dict = {}
for item in lst:
if item in dict:
dict[item] += 1
else:
dict[item] = 1
return dict

This function returns a dictionary with of the number of times each value in 
the list is repeated. Even shorter using dict.setdefault:


def reps(lst):
dict={}
for item in lst:
dict[item] = dict.setdefault(item,0) + 1
return dict

For instance, if lst=[1,2,2,2,4,4,5], then reps(lst) returns
{1: 1, 2: 3, 4: 2, 5: 1}

Using the fact that the list is ordered, one can design a more efficient 
solution (go through the list; if this item is equal to the previous, then it 
is repeated, else, it is a new value). But you list is short enough for this 
direct approach to work.

Hope this helps. Cheers,
Jose

  

Thanks.  I will be studying your approach.  Thanks all.

Ken
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list [SOLVED]

2010-06-11 Thread Ken G.

Alan Gauld wrote:


"Ken G."  wrote

In any event, if a number is listed more than once, I would like to 
know how many times, such as 2 or 3 times.  For example, '3' is 
listed twice within a list.


Have you looked at the count method of lists?

Something like:

counts = set(( item, mylist.count(item)) for item in mylist if 
mylist.count(item) > 1)


Seems to work...

HTH,



Whee, this is great!  I learned a lot today.  Back to playing and studying.

Ken
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list

2010-06-11 Thread Dave Angel

Ken G. wrote:
I have been working on this problem for several days and I am not 
making any progress.  I have a group of 18 number, in ascending order, 
within a list.  They ranged from 1 to 39.  Some numbers are duplicated 
as much as three times or as few as none.


I started with one list containing the numbers.  For example, they are 
listed as like below:


a = [1, 2, 3, 3, 4]

I started off with using a loop:

   for j in range (0, 5):
   x = a[0] # for example, 1

How would I compare '1' with 2, 3, 3, 4?
Do I need another duplicated list such as b = a and compare a[0] with 
either b[0], b[1], b[2], b[3], b[4]?


Or do I compare a[0] with a[1], a[2], a[3], a[4]?

In any event, if a number is listed more than once, I would like to 
know how many times, such as 2 or 3 times.  For example, '3' is listed 
twice within a list.


TIA,

Ken

I'm a bit surprised nobody has mentioned the obvious solution -- another 
list of size 40, each of which represents the number of times  a 
particular number has appeared.


(Untested)


a = [1, 2, 3, 3, 4]
counts = [0] * 40
for item in a:
counts[item] += 1

Now, if you want to know how many times 3 appears, simply
  print counts[3]

The only downside to this is if the range of possible values is large, 
or non-numeric.  In either of those cases, go back to the default 
dictionary.


DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list

2010-06-11 Thread Steven D'Aprano
On Sat, 12 Jun 2010 12:58:19 am Alan Gauld wrote:

> Have you looked at the count method of lists?
>
> Something like:
>
> counts = set(( item, mylist.count(item)) for item in mylist if
> mylist.count(item) > 1)

That's a Shlemiel the Painter algorithm.

http://www.joelonsoftware.com/articles/fog000319.html


> Seems to work...

You say that now, but one day you will use it on a list of 100,000 
items, and you'll wonder why it takes 45 minutes to finish, and curse 
Python for being slow.





-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list [SOLVED]

2010-06-11 Thread Ken G.

Dave Angel wrote:

Ken G. wrote:
I have been working on this problem for several days and I am not 
making any progress.  I have a group of 18 number, in ascending 
order, within a list.  They ranged from 1 to 39.  Some numbers are 
duplicated as much as three times or as few as none.


I started with one list containing the numbers.  For example, they 
are listed as like below:


a = [1, 2, 3, 3, 4]

I started off with using a loop:

   for j in range (0, 5):
   x = a[0] # for example, 1

How would I compare '1' with 2, 3, 3, 4?
Do I need another duplicated list such as b = a and compare a[0] with 
either b[0], b[1], b[2], b[3], b[4]?


Or do I compare a[0] with a[1], a[2], a[3], a[4]?

In any event, if a number is listed more than once, I would like to 
know how many times, such as 2 or 3 times.  For example, '3' is 
listed twice within a list.


TIA,

Ken

I'm a bit surprised nobody has mentioned the obvious solution -- 
another list of size 40, each of which represents the number of times  
a particular number has appeared.


(Untested)


a = [1, 2, 3, 3, 4]
counts = [0] * 40
for item in a:
counts[item] += 1

Now, if you want to know how many times 3 appears, simply
  print counts[3]

The only downside to this is if the range of possible values is large, 
or non-numeric.  In either of those cases, go back to the default 
dictionary.


DaveA




This will be look into.  Mucho thanks.

Ken

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list [SOLVED]

2010-06-11 Thread Ken G.


Steven D'Aprano wrote:

On Sat, 12 Jun 2010 12:58:19 am Alan Gauld wrote:

  

Have you looked at the count method of lists?

Something like:

counts = set(( item, mylist.count(item)) for item in mylist if
mylist.count(item) > 1)



That's a Shlemiel the Painter algorithm.

http://www.joelonsoftware.com/articles/fog000319.html


  

Seems to work...



You say that now, but one day you will use it on a list of 100,000 
items, and you'll wonder why it takes 45 minutes to finish, and curse 
Python for being slow.
  

Hee, hee.  Will investigate further.  Thanks.

Ken

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list

2010-06-11 Thread Hugo Arts
On 11 jun 2010, at 17:49, Steven D'Aprano  wrote:

> On Sat, 12 Jun 2010 12:58:19 am Alan Gauld wrote:
>
>> Have you looked at the count method of lists?
>>
>> Something like:
>>
>> counts = set(( item, mylist.count(item)) for item in mylist if
>> mylist.count(item) > 1)
>
> That's a Shlemiel the Painter algorithm.
>
> http://www.joelonsoftware.com/articles/fog000319.html
>

It is, but it's also very elegant and simple to understand. And it
works fine on small inputs. Everything is fast for small n.

>
>> Seems to work...
>
> You say that now, but one day you will use it on a list of 100,000
> items, and you'll wonder why it takes 45 minutes to finish, and curse
> Python for being slow.
>

Actually, now that you know it is a shlemiel the painter's algorithm,
you won't have to wonder anymore. And you'll just say: "well, this
piece of code might need to handle huge lists someday, I'll use a
dictionary."

I guess what I'm trying to say is: using code that performs bad in
situations that won't be encountered anyway is not inherently bad.
Otherwise, we'd all still be writing everything in C.

The corrolary, of course, is that you should always know what the
performance characteristics of your code are, and what kind of data it
will handle.

Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list [SOLVED]

2010-06-11 Thread davidheiserca
How about this?

List = [1, 2, 3, 3, 3, 4, 5, 5]
for Item in list(set(List)):
print Item, List.count(Item)


  - Original Message - 
  From: Ken G. 
  To: Steven D'Aprano 
  Cc: tutor@python.org 
  Sent: Friday, June 11, 2010 9:09 AM
  Subject: Re: [Tutor] Looking for duplicates within a list [SOLVED]



  Steven D'Aprano wrote: 
On Sat, 12 Jun 2010 12:58:19 am Alan Gauld wrote:

  Have you looked at the count method of lists?

Something like:

counts = set(( item, mylist.count(item)) for item in mylist if
mylist.count(item) > 1)

That's a Shlemiel the Painter algorithm.

http://www.joelonsoftware.com/articles/fog000319.html


  Seems to work...

You say that now, but one day you will use it on a list of 100,000 
items, and you'll wonder why it takes 45 minutes to finish, and curse 
Python for being slow.
  Hee, hee.  Will investigate further.  Thanks.

Ken


--


  ___
  Tutor maillist  -  Tutor@python.org
  To unsubscribe or change subscription options:
  http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list

2010-06-11 Thread Steven D'Aprano
On Sat, 12 Jun 2010 02:18:52 am Hugo Arts wrote:
> On 11 jun 2010, at 17:49, Steven D'Aprano  wrote:
> > On Sat, 12 Jun 2010 12:58:19 am Alan Gauld wrote:
> >> Have you looked at the count method of lists?
> >>
> >> Something like:
> >>
> >> counts = set(( item, mylist.count(item)) for item in mylist if
> >> mylist.count(item) > 1)
> >
> > That's a Shlemiel the Painter algorithm.
> >
> > http://www.joelonsoftware.com/articles/fog000319.html
>
> It is, but it's also very elegant and simple to understand. 

Your idea of elegant and simple is not the same as mine. To me, I see 
unnecessary work and unneeded complexity. A generator expression for a 
beginner having trouble with the basics? Calling mylist.count(item) 
*twice* for every item?! How is that possibly elegant?



> And it 
> works fine on small inputs. Everything is fast for small n.

[pedantic]
Not everything. Some things are inherently slow, for example:

http://en.wikipedia.org/wiki/Busy_beaver

A five-state Busy Beaver machine takes 47,176,870 steps to return, and a 
six-state Busy Beaver takes 10**21132 steps to return.

A slightly less extreme example is Ackermann's Function:

http://en.wikipedia.org/wiki/Ackermann's_function

where (for example) A(4,2) has over 19,000 digits. Calculating the 
number of recursive steps needed to calculate that value is left as an 
exercise.
[/pedantic]

These are extreme examples, but the point is that there are tasks which 
are hard even for small N. "Find N needles in a haystack" remains hard 
even for N=1.

(And before you suggest using a magnet, it's a *plastic* needle.)


[...]
> I guess what I'm trying to say is: using code that performs bad in
> situations that won't be encountered anyway is not inherently bad.

The problem is that situations that won't be encountered often *are* 
encountered, long after the coder who introduced the poorly-performing 
algorithm has moved on.

Here's a real-life example, from Python itself: last September, on the 
Python-Dev mailing list, Chris Withers reported a problem downloading a 
file using Python's httplib module. For a file that took wget or 
Internet Explorer approximately 2 seconds to download, it took Python 
up to thirty MINUTES -- that's nearly a thousand times slower.

Eventually Chris, with the help of others on the list, debugged the 
problem down to a Shlemiel algorithm in the httplib code. Somebody 
found a comment in the _read_chunked method that said:

  # XXX This accumulates chunks by repeated string concatenation,
  # which is not efficient as the number or size of chunks gets big.

So somebody used an algorithm which they KNEW was inefficient and slow, 
it had been there for years, affecting who knows how many people, until 
eventually somebody was annoyed sufficiently to do something about it. 
And the sad thing is that avoiding repeated string concatenation is 
easy and there was no need for it in the first place.


> Otherwise, we'd all still be writing everything in C.

You've missed the point. It's not the language, you can write poorly 
performing code in any language, and an O(N) algorithm in a slow 
language will probably out-perform an O(N**2) or O(2**N) algorithm in a 
fast language.


-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list

2010-06-11 Thread Steven D'Aprano
On Fri, 11 Jun 2010 11:57:34 pm Ken G. wrote:
> I have been working on this problem for several days and I am not
> making any progress.  I have a group of 18 number, in ascending
> order, within a list.  They ranged from 1 to 39.  Some numbers are
> duplicated as much as three times or as few as none.


To find out how many times each number is in the list:

counts = {}
for i in mylist:
# Don't do the work if you've already done it.
if i not in counts:  
counts[i] = mylist.count(i)


Can we do better? For very small lists, probably not, because the 
count() method is implemented in fast C, and anything we write will be 
in slow-ish Python. But for larger lists, count() is wasteful, because 
every time you call it, it walks the entire length of the string from 
start to finish. Since we know the list is sorted, that's a rubbish 
strategy. Let's write a quick helper function:

# Untested
def count_equals(alist, start):
"""Count the number of consecutive items of alist equal 
to the item at start. Returns the count and the next 
place to start."""
item = alist[start]
count = 1
for i in xrange(start+1, len(alist)):
x = alist[i]
if x == item: 
count += 1
else:
return (count, i)
return (count, len(alist))


Now, with this we can avoid wastefully starting from the beginning of 
the list each time. (But remember, this will only be worthwhile for 
sufficiently large lists. My guess, and this is only a guess, is that 
it won't be worthwhile for lists smaller than perhaps 1,000 items.)

counts = {}
i = 0
while i < len(mylist):
count, i = count_equals(mylist, i)
counts[i] = count


> I started with one list containing the numbers.  For example, they
> are listed as like below:
>
> a = [1, 2, 3, 3, 4]
>
> I started off with using a loop:
>
> for j in range (0, 5):
> x = a[0] # for example, 1
>
> How would I compare '1' with 2, 3, 3, 4?

# Untested
counts = {}
for j in range(len(a)):
x = a[j]
count = 1
for k in range(j+1, len(a)):
if x == a[k]:
count += 1
else:
counts[x] = count
break


> Do I need another duplicated list such as b = a and compare a[0] with
> either b[0], b[1], b[2], b[3], b[4]?

b = a doesn't create a duplicated list, it gives the same list a second 
name. Watch this:

>>> a = [1, 2, 3]
>>> b = a
>>> b.append("Surprise!")
>>> a
[1, 2, 3, 'Surprise!']

b and a both refer to the same object, the list [1,2,3,'Surprise!']. To 
make a copy of the list, you need:

b = a[:]

But why bother, if all you're doing is comparisons and not modifying it?



-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list [SOLVED]

2010-06-11 Thread ALAN GAULD


> > counts = 
> > set(( item, mylist.count(item)) for item in mylist if mylist.count(item) > 
> Whee, this is great!  I learned a lot today.  

I should have added that although thats a one liner in code terms it does 
involve iterating over the list twice - in count() - for each element. So it 
might not be very fast for big lists, certainly there are more efficient 
solutions if you don't mind writing more code.

But for most lists you will likely find it is fast enough, certainly for 18 
items!

Alan G.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list [SOLVED]

2010-06-11 Thread Ken G.

Hugo Arts wrote:

On 11 jun 2010, at 17:49, Steven D'Aprano  wrote:

  

On Sat, 12 Jun 2010 12:58:19 am Alan Gauld wrote:



Have you looked at the count method of lists?

Something like:

counts = set(( item, mylist.count(item)) for item in mylist if
mylist.count(item) > 1)
  

That's a Shlemiel the Painter algorithm.

http://www.joelonsoftware.com/articles/fog000319.html




It is, but it's also very elegant and simple to understand. And it
works fine on small inputs. Everything is fast for small n.

  

Seems to work...
  

You say that now, but one day you will use it on a list of 100,000
items, and you'll wonder why it takes 45 minutes to finish, and curse
Python for being slow.




Actually, now that you know it is a shlemiel the painter's algorithm,
you won't have to wonder anymore. And you'll just say: "well, this
piece of code might need to handle huge lists someday, I'll use a
dictionary."

I guess what I'm trying to say is: using code that performs bad in
situations that won't be encountered anyway is not inherently bad.
Otherwise, we'd all still be writing everything in C.

The corrolary, of course, is that you should always know what the
performance characteristics of your code are, and what kind of data it
will handle.

Hugo

  

I appreciate your input.  Thanks!

Ken
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list [SOLVED]

2010-06-11 Thread Ken G.

davidheise...@gmail.com wrote:

How about this?
 
List = [1, 2, 3, 3, 3, 4, 5, 5]

for Item in list(set(List)):
print Item, List.count(Item)
 
 


- Original Message -
*From:* Ken G. 
*To:* Steven D'Aprano 
*Cc:* tutor@python.org 
*Sent:* Friday, June 11, 2010 9:09 AM
*Subject:* Re: [Tutor] Looking for duplicates within a list [SOLVED]


Steven D'Aprano wrote:

On Sat, 12 Jun 2010 12:58:19 am Alan Gauld wrote:

  

Have you looked at the count method of lists?

Something like:

counts = set(( item, mylist.count(item)) for item in mylist if
mylist.count(item) > 1)



That's a Shlemiel the Painter algorithm.

http://www.joelonsoftware.com/articles/fog000319.html


  

Seems to work...



You say that now, but one day you will use it on a list of 100,000 
items, and you'll wonder why it takes 45 minutes to finish, and curse 
Python for being slow.
  

Hee, hee.  Will investigate further.  Thanks.

Ken




___


Oh, a nice one.  Many thanks.

Ken
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list [SOLVED]

2010-06-11 Thread Alan Gauld
 wrote 


How about this?

List = [1, 2, 3, 3, 3, 4, 5, 5]
for Item in list(set(List)):
print Item, List.count(Item)


Not bad and you don't need the convert back to list()
But it doesn't filter out those items which are unique 
which the OP asked for.


So I guess it becomes

for item in set(List):
   n = List.count(item)
   if n > 1:
   print item, n

which is still pretty clear.

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] What's the catch with ZopeDB?

2010-06-11 Thread Knacktus

Hey everyone,

I'm planning to create small application which manages product data e.g. 
parts of cars. There are quite some relations, e.g.

- a car consists of certain assemblies,
- an assembly consists of certatin parts,
- a part has serveral documents which describe the part, e.g. a CAD 
document or material data.


So, one could think of storing the data in a relational database. But 
now I start to think ... ;-):


- I would just need some predefined queries which would be performed by 
Python code, of course. Maybe using an ORM.

- Therefore, I don't think I need all the power and flexibility of SQL.
- I will work with Python objects. Why should I translate to an 
relational schema "just" for persistence?
- Performancewise, caching is probably much more sensitive than pure 
database performance. (That my guess...)


To me, ZopeDB (a object database for Python) looks like an awesomely 
easy solution. I could save some brain power for the innovative part or 
drink more beer watching the soccer world cup. At the same moment, I 
wonder why anyone in the python world would go through the hassle of 
using relational databases unless forced.


So, has anyone experience with ZopeDB? Are there some drawbacks I should 
be aware of before getting a book and dive in? (It sounds too good ;-))


Cheers,

Jan
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list

2010-06-11 Thread Hugo Arts
I really shouldn't, but I'll bite just this once.

On Fri, Jun 11, 2010 at 7:28 PM, Steven D'Aprano  wrote:
>
> Your idea of elegant and simple is not the same as mine. To me, I see
> unnecessary work and unneeded complexity. A generator expression for a
> beginner having trouble with the basics? Calling mylist.count(item)
> *twice* for every item?! How is that possibly elegant?
>

What I mean by simple is that that line of code is the most obvious
and direct translation of "a list of every item and how often it
appears, for every item that appears more than once." I call it
elegant because the code is even shorter than that English description
while remaining readable, possibly even to someone with little
programming experience.

I'm making a judgement based on readability, not performance. Code
that goes for performance in lieu of readability I'd call "clever,"
and that's not a compliment.  Yes, it's an opinion. You disagree.
That's fine.


> [pedantic]
> Not everything. Some things are inherently slow, for example:
>
> http://en.wikipedia.org/wiki/Busy_beaver
>
> A five-state Busy Beaver machine takes 47,176,870 steps to return, and a
> six-state Busy Beaver takes 10**21132 steps to return.
>
> A slightly less extreme example is Ackermann's Function:
>
> http://en.wikipedia.org/wiki/Ackermann's_function
>
> where (for example) A(4,2) has over 19,000 digits. Calculating the
> number of recursive steps needed to calculate that value is left as an
> exercise.
> [/pedantic]
>
> These are extreme examples, but the point is that there are tasks which
> are hard even for small N. "Find N needles in a haystack" remains hard
> even for N=1.
>

Now you're just arguing for the sake of being right (points for
marking it [pedantic], I suppose). The busy beaver and Ackermann
function have no bearing on this discussion whatsoever. If we're going
to argue pedantically, the 1-state busy beaver (n=1) finishes in one
step, and A(1,0) = A(0, 1) = 2. Even those are fast for small n, they
just grow ridiculously quickly.

Also, (pedantically), the problem "find needles in a haystack" grows
in the size of the haystack, not the amount of needles. So the more
appropriate n=1 version would be a haystack of size 1 with a needle in
it. Which is quite easy.

> (And before you suggest using a magnet, it's a *plastic* needle.)

That solution is not in the spirit of the problem, and since I know
that, I wouldn't bring it up.

>
> The problem is that situations that won't be encountered often *are*
> encountered, long after the coder who introduced the poorly-performing
> algorithm has moved on.
>
> Here's a real-life example, from Python itself: last September, on the
> Python-Dev mailing list, Chris Withers reported a problem downloading a
> file using Python's httplib module. For a file that took wget or
> Internet Explorer approximately 2 seconds to download, it took Python
> up to thirty MINUTES -- that's nearly a thousand times slower.
>
> Eventually Chris, with the help of others on the list, debugged the
> problem down to a Shlemiel algorithm in the httplib code. Somebody
> found a comment in the _read_chunked method that said:
>
>   # XXX This accumulates chunks by repeated string concatenation,
>   # which is not efficient as the number or size of chunks gets big.
>
> So somebody used an algorithm which they KNEW was inefficient and slow,
> it had been there for years, affecting who knows how many people, until
> eventually somebody was annoyed sufficiently to do something about it.
> And the sad thing is that avoiding repeated string concatenation is
> easy and there was no need for it in the first place.
>

Estimating the size of your input is sometimes hard. That's a valid
point. OTOH, that line of code takes maybe 10 seconds to write, and
after profiling reveals it to be slow (you *are* profiling your code,
right?) you can easily replace it with something more appropriate. Or
your successor can, since he'll have no problem figuring out what it
does.

>
> You've missed the point. It's not the language, you can write poorly
> performing code in any language, and an O(N) algorithm in a slow
> language will probably out-perform an O(N**2) or O(2**N) algorithm in a
> fast language.
>

That wasn't the point I was trying to make. My point was that speed is
not always the primary concern, and if it is, you shouldn't be writing
code in python anyway, since it is always possible to write a program
in C that performs better.

Alan's code makes a trade-off between performance and readability. I'd
call it the easiest to read solution so far. That's a trade-off that
may or may not be appropriate. My point is that you shouldn't dismiss
the code just because the algorithm sucks. You should only dismiss it
because the algorithm sucks *and* your code will have to handle large
inputs.

Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org

Re: [Tutor] What's the catch with ZopeDB?

2010-06-11 Thread Emile van Sebille

On 6/11/2010 12:42 PM Knacktus said...


So, has anyone experience with ZopeDB? Are there some drawbacks I should
be aware of before getting a book and dive in? (It sounds too good ;-))



I've been using it as part of a couple applications I wrote 10 years ago 
that use zope.  I'm not sure how my comments relate to non-zope usage or 
the recent versions so YMMV.


The two things that bother me most are that without packing the 
database, it grows -- apparently without bound.  It'll pack a 1Gb source 
DB down to 32Mb.  Also, I once experienced corruption and found it 
impossible to recover the lost data, and I've a fair amount of 
experience recovering lost data even from unmountable partitions and 
drives that don't power up.


I'm not opposed to the idea so I think I'd try it out, but those are 
issues I'd watch for.


Emile



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list

2010-06-11 Thread Dave Kuhlman
On Fri, Jun 11, 2010 at 09:57:34AM -0400, Ken G. wrote:
> 
> 
>for j in range (0, 5):
>x = a[0] # for example, 1

One picky, little point.

I've seen several solutions in this thread that included something
like the following:

for i in range(len(mylist)):
val = mylist[i]
mylist[i] = f(val)
o
o
o

That works fine, but ...

You can do this a bit more easily by using the "enumerate" built-in
function.  It provides that index.  Example:

for i, val in enumerate(mylist):
mylist[i] = f(val)
o
o
o

See http://docs.python.org/library/functions.html#enumerate

- Dave

-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the catch with ZopeDB?

2010-06-11 Thread Dave Kuhlman
On Fri, Jun 11, 2010 at 09:42:35PM +0200, Knacktus wrote:

> 
> To me, ZopeDB (a object database for Python) looks like an awesomely 
> easy solution. I could save some brain power for the innovative part or 
> drink more beer watching the soccer world cup. At the same moment, I 
> wonder why anyone in the python world would go through the hassle of 
> using relational databases unless forced.
> 
> So, has anyone experience with ZopeDB? Are there some drawbacks I should 
> be aware of before getting a book and dive in? (It sounds too good ;-))
> 

Jan -

If you are evaluating alternative solutions, you might also look
into Django models.  There have been some very positive comments
about Django on this list.  And, Django models can be used outside
of the Django Web applications.  Also, Django models are reasonably
object oriented.  A Django model/DB can sit on top of several
different relational database engines, for example, PostgreSQL, MySQL,
sqlite3, etc.

See:

http://docs.djangoproject.com/en/1.2/#the-model-layer
http://www.djangobook.com/en/2.0/chapter05/

- Dave

-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python a substitute/alternative for PhP?

2010-06-11 Thread Eldon Londe Mello Junior

Hi there,

If you care to listen to my story and fully help me out, just keep on reading 
}else{ move to the final question :)  

I'm just finishing an introductory course on PhP and MySQL (HTML, CSS and 
Javascript basics included). That's a typical first step to novice programmers 
in Brazil.

However, I've been reading a lot about programming languages and stuff in order 
to make the best choice as I don't want to spend much time learning unnecessary 
things I won't need in the future.

Thus, I decided I want to be a contributor for the GNU/LINUX community and, of 
course, become sort of an opensource-solutions professional programmer. And if 
I got it right, python would the most adequate language for me to reach my 
goals.

Only a few programmers in Brazil are familiar with python though. As I said 
before, most beginners start with PhP and stick with it or go for JAVA or MS 
proprietary languages. Actually, you can only learn python on your own around 
here as no college or private institutes offer python courses.

As you may see it coming, the big question for me is: should I stick with PHP 
as most people here (those fond of free software) or Python is or would be a 
better choice for me?

FINAL QUESTION> Is Python a substitute for PHP? I mean, can I start learning 
python by trying to do the things I've learned with PHP? Are they different 
anyhow or they actually compete against each other?

Thanks in advance, advice on which steps to take to reach my career goals would 
be very appreciated as well!

Eldon.





> Date: Fri, 11 Jun 2010 15:27:44 -0700
> From: dkuhl...@rexx.com
> To: Tutor@python.org
> Subject: Re: [Tutor] What's the catch with ZopeDB?
> 
> On Fri, Jun 11, 2010 at 09:42:35PM +0200, Knacktus wrote:
> 
> > 
> > To me, ZopeDB (a object database for Python) looks like an awesomely 
> > easy solution. I could save some brain power for the innovative part or 
> > drink more beer watching the soccer world cup. At the same moment, I 
> > wonder why anyone in the python world would go through the hassle of 
> > using relational databases unless forced.
> > 
> > So, has anyone experience with ZopeDB? Are there some drawbacks I should 
> > be aware of before getting a book and dive in? (It sounds too good ;-))
> > 
> 
> Jan -
> 
> If you are evaluating alternative solutions, you might also look
> into Django models.  There have been some very positive comments
> about Django on this list.  And, Django models can be used outside
> of the Django Web applications.  Also, Django models are reasonably
> object oriented.  A Django model/DB can sit on top of several
> different relational database engines, for example, PostgreSQL, MySQL,
> sqlite3, etc.
> 
> See:
> 
> http://docs.djangoproject.com/en/1.2/#the-model-layer
> http://www.djangobook.com/en/2.0/chapter05/
> 
> - Dave
> 
> -- 
> Dave Kuhlman
> http://www.rexx.com/~dkuhlman
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
  
_
Your E-mail and More On-the-Go. Get Windows Live Hotmail Free.
https://signup.live.com/signup.aspx?id=60969___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python a substitute/alternative for PhP?

2010-06-11 Thread Alex Hall
Personally, I would learn Python. My college does not offer Python
either, so I had to learn what I know on my own(of course, by that I
mean constantly pestering this and other of the amazing Python email
lists). PHP is fine in itself, but, after using it, Java, and intros
to a few other languages, nothing has been able to beat Python's ease
of use, massive extensibility (there is a package to let you do just
about anything you want), and support community. It is a great
language and, especially if you plan to stick with desktop
applications, I think it is much easier than a language like C++ or
Java. Your life will be even easier than mine since you are going to
be on Linux; I believe most Linux distros come with Python, while
Windows does not, so what you make can be distributed as scripts while
I have to use a program like py2exe and package the entire Python
interpreter.
Anyway, just my thoughts. Note that I am still in college for my
computer science degree and am in no way a professional programmer,
just someone who has waded in several languages and found Python to be
the only one worth diving into all the way.

On 6/11/10, Eldon Londe Mello Junior  wrote:
>
> Hi there,
>
> If you care to listen to my story and fully help me out, just keep on
> reading }else{ move to the final question :)
>
> I'm just finishing an introductory course on PhP and MySQL (HTML, CSS and
> Javascript basics included). That's a typical first step to novice
> programmers in Brazil.
>
> However, I've been reading a lot about programming languages and stuff in
> order to make the best choice as I don't want to spend much time learning
> unnecessary things I won't need in the future.
>
> Thus, I decided I want to be a contributor for the GNU/LINUX community and,
> of course, become sort of an opensource-solutions professional programmer.
> And if I got it right, python would the most adequate language for me to
> reach my goals.
>
> Only a few programmers in Brazil are familiar with python though. As I said
> before, most beginners start with PhP and stick with it or go for JAVA or MS
> proprietary languages. Actually, you can only learn python on your own
> around here as no college or private institutes offer python courses.
>
> As you may see it coming, the big question for me is: should I stick with
> PHP as most people here (those fond of free software) or Python is or would
> be a better choice for me?
>
> FINAL QUESTION> Is Python a substitute for PHP? I mean, can I start learning
> python by trying to do the things I've learned with PHP? Are they different
> anyhow or they actually compete against each other?
>
> Thanks in advance, advice on which steps to take to reach my career goals
> would be very appreciated as well!
>
> Eldon.
>
>
>
>
>
>> Date: Fri, 11 Jun 2010 15:27:44 -0700
>> From: dkuhl...@rexx.com
>> To: Tutor@python.org
>> Subject: Re: [Tutor] What's the catch with ZopeDB?
>>
>> On Fri, Jun 11, 2010 at 09:42:35PM +0200, Knacktus wrote:
>>
>> >
>> > To me, ZopeDB (a object database for Python) looks like an awesomely
>> > easy solution. I could save some brain power for the innovative part or
>> > drink more beer watching the soccer world cup. At the same moment, I
>> > wonder why anyone in the python world would go through the hassle of
>> > using relational databases unless forced.
>> >
>> > So, has anyone experience with ZopeDB? Are there some drawbacks I should
>> >
>> > be aware of before getting a book and dive in? (It sounds too good ;-))
>> >
>>
>> Jan -
>>
>> If you are evaluating alternative solutions, you might also look
>> into Django models.  There have been some very positive comments
>> about Django on this list.  And, Django models can be used outside
>> of the Django Web applications.  Also, Django models are reasonably
>> object oriented.  A Django model/DB can sit on top of several
>> different relational database engines, for example, PostgreSQL, MySQL,
>> sqlite3, etc.
>>
>> See:
>>
>> http://docs.djangoproject.com/en/1.2/#the-model-layer
>> http://www.djangobook.com/en/2.0/chapter05/
>>
>> - Dave
>>
>> --
>> Dave Kuhlman
>> http://www.rexx.com/~dkuhlman
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>   
> _
> Your E-mail and More On-the-Go. Get Windows Live Hotmail Free.
> https://signup.live.com/signup.aspx?id=60969


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for duplicates within a list

2010-06-11 Thread Steven D'Aprano
On Sat, 12 Jun 2010 05:59:45 am Hugo Arts wrote:
[...]

Putting aside all the theoretical arguments, and getting to an actual, 
real-life example:

> > So somebody used an algorithm which they KNEW was inefficient and
> > slow, it had been there for years, affecting who knows how many
> > people, until eventually somebody was annoyed sufficiently to do
> > something about it. And the sad thing is that avoiding repeated
> > string concatenation is easy and there was no need for it in the
> > first place.
>
> Estimating the size of your input is sometimes hard. That's a valid
> point. OTOH, that line of code takes maybe 10 seconds to write, and 
> after profiling reveals it to be slow (you *are* profiling your code,
> right?) you can easily replace it with something more appropriate. Or
> your successor can, since he'll have no problem figuring out what it
> does.

I beg to differ. The incident with httplib is actual, real-life evidence 
that your caviler attitude towards poor algorithms is bad advice. You 
wave your hands and say "Oh well, it doesn't matter, because somebody 
will find the slowdown and easily fix it". But in real life, it isn't 
that simple, not even in Open Source software let alone proprietary 
software where you might not have access to the source and the vendor 
might not care. (Has Microsoft yet fixed the ridiculously slow 
behaviour of the Trash Can when it has thousands of items in it?)

I was mistaken when I said Chris Withers reported this performance bug 
in September. He actually first raised it with the Python-Dev team in 
early August:

http://mail.python.org/pipermail/python-dev/2009-August/thread.html#91125
http://mail.python.org/pipermail/python-dev/2009-September/thread.html#91584

So this took AT LEAST a month elapsed time, with multiple people 
contributing, at least two different bug reports involved (and possibly 
five), and who knows how many hours debugging to solve this. Once the 
issue was identified, the solution was trivial:

http://svn.python.org/view/python/trunk/Lib/httplib.py?r1=74523&r2=74655

but it took a lot of effort to identify the issue.

As Guido van Rossum himself said, this was an embarrassment. The fix was 
entirely trivial, and there was no good reason to write the Shlemiel 
algorithm in the first place, especially since the author apparently 
knew it was a Shlemiel algorithm. Perhaps he too thought it was "more 
elegant" and that somebody else would "easily" deal with the problems 
that it caused.

The poorly-performing httplib code has been in at least three major 
releases of CPython (2.4 to 2.6) and possibly more, but apparently due 
to differences in realloc it only affected Windows users. So once 
you've run into the bug, your *second* problem is that other people 
say "It works for me, it must be your network".

It is not true that "he'll have no problem figuring out what it does". 
In code of any complexity, you can run into multiple problems and 
wild-goose-chases. This is a good example. Read the thread and see how 
many different ideas people had: they blamed the proxy, they blamed a 
pre-existing bug in sockets, they blamed buffer sizes. Nobody publicly 
blamed "third-party drivers", but I bet you somebody was thinking it.

I think you have a very rosy view of how easy it is to debug performance 
issues. Debugging is hard. Debugging performance problems is ten times 
harder.

Avoiding bad algorithms isn't premature optimization, it is thinking 
ahead. There's a fine line between them, I agree, but with the risks of 
hard-to-debug performance degradation caused by O(N**2) algorithms you 
should have a *seriously* compelling reason for using one, and not just 
because it's "good enough for small N" or "its a one-liner".

On modern desktop or laptop hardware, I would expect small N to mean 
multiple hundreds or even thousands, not a dozen or two. This isn't 
1985 when desktop computers couldn't count past 32768 and 1000 items 
was a lot. Today, large N is tens of millions. Huge is hundreds of 
millions, at which point I start worrying about paging into virtual 
memory more than the algorithm, because memory paging is an O(N**2) (or 
worse!) algorithm too.



[...]
> That wasn't the point I was trying to make. My point was that speed
> is not always the primary concern, and if it is, you shouldn't be
> writing code in python anyway, since it is always possible to write a
> program in C that performs better.

It is noteworthy the PyPy people use JIT compilation of Python code to 
beat the pants off CPython speed, and their audacious aim is to be 
*faster* than C, at least for some things. It is possible to write 
Python programs that run faster than the equivalent program written in 
optimized C.

http://news.ycombinator.com/item?id=1379382


> Alan's code makes a trade-off between performance and readability.
> I'd call it the easiest to read solution so far. 

Really? I think its readability is pretty poor, and nowhere near as 
clear as an

[Tutor] Problems with Importing into the Python Shell

2010-06-11 Thread Andrew Martin
Hey, everyone, I am new to programming and just downloaded Python 2.6 onto
my windows vista laptop. I am attempting to follow 4.11 of the tutorial
called "How to Think Like a Computer Scientist: Learning with Python v2nd
Edition documentation" (
http://openbookproject.net/thinkcs/python/english2e/ch04.html). However, I
am having some trouble. First off, I do not understand how to import things
into the python shell. I have a script I saved as chap03.py, but when I try
to import it into the python shell i get an error like this:

>>> from chap03 import *

Traceback (most recent call last):
  File "", line 1, in 
from chap03 import *
  File "C:/Python26\chap03.py", line 2
print param param
^
SyntaxError: invalid syntax
>>>

The chap03.py file is a simple one that looks like this:
def print_twice(param):
print param param

My second problem is that I need to install and import GASP in order to
follow the tutorial. When I tried to do import it, I ran into an error like
this:
>>> from gasp import *

Traceback (most recent call last):
  File "", line 1, in 
from gasp import *
  File "C:\Python26\lib\site-packages\gasp\__init__.py", line 1, in 
from api import *
  File "C:\Python26\lib\site-packages\gasp\api.py", line 1, in 
import backend
  File "C:\Python26\lib\site-packages\gasp\backend.py", line 7, in 
except ImportError: raise 'Pygame is not installed. Please install it.'
TypeError: exceptions must be old-style classes or derived from
BaseException, not str
>>>

Can anybody help me with this?

Thanks a lot

P.S. SORRY FOR THE LONG EMAIL
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor