[Tutor] List and tuple question -resend #2

2008-05-04 Thread quantrum75
Sincerely sorry for spamming everybody. Dint know about yahoo's quirks 
regarding html attachments..
Hopefully this works.

Hi everybody
I have a simple newbie kind of questions. I have tried hard to solve it, but 
couldn't figure it out.
The problem is,
I want to make a tuple of tuples or a list of lists in a iterative fashion in 
the following format. For example,

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

or

a=((1,),(2,),(3,),(4,),(5,))

I need the comma since it is going into an excel sheet.
I tried
a=[]
for i in range(5):
a.append('['+str(i)+','+']')
but it gives me,
['[0,]', '[1,]', '[2,]', '[3,]', '[4,]', '[5,]']
Which is no good since excel reads the whole thing inside the ' '
instead of the actual number.

I know the solution is probably simple and I appreciate any help.
Thanks
Rama


  

Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list and tuple question

2008-05-04 Thread Alan Gauld


quantrum75 [EMAIL PROTECTED] wrote in


 Hi everybody


Hi,

I think the key to your question is here:


 a=((1,),(2,),(3,),(4,),(5,))

 I need the comma since it is going into an excel sheet.


I think you are confused between tuples and comma separated
strings - which is what you normally use to load data into Excel

I think you probably want to look at the csv module which makes
it easier to create properly foermatted comma separated values


 a=[]
 for i in range(5):
 a.append('['+str(i)+','+']')


This appends a string that looks like a list to a list.

To create a list of lists you only need:

a.append([i])

But that doesmn't show any commas - and neither should it
since the comma is not part of the list/tuple for a single item,
its only a syntactic trick to force Python to produce a tuple.


a = (1,)
a

(1,)

print a[0]

1

So the comma is only part of the representation to show
its actually a tuple not just a number in parentheses.


 ['[0,]', '[1,]', '[2,]', '[3,]', '[4,]', '[5,]']


Which is what you told it to store.

 Which is no good since excel reads the whole thing inside the ' 
'

 instead of the actual number.


How are you feeding the data to excel?
Or to rephtrase that how is Excel 'reading' the data?

That is fairly critical to solving your core problem.

 I know th e solution is probably simple and I appreciate any 
help.


I think the solution lies in a different place to where you
are looking.

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



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


Re: [Tutor] List comprehensions

2008-04-10 Thread Alan Gauld

Dinesh B Vadhia [EMAIL PROTECTED] wrote i

 I'm using a Javascript autocomplete plugin for an online 
 web application/service.  Each time a user inputs a character, 
 the character is sent to the backend Python program which 
 searches for the character in a list of 10,000 string items.  

Eeek! 
That will be incredibly slow over any kind of network 
other than fast gigabit! try doing a ping from the client to the 
server. If it over the internet you will be lucky to get less that 
100ms, over a fast LAN it might be around 30ms. 
Add in the transmission time for your data 
- (ie 150*ave string length*10/average bandwidth) 
That network delay will swamp any optimisation of a for loop.

It is likely to come out around 10ms so your total delay 
between each character becomes 40-110ms or a maximum 
typing rate of 9-25 characters per second. The latter will feel
slightly clunky but the former will feel very sluggish. And on 
anything less than a good internet connection the rate could 
drop off to around 1 character per second! And anyone using 
a mobile GPRS connection for access would be crippled.

Alan G.

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


Re: [Tutor] List comprehensions

2008-04-10 Thread linuxian iandsd
also if you need to go for 2 results I propose you use filters 
interactive menus which will help you tailor the query to the users desires
 thus limit the query results.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comprehensions

2008-04-10 Thread linuxian iandsd
i think you are using ajax ... which undoubdetly uses an sql database since
its based on queries run from whithin the application in the browser
whithout the need for refreshing the page ... i would suggest you try
serching internet for something like  google autocomplete feature  i
guess the queries are also no that long they have a limit of the results ...
for example not more than 20 results per query.

than said there would be no loop. just a query (with a limit of 20 rersults)
each time a use inputs a character.

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


Re: [Tutor] List comprehensions

2008-04-10 Thread W W
My guess, though I'm not sure, is that google uses hashes...

why? Because they're a *ahem* load faster than loops, and the reason
is they replace the repetitive nature of a loop by using some type of
formula. Exactly /how/ this is implemented, I'm not sure.

A simple example of the speed difference:

11 * 1 =  11
The program spent 0.000810146331787 seconds.
11 * 1 =  11
The program spent 6.19888305664e-05 seconds.
(btw, that means .06... )

The difference? The first was a loop:

  1 from time import time
  2 start_time = time()
  3
  4 x = 0
  5 while x  11:
  6 x +=1
  7
  8 print 11 * 1 = , x
  9
 10 end_time = time()
 11 print The program spent, end_time - start_time, seconds.

And the second, a algebraic statement

 14 start_time = time()
 15
 16 x = 11 / 1
 17 print 11 * 1 = , x
 18
 19 end_time = time()
 20 print The program spent, end_time - start_time, seconds.

It would be simple to replace the 11 with a variable supplied by
something like variable = int(raw_input(Enter a number: ))
and you would come out with similar output.

That's basically the reason a dictionary finds dictionary[foo]
faster than a for loop: the key, foo, is transformed into some value
(As I understand it, the hashtable refers to some memory location, i.e
0x08f or some such), and there, sitting in that location, is the value
for the key foo.

so rather than comparing each value a list, it would be like having
some formula to grab that value.

I hope this wasn't too confusing, and if anyone has any corrections or
clarifications, feel free to muck about.

But yeah, a hash is probably the way you want to go (from what I know)
-Wayne

On Thu, Apr 10, 2008 at 5:03 AM, linuxian iandsd [EMAIL PROTECTED] wrote:
 also if you need to go for 2 results I propose you use filters 
 interactive menus which will help you tailor the query to the users desires
  thus limit the query results.

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





-- 
To be considered stupid and to be told so is more painful than being
called gluttonous, mendacious, violent, lascivious, lazy, cowardly:
every weakness, every vice, has found its defenders, its rhetoric, its
ennoblement and exaltation, but stupidity hasn't. - Primo Levi
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comprehensions

2008-04-10 Thread Kent Johnson
Dinesh B Vadhia wrote:
 Kent
  
 I'm using a Javascript autocomplete plugin for an online web 
 application/service.  Each time a user inputs a character, the character 
 is sent to the backend Python program which searches for the character 
 in a list of 10,000 string items.  Once it finds the character, the 
 backend will return that string and N other adjacent string items where 
 N can vary from 20 to 150.  Each string item is sent back to the JS in 
 separate print statements.  Hence, the for loop.

Ok, this sounds a little closer to a real spec. What kind of search are 
you doing? Do you really just search for individual characters or are 
you looking for the entire string entered so far as a prefix? Is the 
list of 10,000 items sorted? Can it be?

You need to look at your real problem and find an appropriate data 
structure, rather than showing us what you think is the solution and 
asking how to make it faster.

For example, if what you have a sorted list of strings and you want to 
find the first string that starts with a given prefix and return the N 
adjacent strings, you could use the bisect module to do a binary search 
rather than a linear search. Binary search of 10,000 items will take 
13-14 comparisons to find the correct location. Your linear search will 
take an average of 5,000 comparisons.

You might also want to use a trie structure though I'm not sure if that 
will let you find adjacent items.
http://www.cs.mcgill.ca/~cs251/OldCourses/1997/topic7/
http://jtauber.com/blog/2005/02/10/updated_python_trie_implementation/

 I haven't done any profiling yet as we are still building the system but 
 it seemed sensible that replacing the for loop with a built-in would 
 help.  Maybe not?

Not. An algorithm with poor big O performance should be *replaced*, 
not optimized.

Kent

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


Re: [Tutor] List comprehensions

2008-04-10 Thread Alan Gauld
Kent Johnson [EMAIL PROTECTED] wrote

 application/service.  Each time a user inputs a character, the 
 character
 is sent to the backend Python program which searches for the 
 character
 in a list of 10,000 string items.  Once it finds the character, 
 the
 backend will return that string and N other adjacent string items 
 where
 N can vary from 20 to 150.  Each string item is sent back to the JS 
 in
 separate print statements.  Hence, the for loop.

 You need to look at your real problem and find an appropriate data
 structure, rather than showing us what you think is the solution and
 asking how to make it faster.

One possibility is that the javascript fetches the list back on the
first few characters and caches it on the browser, it can then do
the search locally and only go back to the server if the user
deletes enough characters to invalidate the cache. That would
make a big difference to the overall speed by eliminating several
network lookups. I am assuming the server lookup list does not
change significantly over the duration of a form submission?

Alan G 


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


Re: [Tutor] List comprehensions

2008-04-10 Thread Kent Johnson
Alan Gauld wrote:

 One possibility is that the javascript fetches the list back on the
 first few characters and caches it on the browser

Here is an autocomplete widget I use that can do exactly that:
http://www.dyve.net/jquery/?autocomplete

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


Re: [Tutor] List comprehensions

2008-04-10 Thread bob gailer
Dinesh B Vadhia wrote:
 Kent
  
 I'm using a Javascript autocomplete plugin for an online web 
 application/service.  Each time a user inputs a character, the 
 character is sent to the backend Python program which searches for the 
 character in a list of 10,000 string items. Once it finds the 
 character, the backend will return that string and N other adjacent 
 string items where N can vary from 20 to 150.
So if I had these strings

ape, bee, cat, dog, eel, fly, gnu, hex, imp, jut, 
kit, lox

and N were 2 and the user entered g the program finds dog and sends 
back cat, dog, eel.

OK so far or not?

The user then enters y, the program finds fly and sends back eel, 
fly, gnu.

OK so far or not?

The user then enters x, the program finds no match and sends back what??

  Each string item is sent back to the JS in separate print statements. 
IIRC you don't need separate print statements. Just put \n between the 
strings and print one big string.
 Hence, the for loop.
  
 Now, N = 20 to 150 is not a lot (for a for loop) but this process is 
 performed each time the user enters a character.  Plus, there will be 
 thousands (possibly more) users at a time.  There is also the 
 searching of the 10,000 string items using the entered character.  
 All of this adds up in terms of performance.
  
 I haven't done any profiling yet as we are still building the system 
 but it seemed sensible that replacing the for loop with a built-in 
 would help.  Maybe not?
  
 Hope that helps.
  
 Dinesh
  
  
 - Original Message -
 *From:* Kent Johnson mailto:[EMAIL PROTECTED]
 *To:* Dinesh B Vadhia mailto:[EMAIL PROTECTED]
 *Cc:* tutor@python.org mailto:tutor@python.org
 *Sent:* Wednesday, April 09, 2008 1:48 PM
 *Subject:* Re: [Tutor] List comprehensions

 Dinesh B Vadhia wrote:
  Here is a for loop operating on a list of string items:
  
  data = [string 1, string 2, string 3, string 4, string 5,
  string 6, string 7, string 8, string 9, string 10, string 
 11]
  
  result = 
  for item in data:
  result = some operation on item
  print result
  
  I want to replace the for loop with another structure to improve
  performance (as the data list will contain 10,000 string items].  At
  each iteration of the for loop the result is printed (in fact, the
  result is sent from the server to a browser one result line at a time)

 Any savings you have from optimizing this loop will be completely
 swamped by the network time. Why do you think this is a bottleneck?

 You could use
 [ sys.stdout.write(some operation on item) for item in data ]

 but I consider this bad style and I seriously doubt you will see any
 difference in performance.

  The for loop will be called continuously and this is another reason to
  look for a potentially better structure preferably a built-in.

 What do you mean 'called continuously'?

 Kent
 

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


-- 
Bob Gailer
919-636-4239 Chapel Hill, NC

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


[Tutor] List comprehensions

2008-04-09 Thread Dinesh B Vadhia
Here is a for loop operating on a list of string items:

data = [string 1, string 2, string 3, string 4, string 5, string 6, 
string 7, string 8, string 9, string 10, string 11]

result = 
for item in data:
result = item + \n
print result

I want to replace the for loop with a List Comrehension (or whatever) to 
improve performance (as the data list will be 10,000].  At each stage of the 
for loop I want to print the result ie.

[print (item + \n)  for item in data]

But, this doesn't work as the inclusion of the print causes an invalid syntax 
error.

Any thoughts?

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


Re: [Tutor] List comprehensions

2008-04-09 Thread Jerry Hill
On Wed, Apr 9, 2008 at 7:12 AM, Dinesh B Vadhia
[EMAIL PROTECTED] wrote:
 I want to replace the for loop with a List Comrehension (or whatever) to
 improve performance (as the data list will be 10,000].  At each stage of
 the for loop I want to print the result ie.

List comprehensions are for building lists, not consuming them.  If
you want to do something with every element in a list, other than
building a new list with it, you should be using a for loop.

 [print (item + \n)  for item in data]

 But, this doesn't work as the inclusion of the print causes an invalid
 syntax error.

 Any thoughts?

Don't do this.

Perhaps you could share a small piece of code that you think is too
slow, and ask for advice in speeding it up?  If you're not sure which
small pieces of code are too slow, you need to profile your
application to find out.  See the documentation for python's profile
module.  If you don't have enough code written to profile, then it's
probably too early to be doing these optimizations.

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


Re: [Tutor] List comprehensions

2008-04-09 Thread linuxian iandsd
On Wed, Apr 9, 2008 at 7:44 PM, Jerry Hill [EMAIL PROTECTED] wrote:

 On Wed, Apr 9, 2008 at 7:12 AM, Dinesh B Vadhia
 [EMAIL PROTECTED] wrote:
  I want to replace the for loop with a List Comrehension (or whatever) to
  improve performance (as the data list will be 10,000].  At each stage
 of
  the for loop I want to print the result ie.

 List comprehensions are for building lists, not consuming them.  If
 you want to do something with every element in a list, other than
 building a new list with it, you should be using a for loop.

  [print (item + \n)  for item in data]
 
  But, this doesn't work as the inclusion of the print causes an invalid
  syntax error.
 
  Any thoughts?

 Don't do this.

 Perhaps you could share a small piece of code that you think is too
 slow, and ask for advice in speeding it up?  If you're not sure which
 small pieces of code are too slow, you need to profile your
 application to find out.  See the documentation for python's profile
 module.  If you don't have enough code written to profile, then it's
 probably too early to be doing these optimizations.

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



if you explain the source of the list, what do you want to change in it,
what destination will it take, i m sure the guys here will help a lot.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comprehensions

2008-04-09 Thread Dinesh B Vadhia
Sorry, let's start again.

Here is a for loop operating on a list of string items:

data = [string 1, string 2, string 3, string 4, string 5, string 6, 
string 7, string 8, string 9, string 10, string 11]

result = 
for item in data:
result = some operation on item 
print result

I want to replace the for loop with another structure to improve performance 
(as the data list will contain 10,000 string items].  At each iteration of the 
for loop the result is printed (in fact, the result is sent from the server to 
a browser one result line at a time)

The for loop will be called continuously and this is another reason to look for 
a potentially better structure preferably a built-in.

Hope this makes sense!  Thank-you.

Dinesh



- Original Message - 
From: Kent Johnson 
To: Dinesh B Vadhia 
Cc: tutor@python.org 
Sent: Wednesday, April 09, 2008 12:40 PM
Subject: Re: [Tutor] List comprehensions


Dinesh B Vadhia wrote:
 Here is a for loop operating on a list of string items:
  
 data = [string 1, string 2, string 3, string 4, string 5, 
 string 6, string 7, string 8, string 9, string 10, string 11]
  
 result = 
 for item in data:
 result = item + \n
 print result

I'm not sure what your goal is here. Do you mean to be accumulating all 
the values in data into result? Your sample code does not do that.

 I want to replace the for loop with a List Comrehension (or whatever) to 
 improve performance (as the data list will be 10,000].  At each stage 
 of the for loop I want to print the result ie.
  
 [print (item + \n)  for item in data]
  
 But, this doesn't work as the inclusion of the print causes an invalid 
 syntax error.

You can't include a statement in a list comprehension. Anyway the time 
taken to print will swamp any advantage you get from the list comp.

If you just want to print the items, a simple loop will do it:

for item in data:
   print item + '\n'

Note this will double-space the output since print already adds a newline.

If you want to create a string with all the items with following 
newlines, the classic way to do this is to build a list and then join 
it. To do it with the print included, try

result = []
for item in data:
   newItem = item + '\n'
   print newItem
   result.append(newItem)
result = ''.join(result)

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


Re: [Tutor] List comprehensions

2008-04-09 Thread linuxian iandsd
i can't think of anything but a loop here UNLESS you take the list from its
source one element at a time, process it  then print the result.

example of this would be :

 list comes in from standard input.
 list comes from a database
 list is read from a file.

so again where the list comes from is important.

if its standard input then you program will be easy  won't use any memory i
guess.

import sys   # cgi  cgitb if going web
data = sys.stdin.readline()
do what ever you like with data
print data




On Wed, Apr 9, 2008 at 8:15 PM, Dinesh B Vadhia [EMAIL PROTECTED]
wrote:

  Sorry, let's start again.

  Here is a for loop operating on a list of string items:

 data = [string 1, string 2, string 3, string 4, string 5,
 string 6, string 7, string 8, string 9, string 10, string 11]

 result = 
 for item in data:
 result = some operation on item
 print result

 I want to replace the for loop with another structure to improve
 performance (as the data list will contain 10,000 string items].  At each
 iteration of the for loop the result is printed (in fact, the result is sent
 from the server to a browser one result line at a time)

 The for loop will be called continuously and this is another reason to
 look for a potentially better structure preferably a built-in.

 Hope this makes sense!  Thank-you.

 Dinesh



 - Original Message - *From:* Kent Johnson [EMAIL PROTECTED]
 *To:* Dinesh B Vadhia [EMAIL PROTECTED]
 *Cc:* tutor@python.org
 *Sent:* Wednesday, April 09, 2008 12:40 PM
 *Subject:* Re: [Tutor] List comprehensions

 Dinesh B Vadhia wrote:
  Here is a for loop operating on a list of string items:
 
  data = [string 1, string 2, string 3, string 4, string 5,
  string 6, string 7, string 8, string 9, string 10, string
 11]
 
  result = 
  for item in data:
  result = item + \n
  print result

 I'm not sure what your goal is here. Do you mean to be accumulating all
 the values in data into result? Your sample code does not do that.

  I want to replace the for loop with a List Comrehension (or whatever) to

  improve performance (as the data list will be 10,000].  At each stage
  of the for loop I want to print the result ie.
 
  [print (item + \n)  for item in data]
 
  But, this doesn't work as the inclusion of the print causes an invalid
  syntax error.

 You can't include a statement in a list comprehension. Anyway the time
 taken to print will swamp any advantage you get from the list comp.

 If you just want to print the items, a simple loop will do it:

 for item in data:
print item + '\n'

 Note this will double-space the output since print already adds a newline.

 If you want to create a string with all the items with following
 newlines, the classic way to do this is to build a list and then join
 it. To do it with the print included, try

 result = []
 for item in data:
newItem = item + '\n'
print newItem
result.append(newItem)
 result = ''.join(result)

 Kent

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


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


Re: [Tutor] List comprehensions

2008-04-09 Thread Kent Johnson
Dinesh B Vadhia wrote:
 Here is a for loop operating on a list of string items:
  
 data = [string 1, string 2, string 3, string 4, string 5, 
 string 6, string 7, string 8, string 9, string 10, string 11]
  
 result = 
 for item in data:
 result = item + \n
 print result

I'm not sure what your goal is here. Do you mean to be accumulating all 
the values in data into result? Your sample code does not do that.

 I want to replace the for loop with a List Comrehension (or whatever) to 
 improve performance (as the data list will be 10,000].  At each stage 
 of the for loop I want to print the result ie.
  
 [print (item + \n)  for item in data]
  
 But, this doesn't work as the inclusion of the print causes an invalid 
 syntax error.

You can't include a statement in a list comprehension. Anyway the time 
taken to print will swamp any advantage you get from the list comp.

If you just want to print the items, a simple loop will do it:

for item in data:
   print item + '\n'

Note this will double-space the output since print already adds a newline.

If you want to create a string with all the items with following 
newlines, the classic way to do this is to build a list and then join 
it. To do it with the print included, try

result = []
for item in data:
   newItem = item + '\n'
   print newItem
   result.append(newItem)
result = ''.join(result)

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


Re: [Tutor] List comprehensions

2008-04-09 Thread Kent Johnson
Dinesh B Vadhia wrote:
 Here is a for loop operating on a list of string items:
  
 data = [string 1, string 2, string 3, string 4, string 5, 
 string 6, string 7, string 8, string 9, string 10, string 11]
  
 result = 
 for item in data:
 result = some operation on item
 print result
  
 I want to replace the for loop with another structure to improve 
 performance (as the data list will contain 10,000 string items].  At 
 each iteration of the for loop the result is printed (in fact, the 
 result is sent from the server to a browser one result line at a time)

Any savings you have from optimizing this loop will be completely 
swamped by the network time. Why do you think this is a bottleneck?

You could use
[ sys.stdout.write(some operation on item) for item in data ]

but I consider this bad style and I seriously doubt you will see any 
difference in performance.

 The for loop will be called continuously and this is another reason to 
 look for a potentially better structure preferably a built-in.

What do you mean 'called continuously'?

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


Re: [Tutor] List comprehensions

2008-04-09 Thread Jerry Hill
On Wed, Apr 9, 2008 at 4:15 PM, Dinesh B Vadhia
[EMAIL PROTECTED] wrote:
 Sorry, let's start again.

This version really isn't any more helpful than the first one.  I know
you corrected the sample code, but you haven't addressed any of the
fundamental questions that Kent or I asked.

 I want to replace the for loop with another structure to improve performance
 (as the data list will contain 10,000 string items].  At each iteration of
 the for loop the result is printed (in fact, the result is sent from the
 server to a browser one result line at a time)

Are you looking for a different data structure to hold your list of
strings, or are you looking for a replacement for the for loop?  How
long does it take to generate your list?  How long does each iteration
of your for loop take?  What are acceptable times for each of these?
You need to know these things before you can seriously optimize
anything.

If building up the list takes a long time and you only use it once,
consider using a generator instead of building the whole list and then
processing it.  This spreads out the time to create the list and
operates on each piece of data as soon as it's available.

I don't think you're going to find a replacement for a for loop that
is inherently faster.  You keep talking about list comprehensions, but
I don't see how a list comprehension is even appropriate for the
problem you're describing.

 The for loop will be called continuously and this is another reason to look
 for a potentially better structure preferably a built-in.

Again, it would be helpful to discuss actual bits of code, so we can
see if there are places you can gain some performance.  It's hard to
optimize psuedocode, because there are sometimes very minor changes
you can make which affect performance quite a bit. For instance,
attribute lookup in python is relatively slow.  If you can hoist any
attribute lookups out of your loop, you will get some performance
increases.

Also, you should mention what version of python you're using and what
platform it's running on.

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


Re: [Tutor] List comprehensions

2008-04-09 Thread Jerry Hill
On Wed, Apr 9, 2008 at 4:48 PM, Kent Johnson [EMAIL PROTECTED] wrote:
  You could use
  [ sys.stdout.write(some operation on item) for item in data ]

  but I consider this bad style and I seriously doubt you will see any
  difference in performance.

This really isn't a good idea.  It will take just as long as the for
loop, plus it builds a list with the return code for each call to
sys.stdout.write() call (which is probably None).  Then after building
the list with  10,000 entries of None, it throws it away.  That can't
be good for performance.

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


[Tutor] list

2008-03-21 Thread elis aeris
how do I create an empy int array of 10?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list

2008-03-21 Thread Andreas Kostyrka
Empty?

array = []

If you want to assign 10 None, that would be:

array = [None] * 10

Andreas

Am Freitag, den 21.03.2008, 17:03 -0700 schrieb elis aeris:
 how do I create an empy int array of 10?
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor


signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list

2008-03-21 Thread elis aeris
arra = [0] * 10 ?

On Fri, Mar 21, 2008 at 5:29 PM, Andreas Kostyrka [EMAIL PROTECTED]
wrote:

 Empty?

 array = []

 If you want to assign 10 None, that would be:

 array = [None] * 10

 Andreas

 Am Freitag, den 21.03.2008, 17:03 -0700 schrieb elis aeris:
  how do I create an empy int array of 10?
  ___
  Tutor maillist  -  Tutor@python.org
  http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] list

2008-03-21 Thread Steve Willoughby
elis aeris wrote:
 arra = [0] * 10 ?

If you want a list of ten zeroes, yes.

A couple of suggestions:

Find a tutorial introduction to Python such as those on python.org, or
google for dive into python, and go through the examples in there.

Also, use the interactive Python interpreter to try out things like
this.  You'll probably make much faster progress that way compared to
asking such fine-grained questions here.  This is not really a forum
for a long conversational sort of interaction or complete step-by-step
tutorials on Python.

 
 On Fri, Mar 21, 2008 at 5:29 PM, Andreas Kostyrka [EMAIL PROTECTED]
 wrote:
 
 Empty?

 array = []

 If you want to assign 10 None, that would be:

 array = [None] * 10

 Andreas

 Am Freitag, den 21.03.2008, 17:03 -0700 schrieb elis aeris:
 how do I create an empy int array of 10?
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
 
 
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor


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


[Tutor] List Box for Web

2008-02-26 Thread Dinesh B Vadhia
I know this isn't the right forum to ask but I'll try as someone might know.

For my web application, I need a list box with a search capability.  An example 
is the Python documentation (hit the F1 key under Windows from IDLE) and 
specifically the Index list ie. context-sensitive search through a list of 
phrases, but for use on a web page. 

Does anyone know if there are any open source UI widgets for such a capability?

Any help/pointers appreciated. 

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


Re: [Tutor] List Box for Web

2008-02-26 Thread Luciano Ramalho
On Wed, Feb 27, 2008 at 2:21 AM, Dinesh B Vadhia
[EMAIL PROTECTED] wrote:
 For my web application, I need a list box with a search capability.  An
 example is the Python documentation (hit the F1 key under Windows from IDLE)
 and specifically the Index list ie. context-sensitive search through a list
 of phrases, but for use on a web page.

 Does anyone know if there are any open source UI widgets for such a
 capability?

On the web, such functionality goes way beyond a mere widget, as it
requires communication between the client browser and a web server.
The server side of this is straightforward. It's the client that may
be complicated in case you want to give users immediate feedback as
they type. If that is so, you'd need to use AJAX.

Anyhow, because Python does not run in browsers, you will not find a
solution to this using only Python. I suggest you study how web apps
work and start playing with Python modules and frameworks for
server-side web programming. I'd start with the cgi module to make
sure you understand the basics before progressing to more
sophisticated and magical frameworks.

Cheers,

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


[Tutor] the tutor list has been strangely silent for a few days. Anyone know

2008-01-17 Thread bill.wu


the tutor list has been strangely silent for a few days.  Anyone know 
what has happened? why? 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] the tutor list has been strangely silent for a few days. Anyone know

2008-01-17 Thread Luke Paireepinart
bill.wu wrote:
 the tutor list has been strangely silent for a few days. Anyone know
 what has happened? why?
I got about 20 e-mails from the list yesterday.
Do you consider this slient?
or do you maybe have a problem receiving messages?
 
 /*DreamMail*/ - 第一个支持邮件来源跟踪的电子邮件客户端
 www.dreammail.org http://www.dreammail.org
 

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

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


Re: [Tutor] the tutor list has been strangely silent for a few days. Anyone know

2008-01-17 Thread Dick Moores
At 06:41 PM 1/17/2008, bill.wu wrote:

the tutor list has been strangely silent for a few days.  Anyone know
what has happened? why?

FYI I see 34 messages in my Eudora Tutor mailbox, dated 1/16 Pacific
Time (Eudora converts the datetimes to my time zone, PT).

Here's a screenshot of that mailbox:
http://www.rcblue.com/Misc/Jan16TutorMessages.png

If you're missing some, you can read them here:
http://mail.python.org/pipermail/tutor/

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


Re: [Tutor] List processing question - consolidating duplicate entries

2007-11-29 Thread Richard Querin
On Nov 27, 2007 5:40 PM, Kent Johnson [EMAIL PROTECTED] wrote:


 This is a two-liner using itertools.groupby() and operator.itemgetter:

 data = [['Bob', '07129', 'projectA', '4001',5],
 ['Bob', '07129', 'projectA', '5001',2],
 ['Bob', '07101', 'projectB', '4001',1],
 ['Bob', '07140', 'projectC', '3001',3],
 ['Bob', '07099', 'projectD', '3001',2],
 ['Bob', '07129', 'projectA', '4001',4],
 ['Bob', '07099', 'projectD', '4001',3],
 ['Bob', '07129', 'projectA', '4001',2]
 ]

 import itertools, operator
 for k, g in itertools.groupby(sorted(data), key=operator.itemgetter(0,
 1, 2, 3)):
   print k, sum(item[4] for item in g)



I'm trying to understand what's going on in the for statement but I'm having
troubles. The interpreter is telling me that itemgetter expects 1 argument
and is getting 4.

I understand that groupby takes 2 parameters the first being the sorted
list. The second is a key and this is where I'm confused. The itemgetter
function is going to return a tuple of functions (f[0],f[1],f[2],f[3]).

Should I only be calling itemgetter with whatever element (0 to 3) that I
want to group the items by?

I'm almost getting this but not quite. ;)

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


Re: [Tutor] List processing question - consolidating duplicate entries

2007-11-29 Thread Kent Johnson
Richard Querin wrote:
 import itertools, operator
 for k, g in itertools.groupby(sorted(data), key=operator.itemgetter(0,
 1, 2, 3)):
   print k, sum(item[4] for item in g)
 
 
 
 I'm trying to understand what's going on in the for statement but I'm 
 having troubles. The interpreter is telling me that itemgetter expects 1 
 argument and is getting 4.

You must be using an older version of Python, the ability to pass 
multiple arguments to itemgetter was added in 2.5. Meanwhile it's easy 
enough to define your own:
def make_key(item):
   return (item[:4])

and then specify key=make_key.

BTW when you want help with an error, please copy and paste the entire 
error message and traceback into your email.

 I understand that groupby takes 2 parameters the first being the sorted 
 list. The second is a key and this is where I'm confused. The itemgetter 
 function is going to return a tuple of functions (f[0],f[1],f[2],f[3]).

No, it returns one function that will return a tuple of values.

 Should I only be calling itemgetter with whatever element (0 to 3) that 
 I want to group the items by?

If you do that it will only group by the single item you specify. 
groupby() doesn't sort so you should also sort by the same key. But I 
don't think that is what you want.

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


Re: [Tutor] List processing question - consolidating duplicateentries

2007-11-28 Thread Tiger12506
 s=set()
 [s.add(tuple(x)) for x in myEntries]
 myEntries = [list(x) for x in list(s)]

This could be written more concisely as...

s = set(tuple(x) for x in myEntries)
myEntries = [list(x) for x in list(s)]

Generator expressions are really cool.

Not what the OP asked for exactly. He wanted to eliminate duplicates by 
adding their last columns. He said the last column is a number of hours that 
pertain to the first four columns. When you apply your method, it will not 
get rid of duplicate projects, just circumstances where the number of hours 
is equivalent also, and of course, not add the hours like they should be. I 
like the dictionary approach for this personally...

di = {}
for x in myEntries:
try: di[x[:3]]+=x[4]
except KeyError: di[x[:3]] = x[4]

This can be written even smaller and cleaner if you use the default value 
method... 

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


Re: [Tutor] List processing question - consolidating duplicate entries

2007-11-28 Thread Kent Johnson
Michael Langford wrote:
 What you want is a set of entries.

Not really; he wants to aggregate entries.

 # remove duplicate entries
 #
 #  myEntries is a list of lists,
 #such as [[1,2,3],[1,2,foo],[1,2,3]]
 #
 s=set()
 [s.add(tuple(x)) for x in myEntries]

A set can be constructed directly from a sequence so this can be written as
  s=set(tuple(x) for x in myEntries)

BTW I personally think it is bad style to use a list comprehension just 
for the side effect of iteration, IMO it is clearer to write out the 
loop when you want a loop.

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


[Tutor] List processing question - consolidating duplicate entries

2007-11-27 Thread Richard Querin
I'm trying to process a list and I'm stuck. Hopefully someone can help
me out here:

I've got a list that is formatted as follows:
[Name,job#,jobname,workcode,hours]

An example might be:

[Bob,07129,projectA,4001,5]
[Bob,07129,projectA,5001,2]
[Bob,07101,projectB,4001,1]
[Bob,07140,projectC,3001,3]
[Bob,07099,projectD,3001,2]
[Bob,07129,projectA,4001,4]
[Bob,07099,projectD,4001,3]
[Bob,07129,projectA,4001,2]

Now I'd like to consolidate entries that are duplicates. Duplicates
meaning entries that share the same Name, job#, jobname and workcode.
So for the list above, there are 3 entries for projectA which have a
workcode of 4001. (there is a fourth entry for projectA but it's
workcode is 5001 and not 4001).

So I'd like to end up with a list so that the three duplicate entries
are consolidated into one with their hours added up:

[Bob,07129,projectA,4001,11]
[Bob,07129,projectA,5001,2]
[Bob,07101,projectB,4001,1]
[Bob,07140,projectC,3001,3]
[Bob,07099,projectD,3001,2]
[Bob,07099,projectD,4001,3]

I've tried doing it with brute force by stepping through each item and
checking all the other items for matches, and then trying to build a
new list as I go, but that's still confusing me - for instance how can
I delete the items that I've already consolidated so they don't get
processed again?.

I'm not a programmer by trade so I'm sorry if this is a basic computer
science question.

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


Re: [Tutor] List processing question - consolidating duplicate entries

2007-11-27 Thread John Fouhy
On 28/11/2007, Richard Querin [EMAIL PROTECTED] wrote:
 I've got a list that is formatted as follows:
 [Name,job#,jobname,workcode,hours]
[...]
 Now I'd like to consolidate entries that are duplicates. Duplicates
 meaning entries that share the same Name, job#, jobname and workcode.
 So for the list above, there are 3 entries for projectA which have a
 workcode of 4001. (there is a fourth entry for projectA but it's
 workcode is 5001 and not 4001).

You use a dictionary: pull out the jobname and workcode as the dictionary key.


import operator

# if job is an element of the list, then jobKey(job) will be (jobname, workcode)
jobKey = operator.itemgetter(2, 3)

jobList = [...]  # the list of jobs

jobDict = {}

for job in jobList:
  try:
jobDict[jobKey(job)][4] += job[4]
  except KeyError:
jobDict[jobKey(job)] = job

(note that this will modify the jobs in your original list... if this
is Bad, you can replace the last line with ... = job[:])

HTH!

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


Re: [Tutor] List processing question - consolidating duplicate entries

2007-11-27 Thread bob gailer
Richard Querin wrote:
 I'm trying to process a list and I'm stuck. Hopefully someone can help
 me out here:

 I've got a list that is formatted as follows:
 [Name,job#,jobname,workcode,hours]

 An example might be:

 [Bob,07129,projectA,4001,5]
 [Bob,07129,projectA,5001,2]
 [Bob,07101,projectB,4001,1]
 [Bob,07140,projectC,3001,3]
 [Bob,07099,projectD,3001,2]
 [Bob,07129,projectA,4001,4]
 [Bob,07099,projectD,4001,3]
 [Bob,07129,projectA,4001,2]

 Now I'd like to consolidate entries that are duplicates. Duplicates
 meaning entries that share the same Name, job#, jobname and workcode.
 So for the list above, there are 3 entries for projectA which have a
 workcode of 4001. (there is a fourth entry for projectA but it's
 workcode is 5001 and not 4001).

 So I'd like to end up with a list so that the three duplicate entries
 are consolidated into one with their hours added up:

 [Bob,07129,projectA,4001,11]
 [Bob,07129,projectA,5001,2]
 [Bob,07101,projectB,4001,1]
 [Bob,07140,projectC,3001,3]
 [Bob,07099,projectD,3001,2]
 [Bob,07099,projectD,4001,3]
There are at least 2 more approaches.

1 - Use sqlite (or some other database), insert the data into the 
database, then run a sql statement to sum(hours) group by name, project, 
workcode.

2 - Sort the list. Create a new list with an entry for the first name, 
project, workcode. Step thru the list. Each time the name, project, 
workcode is the same, accumulate hours. When any of those change, create 
a list entry for the next name, project, workcode and again start 
accumulating hours.

The last is IMHO the most straightforward, and easiest to code.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List processing question - consolidating duplicate entries

2007-11-27 Thread Kent Johnson
bob gailer wrote:
 2 - Sort the list. Create a new list with an entry for the first name, 
 project, workcode. Step thru the list. Each time the name, project, 
 workcode is the same, accumulate hours. When any of those change, create 
 a list entry for the next name, project, workcode and again start 
 accumulating hours.

This is a two-liner using itertools.groupby() and operator.itemgetter:

data = [['Bob', '07129', 'projectA', '4001',5],
['Bob', '07129', 'projectA', '5001',2],
['Bob', '07101', 'projectB', '4001',1],
['Bob', '07140', 'projectC', '3001',3],
['Bob', '07099', 'projectD', '3001',2],
['Bob', '07129', 'projectA', '4001',4],
['Bob', '07099', 'projectD', '4001',3],
['Bob', '07129', 'projectA', '4001',2]
]

import itertools, operator
for k, g in itertools.groupby(sorted(data), key=operator.itemgetter(0, 
1, 2, 3)):
   print k, sum(item[4] for item in g)

For some explanation see my recent post:
http://mail.python.org/pipermail/tutor/2007-November/058753.html

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


Re: [Tutor] List processing question - consolidating duplicate entries

2007-11-27 Thread Michael Langford
What you want is a set of entries. Unfortunately, python lists are not
hashable which means you have to convert them to something hashable
before you can use the python set datatype.

What you'd like to do is add each to a set while converting them to a
tuple, then convert them back out of the set. In python that is:

#
# remove duplicate entries
#
#  myEntries is a list of lists,
#such as [[1,2,3],[1,2,foo],[1,2,3]]
#
s=set()
[s.add(tuple(x)) for x in myEntries]
myEntries = [list(x) for x in list(s)]

List completions are useful for all sorts of list work, this included.

Do not use a database, that would be very ugly and time consuming too.

This is cleaner than the dict keys approach, as you'd *also* have to
convert to tuples for that.

If you need this in non-list completion form, I'd be happy to write
one if that's clearer to you on what's happening.

  --Michael
-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.RowdyLabs.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comp question

2007-11-03 Thread Michael Langford
I decided you probably should also have a cleanup function since garbage
collection won't work now unless you explicitly clean the function. This
approach works, and also works if you call the function again after you've
called cleanup (it just runs the function 1 more time, then again, returns
the cached result until you cleanup).

def func_once(func):
A decorator that runs a function only once.
def decorated(*args, **kwargs):
try:
return decorated._once_result
except AttributeError:
decorated._once_result = func(*args, **kwargs)
return decorated._once_result
return decorated

def func_once_cleanup(func):
del func._once_result

# Example, will only parse the document once
@func_once
def print_and_return_big_list():
print I'm the method you want to run once
return
[23,2342,234,234,234,24,654,687,54,9684,654,864981,651,849815,65498,1651,6984651,6541,654984,651,645]

if __name__==__main__:
print_and_return_big_list()
print_and_return_big_list()
print_and_return_big_list()
print_and_return_big_list()
print_and_return_big_list()

func_once_cleanup(print_and_return_big_list)

print_and_return_big_list(



On 11/2/07, Michael Langford [EMAIL PROTECTED] wrote:

 Decorate level2 with a decorator that caches:

  http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/425445

 --Michael

 On 11/1/07, Kent Johnson [EMAIL PROTECTED] wrote:
 
  I am building a list like this:
 
   tree = []
   for top in tops:
   l2 = level2(top)
   if l2:
   tree.append((top, l2))
 
  I would really like to turn this into a list comprehension:
 
  tree = [ (top, level2(top)) for top in tops if level2(top) ]
 
  but the call to level2() is expensive enough that I don't want to repeat
  it. Is there any way to do this or am I stuck with the (IMO ugly) loop
  form?
 
  Kent
 
  who actually does have a question occasionally :-)
  ___
  Tutor maillist  -  Tutor@python.org
  http://mail.python.org/mailman/listinfo/tutor
 



 --
 Michael Langford
 Phone: 404-386-0495
 Consulting: http://www.TierOneDesign.com/




-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.TierOneDesign.com/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comp question

2007-11-03 Thread Eric Brunson
Ricardo Aráoz wrote:
 Kent Johnson wrote:
   
 I am building a list like this:

  tree = []
  for top in tops:
  l2 = level2(top)
  if l2:
  tree.append((top, l2))

 I would really like to turn this into a list comprehension:

 tree = [ (top, level2(top)) for top in tops if level2(top) ]

 but the call to level2() is expensive enough that I don't want to repeat 
 it. Is there any way to do this or am I stuck with the (IMO ugly) loop form?

 

 Just playing around here, but would this work and be better

 tree = [ pair for pair in [ (top, level2(top)) for top in tops ]
   if pair[1] ]

   

I came up with something very similar:

tops = [1,2,3,4,5]

def level2(n):
m = n ** 2
if m%2:
return m
else:
return False

tree = [ (x, y) for x, y in [ ( x, level2(x) ) for x in tops ] if y ]
print tree
[(1, 1), (3, 9), (5, 25)]


But, now I would debate conciseness vs. readability and 
maintainability.  Will someone understand what's going on if they have 
to read it?  Will you remember what it does in six months when you're 
maintaining it?

Still, an interesting academic exercise.  :-)

e.

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

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


Re: [Tutor] List comp question

2007-11-03 Thread Ricardo Aráoz
Eric Brunson wrote:
 Ricardo Aráoz wrote:
 Kent Johnson wrote:
   
 I am building a list like this:

  tree = []
  for top in tops:
  l2 = level2(top)
  if l2:
  tree.append((top, l2))

 I would really like to turn this into a list comprehension:

 tree = [ (top, level2(top)) for top in tops if level2(top) ]

 but the call to level2() is expensive enough that I don't want to repeat 
 it. Is there any way to do this or am I stuck with the (IMO ugly) loop form?

 
 Just playing around here, but would this work and be better

 tree = [ pair for pair in [ (top, level2(top)) for top in tops ]
   if pair[1] ]

   
 
 I came up with something very similar:
 
 tops = [1,2,3,4,5]
 
 def level2(n):
 m = n ** 2
 if m%2:
 return m
 else:
 return False
 
 tree = [ (x, y) for x, y in [ ( x, level2(x) ) for x in tops ] if y ]
 print tree
 [(1, 1), (3, 9), (5, 25)]
 
 
 But, now I would debate conciseness vs. readability and 
 maintainability.  Will someone understand what's going on if they have 
 to read it?  Will you remember what it does in six months when you're 
 maintaining it?
 
 Still, an interesting academic exercise.  :-)
 
 e.
 

It is perfectly understandable. Maybe the reason for doing it will not
be clear, you can always put a comment explaining you don't want to call
level2() twice.

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


[Tutor] List comp question

2007-11-02 Thread Kent Johnson
I am building a list like this:

 tree = []
 for top in tops:
 l2 = level2(top)
 if l2:
 tree.append((top, l2))

I would really like to turn this into a list comprehension:

tree = [ (top, level2(top)) for top in tops if level2(top) ]

but the call to level2() is expensive enough that I don't want to repeat 
it. Is there any way to do this or am I stuck with the (IMO ugly) loop form?

Kent

who actually does have a question occasionally :-)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comp question

2007-11-02 Thread Michael Langford
Decorate level2 with a decorator that caches:

 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/425445

--Michael

On 11/1/07, Kent Johnson [EMAIL PROTECTED] wrote:

 I am building a list like this:

  tree = []
  for top in tops:
  l2 = level2(top)
  if l2:
  tree.append((top, l2))

 I would really like to turn this into a list comprehension:

 tree = [ (top, level2(top)) for top in tops if level2(top) ]

 but the call to level2() is expensive enough that I don't want to repeat
 it. Is there any way to do this or am I stuck with the (IMO ugly) loop
 form?

 Kent

 who actually does have a question occasionally :-)
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor




-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.TierOneDesign.com/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List comp question

2007-11-02 Thread Ricardo Aráoz
Kent Johnson wrote:
 I am building a list like this:
 
  tree = []
  for top in tops:
  l2 = level2(top)
  if l2:
  tree.append((top, l2))
 
 I would really like to turn this into a list comprehension:
 
 tree = [ (top, level2(top)) for top in tops if level2(top) ]
 
 but the call to level2() is expensive enough that I don't want to repeat 
 it. Is there any way to do this or am I stuck with the (IMO ugly) loop form?
 

Just playing around here, but would this work and be better

tree = [ pair for pair in [ (top, level2(top)) for top in tops ]
  if pair[1] ]


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


Re: [Tutor] List comp question

2007-11-02 Thread Alan Gauld

Kent Johnson [EMAIL PROTECTED] wrote

 I would really like to turn this into a list comprehension:

 tree = [ (top, level2(top)) for top in tops if level2(top) ]

 but the call to level2() is expensive enough that I don't want to 
 repeat
 it. Is there any way to do this or am I stuck with the (IMO ugly) 
 loop form?

Not sure how this would work but you might be able to use
a temporary global var to hold the result if you wrap it in a
function (I suspect this is similar to the decorator suggestion,
just more explicit...

Pseudo code

tvar = None
def lev2(t):
   global tvar
   tvar = level2(t)
   return tvar

tree = [ (top, tvar) for top in tops if lev2(top) ]

Is that any better than the loop? I'm not sure it is...
unless you have to build the list in multiple places.

Do I like relying on side effects of functions? No.

HTH,

Alan G. 


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


[Tutor] list iteration question for writing to a file on disk

2007-09-14 Thread sacha rook
Hi
 
can someone help with this please?
 
i got to this point with help from the list.
 
from BeautifulSoup import BeautifulSoupdoc = ['htmlheadtitlePage 
title/title/head',   'bodyp id=firstpara align=centerThis is 
paragraph bone/b.',   'p id=secondpara align=blahThis is 
paragraph btwo/b.',   'a href=http://www.google.co.uk;/a',   
'a href=http://www.bbc.co.uk;/a',   'a 
href=http://www.amazon.co.uk;/a',   'a 
href=http://www.redhat.co.uk;/a',   '/html']soup = 
BeautifulSoup(''.join(doc))alist = soup.findAll('a')
import urlparsefor a in alist:href = a['href']print 
urlparse.urlparse(href)[1]
 
so BeautifulSoup used to find a tags; use urlparse to extract to fully 
qualified domain name use print to print a nice list of hosts 1 per line. here
www.google.co.ukwww.bbc.co.ukwww.amazon.co.ukwww.redhat.co.uk
 
nice, so i think write them out to a file; change program to this to write to 
disk and read them back to see what's been done.
 
from BeautifulSoup import BeautifulSoupdoc = ['htmlheadtitlePage 
title/title/head',   'bodyp id=firstpara align=centerThis is 
paragraph bone/b.',   'p id=secondpara align=blahThis is 
paragraph btwo/b.',   'a href=http://www.google.co.uk;/a',   
'a href=http://www.bbc.co.uk;/a',   'a 
href=http://www.amazon.co.uk;/a',   'a 
href=http://www.redhat.co.uk;/a',   '/html']soup = 
BeautifulSoup(''.join(doc))alist = soup.findAll('a')
 
import urlparseoutput = open(fqdns.txt,w)
for a in alist:href = a['href']output.write(urlparse.urlparse(href)[1])
output.close()
 
 
this writes out www.google.co.ukwww.bbc.co.ukwww.amazon.co.ukwww.redhat.co.uk
 
so I look in Alan's tutor pdf for issue and read page 120 where it suggests 
doing this; outp.write(line + '\n') # \n is a newline
 
so i change my line from this
output.write(urlparse.urlparse(href)[1])
to this
output.write(urlparse.urlparse(href)[1] + \n)
 
I look at the output file and I get this
 
www.google.co.ukwww.bbc.co.ukwww.amazon.co.ukwww.redhat.co.uk
 
hooray I think, so then I open the file in the program to read each line to do 
something with it.
i pop this after the last output.close()
 
input = open(fqdns.txt,r)for j in input:print j
input.close()
 
but his prints out 
 
www.google.co.uk
 
www.bbc.co.uk
 
www.amazon.co.uk
 
www.redhat.co.uk
 
 
Why do i get each record with an extra new line ? Am I writing out the records 
incorrectly or am I handling them incorrectly when I open the file and print do 
I have to take out newlines as I process?
 
any help would be great
 
s
 
_
Feel like a local wherever you go.
http://www.backofmyhand.com___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Question re Tutor List Etiquette

2007-08-14 Thread Dick Moores
When sending a reply to a post, to the list, should we also address 
the reply to the author of the post to which we are replying? 
(There's gotta be an easier way to say that..) If we do so, then the 
author gets a duplicate of our reply.

I've run some statistics (but no more bar graphs ;-) ). My Eudora 
mailbox for Tutor contains 12,114 emails (I've deleted the duplicates 
I've received). Of these, 9,424 are replies. Of these replies, 4,338 
(46%) were addressed ONLY to the list. So 54% WERE also sent to the 
author being replied to.

Is there a rule about this? Or should one be made? Or does it matter?

Replying only to the list takes a bit of trouble. The default 
behavior seems to be that the Reply button addresses the author 
only and not the list; Reply to all addresses both the list, the 
author, and any others included in the To: or Cc: headers of the post 
being replied to. Or at least that's how Eudora and Gmail work.

Ten years ago or so I managed a Majordomo list, and I recall that it 
was possible for the list manager to configure the list to include a 
Reply-To header. If this would be possible for the admins to do 
with Tutor -- to include a Reply-To: tutor@python.org header in the 
posts sent out by the list, it would enable us to address a reply 
only to the list by hitting the Reply button.

Dick Moores

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


Re: [Tutor] Question re Tutor List Etiquette

2007-08-14 Thread Eric Brunson
Dick Moores wrote:
 When sending a reply to a post, to the list, should we also address 
 the reply to the author of the post to which we are replying? 
 (There's gotta be an easier way to say that..) If we do so, then the 
 author gets a duplicate of our reply.

 I've run some statistics (but no more bar graphs ;-) ). My Eudora 
 mailbox for Tutor contains 12,114 emails (I've deleted the duplicates 
 I've received). Of these, 9,424 are replies. Of these replies, 4,338 
 (46%) were addressed ONLY to the list. So 54% WERE also sent to the 
 author being replied to.

 Is there a rule about this? Or should one be made? Or does it matter?

 Replying only to the list takes a bit of trouble. The default 
 behavior seems to be that the Reply button addresses the author 
 only and not the list; Reply to all addresses both the list, the 
 author, and any others included in the To: or Cc: headers of the post 
 being replied to. Or at least that's how Eudora and Gmail work.

 Ten years ago or so I managed a Majordomo list, and I recall that it 
 was possible for the list manager to configure the list to include a 
 Reply-To header. If this would be possible for the admins to do 
 with Tutor -- to include a Reply-To: tutor@python.org header in the 
 posts sent out by the list, it would enable us to address a reply 
 only to the list by hitting the Reply button.
   

If you search those 12,114 emails you'll find a discussion of this from 
about 6 weeks ago.  Consensus was split, so the list manager chose to 
leave the policy unchanged.

 Dick Moores

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

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


Re: [Tutor] Question re Tutor List Etiquette

2007-08-14 Thread Luke Paireepinart
Dick Moores wrote:
 When sending a reply to a post, to the list, should we also address 
 the reply to the author of the post to which we are replying? 
 (There's gotta be an easier way to say that..) If we do so, then the 
 author gets a duplicate of our reply.

 I've run some statistics (but no more bar graphs ;-) ). My Eudora 
 mailbox for Tutor contains 12,114 emails (I've deleted the duplicates 
 I've received). Of these, 9,424 are replies. Of these replies, 4,338 
 (46%) were addressed ONLY to the list. So 54% WERE also sent to the 
 author being replied to.

 Is there a rule about this? Or should one be made? Or does it matter?

 Replying only to the list takes a bit of trouble. The default 
 behavior seems to be that the Reply button addresses the author 
 only and not the list; Reply to all addresses both the list, the 
 author, and any others included in the To: or Cc: headers of the post 
 being replied to. Or at least that's how Eudora and Gmail work.

 Ten years ago or so I managed a Majordomo list, and I recall that it 
 was possible for the list manager to configure the list to include a 
 Reply-To header. If this would be possible for the admins to do 
 with Tutor -- to include a Reply-To: tutor@python.org header in the 
 posts sent out by the list, it would enable us to address a reply 
 only to the list by hitting the Reply button.
   
I don't get duplicates _ever_.
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question re Tutor List Etiquette

2007-08-14 Thread Kent Johnson
Dick Moores wrote:
 When sending a reply to a post, to the list, should we also address 
 the reply to the author of the post to which we are replying? 
 (There's gotta be an easier way to say that..) If we do so, then the 
 author gets a duplicate of our reply.

This is configurable for each subscriber. Go to the tutor web page at
http://mail.python.org/mailman/listinfo/tutor

Enter your subscription email at the bottom where it says, To 
unsubscribe from Tutor, get a password reminder, or change your 
subscription options enter your subscription email address:

Set Avoid duplicate copies of messages? to Yes.

 Ten years ago or so I managed a Majordomo list, and I recall that it 
 was possible for the list manager to configure the list to include a 
 Reply-To header. If this would be possible for the admins to do 
 with Tutor -- to include a Reply-To: tutor@python.org header in the 
 posts sent out by the list, it would enable us to address a reply 
 only to the list by hitting the Reply button.

Surely you have been reading the list long enough to know that this 
comes up every three months as a topic! It's not going to change. I'm 
not going to discuss it (and I hope no one else will either). Search the 
archives if you want to see previous discussions.

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


Re: [Tutor] Question re Tutor List Etiquette

2007-08-14 Thread Tom Fitzhenry
On Tue, Aug 14, 2007 at 11:06:05AM -0700, Dick Moores wrote:
 Replying only to the list takes a bit of trouble. The default 
 behavior seems to be that the Reply button addresses the author 
 only and not the list; Reply to all addresses both the list, the 
 author, and any others included in the To: or Cc: headers of the post 
 being replied to. Or at least that's how Eudora and Gmail work.
 
 Ten years ago or so I managed a Majordomo list, and I recall that it 
 was possible for the list manager to configure the list to include a 
 Reply-To header. If this would be possible for the admins to do 
 with Tutor -- to include a Reply-To: tutor@python.org header in the 
 posts sent out by the list, it would enable us to address a reply 
 only to the list by hitting the Reply button.

Mutt can be configured to recognize which emails are from a mailing list and
provides a list-reply command which only replies to the list.
http://www.mutt.org/doc/manual/manual-3.html#ss3.9

I've read Thunderbird have been planning a list-reply button, but could only
find information on a plugin (and patch) which does this at the moment:
http://alumnit.ca/wiki/index.php?page=ReplyToListThunderbirdExtension#toc3

About other mail clients, I don't know.

Procmail can be configured to detect these duplicates and
filter/delete/forward/etc. them:
http://linuxbrit.co.uk/procmail/ (7th paragraph, Now, here's a really useful
rule, ...)

I don't post that often so I don't get this that often, so it doesn't bother me
that much, but I could see how it'd be annoying to those who post frequently.

-- 
Tom Fitzhenry

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


Re: [Tutor] Question re Tutor List Etiquette

2007-08-14 Thread Dick Moores
At 11:56 AM 8/14/2007, Kent Johnson wrote:
Dick Moores wrote:
  When sending a reply to a post, to the list, should we also address
  the reply to the author of the post to which we are replying?
  (There's gotta be an easier way to say that..) If we do so, then the
  author gets a duplicate of our reply.

This is configurable for each subscriber. Go to the tutor web page at
http://mail.python.org/mailman/listinfo/tutor

Enter your subscription email at the bottom where it says, To
unsubscribe from Tutor, get a password reminder, or change your
subscription options enter your subscription email address:

Set Avoid duplicate copies of messages? to Yes.

Great!

  Ten years ago or so I managed a Majordomo list, and I recall that it
  was possible for the list manager to configure the list to include a
  Reply-To header. If this would be possible for the admins to do
  with Tutor -- to include a Reply-To: tutor@python.org header in the
  posts sent out by the list, it would enable us to address a reply
  only to the list by hitting the Reply button.

Surely you have been reading the list long enough to know that this
comes up every three months as a topic!

No, you can't assume that because I'm a long-term subscriber that I 
have always faithfully read the list. If I had, I'd be much better at 
Python than I am!

  It's not going to change. I'm
not going to discuss it (and I hope no one else will either). Search the
archives if you want to see previous discussions.

Well, I don't see that it's all that bad that I brought it up again. 
The newcomers undoubtedly will benefit from your advice, as I did. Or 
does the new welcome message mention how to avoid duplicate copies of 
messages? If not, it should.

Dick

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


Re: [Tutor] Question re Tutor List Etiquette

2007-08-14 Thread Carroll, Barry
 Date: Tue, 14 Aug 2007 12:33:16 -0700
 From: Dick Moores [EMAIL PROTECTED]
 Subject: Re: [Tutor] Question re Tutor List Etiquette
 To: tutor@python.org
 Message-ID: [EMAIL PROTECTED]
 Content-Type: text/plain; charset=us-ascii; format=flowed
 
 At 11:56 AM 8/14/2007, Kent Johnson wrote:
 Dick Moores wrote:
   When sending a reply to a post, to the list, should we also
address
   the reply to the author of the post to which we are replying?
   (There's gotta be an easier way to say that..) If we do so, then
the
   author gets a duplicate of our reply.
 
 This is configurable for each subscriber. Go to the tutor web page at
 http://mail.python.org/mailman/listinfo/tutor
 
 Enter your subscription email at the bottom where it says, To
 unsubscribe from Tutor, get a password reminder, or change your
 subscription options enter your subscription email address:
 
 Set Avoid duplicate copies of messages? to Yes.
 
 Great!
 
   Ten years ago or so I managed a Majordomo list, and I recall that
it
   was possible for the list manager to configure the list to include
a
   Reply-To header. If this would be possible for the admins to do
   with Tutor -- to include a Reply-To: tutor@python.org header in
the
   posts sent out by the list, it would enable us to address a reply
   only to the list by hitting the Reply button.
 
 Surely you have been reading the list long enough to know that this
 comes up every three months as a topic!
 
 No, you can't assume that because I'm a long-term subscriber that I
 have always faithfully read the list. If I had, I'd be much better at
 Python than I am!
 
   It's not going to change. I'm
 not going to discuss it (and I hope no one else will either). Search
the
 archives if you want to see previous discussions.
 
 Well, I don't see that it's all that bad that I brought it up again.
 The newcomers undoubtedly will benefit from your advice, as I did. Or
 does the new welcome message mention how to avoid duplicate copies of
 messages? If not, it should.
 
 Dick
 
 
 
 --
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
 
 End of Tutor Digest, Vol 42, Issue 50
 *

Greetings:

I receive these messages as a digest, not individual e-mails.  So for me
the list is the sender.  To reply to an author, I have to Fwd: and and
copy the address manually.  Since I almost never need to do that,  it
isn't a problem.  Also, it keeps my my inbox uncluttered: half a dozen
e-mails a day instead of scores.  On the other hand, the compilation
delay means that by the time I see a new question, it's nearly always
been answered three times already.  So y'all seldom get the benefit of
my superior Pythonic advice.  =8^)

FWIW,  it's nice to see this topic addressed for once without the usual
accompanying flamage.  I agree with Dick that this info should be added
to the FAQ.  Keep the frustration level down by providing the options up
front.  

Regards,
 
Barry
[EMAIL PROTECTED]
541-302-1107

We who cut mere stones must always be envisioning cathedrals.

-Quarry worker's creed




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


Re: [Tutor] Question re Tutor List Etiquette

2007-08-14 Thread William O'Higgins Witteman
On Tue, Aug 14, 2007 at 08:11:33PM +0100, Tom Fitzhenry wrote:
On Tue, Aug 14, 2007 at 11:06:05AM -0700, Dick Moores wrote:
 Replying only to the list takes a bit of trouble. The default 
 behavior seems to be that the Reply button addresses the author 
 only and not the list; Reply to all addresses both the list, the 
 author, and any others included in the To: or Cc: headers of the post 
 being replied to. Or at least that's how Eudora and Gmail work.

What I have done is to inject a Reply-To header into each email with
procmail, so that hitting reply does what I expect.  Here's the rule I
use:

:0
* ^(From|To|Cc)[EMAIL PROTECTED]
  {
:0hf
  | /usr/bin/formail -A Reply-To: tutor@python.org
:0
  python/
  }

I like this approach because it does not require that the list change
behaviour to what I consider to be the right thing (who cares what I
think), but if the list decided to change their policy then nothing
changes (the header would be changed to itself).
-- 

yours,

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


[Tutor] List within Dictionary

2007-06-20 Thread pearl jb
I wanted to know How to access the list elements which is in Dictionary  

dict = {'John':['ph=919873673','res=91928827737'] , 'William' : 
['ph=91983763','res=91837474848'] }


I want the output to be

1. John res=91928827737
2. William ph=91983763


   
-
 Once upon a time there was 1 GB storage on Yahoo! Mail. Click here for happy 
ending!___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List within Dictionary

2007-06-20 Thread Kent Johnson
pearl jb wrote:
 I wanted to know How to access the list elements which is in Dictionary 
 
 dict = {'John':['ph=919873673','res=91928827737'] , 'William' : 
 ['ph=91983763','res=91837474848'] }
 
 
 I want the output to be
 
 1. John res=91928827737
 2. William ph=91983763

You can use dict.items() to iterate over key, value pairs, then access 
the elements of the value list using regular list indexing. I'm not sure 
how you want to select res for John and ph for William so here is an 
example that prints res for both:

In [11]: d = {'John':['ph=919873673','res=91928827737'] , 'William' : 
['ph=91983763','res=91837474848'] }
In [12]: for name, phones in d.items():
: print name, phones[1]

William res=91837474848
John res=91928827737


Note that the ordering of keys is essentially random. If you want the 
list in order by name, you can sort the items:

In [13]: for name, phones in sorted(d.items()): 

 print name, phones[1]

John res=91928827737
William res=91837474848


Using enumerate() and some string formatting will give you a sequence 
number:

In [17]: for i, (name, phones) in enumerate(sorted(d.items())):
 print '%s. %s %s' % (i+1, name, phones[1])

1. John res=91928827737
2. William res=91837474848

Docs:
dict.items(): http://docs.python.org/lib/typesmapping.html#l2h-294
sorted(): http://docs.python.org/lib/built-in-funcs.html#l2h-68
enumerate(): http://docs.python.org/lib/built-in-funcs.html#l2h-24
string formatting: http://docs.python.org/lib/typesseq-strings.html

Two more notes:
- Don't use dict as the name for a dict (or list to name a list, or str 
to name a string) as it shadows the name of the built-in type.
- Please don't send HTML mail to the list - use plain text

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


[Tutor] List slicing and joining

2007-04-11 Thread Ben Sherman
I've got a list that contain a bunch of information, including the
FQDN of a host.

host_data=['foo.example.com', 'other unimportant data']

I need to seperate the hostname from the domain name.

This is how I'm doing it, and it work, but it seems *really* hacky.
Is there a better (or more pythony) way?

hostname=host_data[0].split(.)[0]
domain=..join(host_data[0].split(.)[1:])

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


Re: [Tutor] List slicing and joining

2007-04-11 Thread Andreas Kostyrka
* Ben Sherman [EMAIL PROTECTED] [070411 22:02]:
 I've got a list that contain a bunch of information, including the
 FQDN of a host.
 
 host_data=['foo.example.com', 'other unimportant data']
 
 I need to seperate the hostname from the domain name.
 
 This is how I'm doing it, and it work, but it seems *really* hacky.
 Is there a better (or more pythony) way?
 
 hostname=host_data[0].split(.)[0]
 domain=..join(host_data[0].split(.)[1:])

Well, it's basically ok, but I'd probably do something like this:
fqdn_parts = host_data[0].split(.)
host = fqdn_parts[0]
domain = ..join(fqdn_parts[1:])

Alternativly, if you are sure that you've got a '.' in the host_part,
you can do:

host, domain = host_data[0].split(., 1)

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


Re: [Tutor] List and comprehension questions

2007-02-25 Thread Kent Johnson
Smith, Jeff wrote:
 I'm getting use to using list iteration and comprehension but still have
 some questions.
 
 1. I know to replace
 for i in range(len(list1)):
 do things with list1[i]
 with
 for li in list1:
 do things with li
 but what if there are two lists that you need to access in sync.  Is
 there a simple way to replace
 for i in range(len(list1)):
 do things with list1[i] and list2[i]
 with a simple list iteration?

Use zip() to generate pairs from both (or multiple) lists:
for i1, i2 in zip(list1, list2):
   do things with i1 and i2

 
 2. I frequently replace list iterations with comprehensions
 list2 = list()
 for li in list1:
 list2.append(somefun(li))
 becomes
 list2 = [somefun(li) for li in list1]
 but is there a similar way to do this with dictionaries?
 dict2 = dict()
 for (di, dv) in dict1.iteritems():
 dict2[di] = somefun(dv)

You can construct a dictionary from a sequence of (key, value) pairs so 
this will work (using a generator expression here, add [] for Python  2.4):
dict2 = dict( (di, somefun(dv) for di, dv in dict1.iteritems() )

 
 3. Last but not least.  I understand the replacement in #2 above is the
 proper Pythonic idiom, but what if a list isn't being created.  Is it
 considered properly idiomatic to replace
 for li in list1:
 somefun(li)
 with
 [somefun(li) for li in list1]

I think this is somewhat a matter of personal preference; IMO it is 
ugly, I reserve list comps for when I actually want a list.

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


Re: [Tutor] List and comprehension questions

2007-02-25 Thread Bob Gailer
Kent Johnson wrote:
 Smith, Jeff wrote:
   
 I'm getting use to using list iteration and comprehension but still have
 some questions.

 1. I know to replace
 for i in range(len(list1)):
 do things with list1[i]
 with
 for li in list1:
 do things with li
 but what if there are two lists that you need to access in sync.  Is
 there a simple way to replace
 for i in range(len(list1)):
 do things with list1[i] and list2[i]
 with a simple list iteration?
 

 Use zip() to generate pairs from both (or multiple) lists:
 for i1, i2 in zip(list1, list2):
do things with i1 and i2

   
 2. I frequently replace list iterations with comprehensions
 list2 = list()
 for li in list1:
 list2.append(somefun(li))
 becomes
 list2 = [somefun(li) for li in list1]
 but is there a similar way to do this with dictionaries?
 dict2 = dict()
 for (di, dv) in dict1.iteritems():
 dict2[di] = somefun(dv)
 

 You can construct a dictionary from a sequence of (key, value) pairs so 
 this will work (using a generator expression here, add [] for Python  2.4):
 dict2 = dict( (di, somefun(dv) for di, dv in dict1.iteritems() )
   
Missing )?

dict((di, somefun(dv)) for di, dv in dict1.iteritems())

   
 3. Last but not least.  I understand the replacement in #2 above is the
 proper Pythonic idiom, but what if a list isn't being created.  Is it
 considered properly idiomatic to replace
 for li in list1:
 somefun(li)
 with
 [somefun(li) for li in list1]
 

 I think this is somewhat a matter of personal preference; IMO it is 
 ugly, I reserve list comps for when I actually want a list.

 Kent


-- 
Bob Gailer
510-978-4454

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


Re: [Tutor] list problem

2007-02-21 Thread Michael Lange
On Wed, 21 Feb 2007 21:21:26 +1300
John Fouhy [EMAIL PROTECTED] wrote:

  Kirk Bailey wrote:
   ok, here comes some code:
  
   f1=open(pagename,'r')
   page=f1.readlines()
   f1.close()
  
   at the end of which, the data is in page, which is a list. But
   something strange is going on here. all the data is in a single cell!
   it's a one cell list! Say what?
 
 Have you tried looking at pagename in a text editor?  If readlines()
 is returning only one line, then you should be able to spot that in
 the file.
 

Just a guess: if the behavior changed in between Python-2.3 and 2.5 ,
maybe it is an issue with line endings, LF vs. CR-LF ?

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


Re: [Tutor] list problem

2007-02-21 Thread Kent Johnson
 Kirk Bailey wrote:
 ok, here comes some code:

 f1=open(pagename,'r')
 page=f1.readlines()
 f1.close()

 at the end of which, the data is in page, which is a list. But 
 something strange is going on here. all the data is in a single cell! 
 it's a one cell list! Say what?

It sounds like for some reason the newer Python is not recognizing the 
line endings in the file. I'm not sure why that would be; are you 
running both versions on the same OS? That could cause different 
behaviour since the default line ending is different on Windows and 
Linux, for example.

Try opening the file with universal line endings:
f1 = open(pagename, 'Ur')

Kent


 Later on, when we try to itenerate the list and do things line by 
 line, it takes the entire thing at one swallow, and this creates some 
 trouble.

 Here's is a link to the entire program.
 http://www.tinylist.org/MW.txt
 this is the reader engine for a wiki to be used in a windows environment.

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


[Tutor] list problem

2007-02-20 Thread Kirk Bailey
ok, getting back to python and wikiness, I have a problem, this software 
of mine seems to exibit different behavior under the latest edition of 
python (2.5) than under the version used when I first wrote it (2.3).

It loads the page file, but returns it as a list (which is correcft) of 
one element, the entire file is in one cell. Prior, it returned each 
line as an element in the list. this is causing me some processing 
problems, and I am not a happy camper.

I am still getting out the WD-40 and loosening up rusty hinges and 
joints oin my python processing prefrontals, it's been quite a while. I 
cna post the current program to a website if it would help, or send it 
to you off list directly.

The idea is to use MiniWiki in one's windoze laptop as a wiki/notebook. 
Wikinehesa is optimied for freebsd/linux and works fine as is.

Discussion on or off list is saught. Constructive criticism will be 
graciously received and thanked.

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


Re: [Tutor] list problem

2007-02-20 Thread Luke Paireepinart
Kirk Bailey wrote:
 ok, getting back to python and wikiness, I have a problem, this software 
 of mine seems to exibit different behavior under the latest edition of 
 python (2.5) than under the version used when I first wrote it (2.3).

 It loads the page file, but returns it as a list (which is correcft) of 
 one element, the entire file is in one cell. Prior, it returned each 
 line as an element in the list. this is causing me some processing 
 problems, and I am not a happy camper.

 I am still getting out the WD-40 and loosening up rusty hinges and 
 joints oin my python processing prefrontals, it's been quite a while. I 
 cna post the current program to a website if it would help, or send it 
 to you off list directly.

 The idea is to use MiniWiki in one's windoze laptop as a wiki/notebook. 
 Wikinehesa is optimied for freebsd/linux and works fine as is.

 Discussion on or off list is saught. Constructive criticism will be 
 graciously received and thanked.

   
If you could give us a snippet of input data,
as well as the function and the 10 or so lines preceding and following 
it, that returns a single-element list,
we could probably help.  Or better yet, write a new function that's very 
simple that just displays the behavior you don't want.
It sounds like you have a lot of code, though, and since you know where 
the problem is occurring it's easier if you give us an excerpt,
the cliffs notes version, if you will, than for one of us to read 
through your code.
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list problem

2007-02-20 Thread Rikard Bosnjakovic
On 2/21/07, Kirk Bailey [EMAIL PROTECTED] wrote:

[...]
 Discussion on or off list is saught. Constructive criticism will be
 graciously received and thanked.

Without links, pointers, code or anything grippable I find it
difficult to comment or discuss anything, since i haven't got the
faintest idea or your setup, environments, applications, and the like.

How about giving it another go, feeding us with more info?

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


Re: [Tutor] list problem

2007-02-20 Thread Luke Paireepinart
Kirk:
Please reply to this message, not the other one I sent,
and please reply on-list in the future, using the 'reply-all' button 
rather than 'reply.'
Otherwise the message just goes to me instead of to everyone, which is 
the default on this list.
This copy of your e-mail is forwarded to the list, so use a 'reply-all' 
on it so everyone can see your reply.
-Luke


Original e-mail:

Kirk Bailey wrote:
 ok, here comes some code:

 f1=open(pagename,'r')
 page=f1.readlines()
 f1.close()

 at the end of which, the data is in page, which is a list. But 
 something strange is going on here. all the data is in a single cell! 
 it's a one cell list! Say what?

 Later on, when we try to itenerate the list and do things line by 
 line, it takes the entire thing at one swallow, and this creates some 
 trouble.

 Here's is a link to the entire program.
 http://www.tinylist.org/MW.txt
 this is the reader engine for a wiki to be used in a windows environment.



 Luke Paireepinart wrote:
 Kirk Bailey wrote:
 ok, getting back to python and wikiness, I have a problem, this 
 software of mine seems to exibit different behavior under the latest 
 edition of python (2.5) than under the version used when I first 
 wrote it (2.3).

 It loads the page file, but returns it as a list (which is correcft) 
 of one element, the entire file is in one cell. Prior, it returned 
 each line as an element in the list. this is causing me some 
 processing problems, and I am not a happy camper.

 I am still getting out the WD-40 and loosening up rusty hinges and 
 joints oin my python processing prefrontals, it's been quite a 
 while. I cna post the current program to a website if it would help, 
 or send it to you off list directly.

 The idea is to use MiniWiki in one's windoze laptop as a 
 wiki/notebook. Wikinehesa is optimied for freebsd/linux and works 
 fine as is.

 Discussion on or off list is saught. Constructive criticism will be 
 graciously received and thanked.

   
 If you could give us a snippet of input data,
 as well as the function and the 10 or so lines preceding and 
 following it, that returns a single-element list,
 we could probably help.  Or better yet, write a new function that's 
 very simple that just displays the behavior you don't want.
 It sounds like you have a lot of code, though, and since you know 
 where the problem is occurring it's easier if you give us an excerpt,
 the cliffs notes version, if you will, than for one of us to read 
 through your code.
 -Luke




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


Re: [Tutor] Replying to the tutor-list

2007-02-18 Thread Dave Kuhlman
On Fri, Feb 16, 2007 at 11:28:21AM +, Tim Golden wrote:
 Kent Johnson wrote:
  Tim Golden wrote:
  field and [EMAIL PROTECTED] in cc: My problem there is that I usually
  don't want to send the originating individual a private copy
  of an email he/she is going to receive from the list in any
  case, so I usually cut-and-paste around so that only the list
  is in To: AFAIK, TB doesn't offer any configurability here,
  neither a reply-to-list button, nor any option to treat a
  list specially on a general reply-to-all.

MailMan provides an administrative option to remove duplicates:

Filter out duplicate messages to list members (if possible)

I believe that it is on by default.

Am I right that, if set, this eliminates your problem?

  
  I use TB too; if you reply-all and just delete the originator from To: 
  (leaving the list as Cc:) it works fine. Though I have started leaving 
  the originator in as some people seem to prefer that.
 
 Thanks for that; (testing it out on this post). My issue is that my 
 procmail filter doesn't seem to pick up the list when it's cced.
 But that's for me to sort out it :)

Maybe a procmail rule something like this:

:0:
* ^(To|Cc):[EMAIL PROTECTED]
tutor-python-folder

Note the Cc.

Dave


-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Replying to the tutor-list

2007-02-18 Thread Tim Golden
Dave Kuhlman wrote:
 On Fri, Feb 16, 2007 at 11:28:21AM +, Tim Golden wrote:
 Kent Johnson wrote:
 Tim Golden wrote:
 field and [EMAIL PROTECTED] in cc: My problem there is that I usually
 don't want to send the originating individual a private copy
 of an email he/she is going to receive from the list in any
 case, so I usually cut-and-paste around so that only the list
 is in To: AFAIK, TB doesn't offer any configurability here,
 neither a reply-to-list button, nor any option to treat a
 list specially on a general reply-to-all.
 
 MailMan provides an administrative option to remove duplicates:
 
 Filter out duplicate messages to list members (if possible)
 
 I believe that it is on by default.
 
 Am I right that, if set, this eliminates your problem?

Thanks very much for that info. Yes, it's not that I've received
irate messages from people who've received a message twice! More
that it feels untidy, but I suspect that you're right and that
any real problem is neatly solved by Mailman.

 Thanks for that; (testing it out on this post). My issue is that my 
 procmail filter doesn't seem to pick up the list when it's cced.
 But that's for me to sort out it :)
 
 Maybe a procmail rule something like this:
 
 :0:
 * ^(To|Cc):[EMAIL PROTECTED]
 tutor-python-folder
 
 Note the Cc.

I'm actually using the List-Id header:

:0:
* ^List-Id:.*tutor.python.org
IN-lists

so I'm surprised it fell through; I would expect the
List-Id to appear in the headers anyway. If it happens
again, I'll look more closely at the headers. It hasn't
yet. Thanks for the hint, though.

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


Re: [Tutor] Replying to the tutor-list

2007-02-16 Thread Alan Gauld
Luke Paireepinart [EMAIL PROTECTED] wrote

 It's not the inconvenience but the fact that it's nonstandard, as 
 far as
 every mailing list i've been on except this.

It is interesting to see this thread because its a hot button of mine
that many new mailing lists implement this non standard behaviour
- ie send replies to the list!

But its obvious there are two views at work here.

I see the standard behaviour of Reply as - It only ever sends
to one person. Thus if I use Reply I know, with absolute certainty,
that I will never, ever, be sending private views to anyone other than
the intended recipient. Now when a mailing list subverts that it
completely blows that contract out of the water. It means I have
to be much, much, more careful about checking what I send and
to whom. When dealing with 300+ emails per day, only a small
number of which are from lists, that's a major pain.

The other view is that Reply should send the mail to whatever the
original source of the message was whether it be a list, newsgroup,
forum or whatever. (But in that view what is the purpose of
ReplyALL - why is it there?) Particularly since doing it this way
actually loses the natural ability of the mail tool to send to an
individual!

 I didn't get the e-mail from you.  You posted the e-mail to the list 
 and
 i received it because I'm a member of the list.

But you didn't really get it from the list either. The list server 
does
occasionally send emails - bounces, errors, reminders etc - but
really it forwards mails from an originator. You do get mails from
me, not the list server. The list server is no different to your 
normal
SMTP relay. It simply forwards the mails I send, it is a mechanism
to replace everyone having to maintain their own copy of a very
large distribution list. But when I send a mail to tutor my mental
model is that the tutor addrsss is just a large distribution list.

As to standard list behaviour, I don't know of any list thats been
around for more than say 10 years that uses Reply to send to All.
This seems to be a very recent thing. (And most of the lists I am
on have been around for much more than 10 years! :-)

 The list is the sender.  It aggregates posts to me.

If you subscribe to the digest this is true, but you should never
reply to the digest! The individiual mails inside the digest are
all sent from the individual posters.

 When I reply it should put my post in the same thread,
 one level below and immediately after the previous person's
 post

Sorry, we are talking about a mailing list here, not a newsgroup
or forum. Mail doesn't naturally support threading, many mail
clients don't do it at all. Others simply sort by subject/date.
Threading of email is always a bit arbitrary and error prone in
my exprience. The concept is there but the implementation
is nearly always dependant on the client (from gmane to Outlook...).

 It has retrained me to use reply-all, but I still don't like it.
 Also you end up CCing copies of your e-mails to everyone.
 http://www.unicom.com/pw/reply-to-harmful.html

That's a plus in my book because I use the digest (or gmane
newsgroup) so it keeps me in real-time sync with the threads
I'm actively involved with but leaves the others for batch mode
reading...

Regards,

Alan G.
Vive la difference! 


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


Re: [Tutor] Replying to the tutor-list

2007-02-16 Thread Tim Golden
Alan Gauld wrote:
 But its obvious there are two views at work here.

(The one which sees an apostrophe in it's and the
one which doesn't? ;)

But, joking aside, I think you've summarised the situation
quite well, and I suspect that -- given the what must be
thousands of mailing lists and newsgroups out there --
anyones claim that most x do y is probably based on
anecdotal experience, matter how widespread and long-lived.

I would take minor issue -- with you, and with the creators
of Thunderbird which is my current mail client of choice. It
looks to me as though you're suggesting that the reply-all
button is there to reply to a list, whereas it seems to me
to be there to reply to all the recipients in the original
email, *not* just to reply to a group or other bulk originator.
Now that's what Thunderbird does: puts Alan Gauld in the To:
field and [EMAIL PROTECTED] in cc: My problem there is that I usually
don't want to send the originating individual a private copy
of an email he/she is going to receive from the list in any
case, so I usually cut-and-paste around so that only the list
is in To: AFAIK, TB doesn't offer any configurability here,
neither a reply-to-list button, nor any option to treat a
list specially on a general reply-to-all.

I work with several different lists and simply work around
the differences between them so this is hardly problematic
for me, but if anyone knows of a TB configuration (or extension)
which offers reply-to-group please do let me know!

 Vive la difference! 

Indeed.

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


Re: [Tutor] Replying to the tutor-list

2007-02-16 Thread Tim Golden
Tim Golden wrote:
 Alan Gauld wrote:
 But its obvious there are two views at work here.
 
 (The one which sees an apostrophe in it's and the
 one which doesn't? ;)
 
 But, joking aside, I think you've summarised the situation
 quite well, and I suspect that -- given the what must be
 thousands of mailing lists and newsgroups out there --
 anyones claim 

Ouch! anyone's claim...

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


Re: [Tutor] Replying to the tutor-list

2007-02-16 Thread Kent Johnson
Tim Golden wrote:
 field and [EMAIL PROTECTED] in cc: My problem there is that I usually
 don't want to send the originating individual a private copy
 of an email he/she is going to receive from the list in any
 case, so I usually cut-and-paste around so that only the list
 is in To: AFAIK, TB doesn't offer any configurability here,
 neither a reply-to-list button, nor any option to treat a
 list specially on a general reply-to-all.

I use TB too; if you reply-all and just delete the originator from To: 
(leaving the list as Cc:) it works fine. Though I have started leaving 
the originator in as some people seem to prefer that.

Kent

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


Re: [Tutor] Replying to the tutor-list

2007-02-16 Thread Tim Golden
Kent Johnson wrote:
 Tim Golden wrote:
 field and [EMAIL PROTECTED] in cc: My problem there is that I usually
 don't want to send the originating individual a private copy
 of an email he/she is going to receive from the list in any
 case, so I usually cut-and-paste around so that only the list
 is in To: AFAIK, TB doesn't offer any configurability here,
 neither a reply-to-list button, nor any option to treat a
 list specially on a general reply-to-all.
 
 I use TB too; if you reply-all and just delete the originator from To: 
 (leaving the list as Cc:) it works fine. Though I have started leaving 
 the originator in as some people seem to prefer that.

Thanks for that; (testing it out on this post). My issue is that my 
procmail filter doesn't seem to pick up the list when it's cced.
But that's for me to sort out it :)

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


Re: [Tutor] Replying to the tutor-list

2007-02-16 Thread Michael P. Reilly

On 2/16/07, Alan Gauld [EMAIL PROTECTED] wrote:


As to standard list behaviour, I don't know of any list thats been
around for more than say 10 years that uses Reply to send to All.
This seems to be a very recent thing. (And most of the lists I am
on have been around for much more than 10 years! :-)

Regards,

Alan G.



If I remember correctly, LISTSERV did not return replies to the original
poster but to the mailing list you were subscribed to.  LISTSERV, c. 1984,
is the first mailing list software package and was widely used at the time.
 I believe this is still the default behavior.

This is different because the postings are coming from LISTSERV, not the
original sender.  Based on documentation, there is currently an option to
add a reply-to of the original sender; but not all clients honor reply-to
fields.

However, the standard behavior at the time was that replies went back to
the mailing list, not to the original sender.
 -Arcege
--
There's so many different worlds,
So many different suns.
And we have just one world,
But we live in different ones.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Replying to the tutor-list

2007-02-16 Thread Michael P. Reilly

On 2/16/07, ALAN GAULD [EMAIL PROTECTED] wrote:


 However, the standard behavior at the time was that
 replies went back to the mailing list, not to the original sender.

But the mailing list was the original sender so it was all wonderfully
consistent. Reply  goes to sender only, which happens
to be the list server...

Ah, the good ol' days :-)

Alan g.

Alan,


The issue is not what the mailing list does, but what the user expects and
should do.  Listserv was the first mailing list system from 23 years ago.
The users expected, as standard behavior, that replies would go to the
mailing list, not to the original sender.  You had made a claim that more
than 10 years ago (when listserv was still in use) that the standard
behavior was that mailing lists was that users would reply to the original
sender.  I'm just offering up one, very well-known example to refute that.

Myself, I'm not a person who cares how the mailing list goes.  I'll adapt.
But it does irk me when standards are applied because of misunderstandings
of applications.  For example, the usual convention is that people attach
their comments below the respondent's.  At my work, they have tried to
convince me that the standard is to put it above simply because Outlook
does that.

When making arguments, please make the arguments on a technical basis, not
on this was how it has been done in the past.  If that was the case, then
all the stuff you get in your mailbox isn't spam since spam related only
to cross-posting on newsgroups (anyone remember the Spam Wars?).  However,
the general collective has decided to expand the standard definition.

Times change, standards can evolve. Sometimes not for the better.  Make an
argument for keeping the standards how they should be technically, not
historically.

 -Arcege
--
There's so many different worlds,
So many different suns.
And we have just one world,
But we live in different ones.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Replying to the tutor-list

2007-02-16 Thread Roel Schroeven
Tim Golden schreef:
 I would take minor issue -- with you, and with the creators
 of Thunderbird which is my current mail client of choice. It
 looks to me as though you're suggesting that the reply-all
 button is there to reply to a list, whereas it seems to me
 to be there to reply to all the recipients in the original
 email, *not* just to reply to a group or other bulk originator.
 Now that's what Thunderbird does: puts Alan Gauld in the To:
 field and [EMAIL PROTECTED] in cc: My problem there is that I usually
 don't want to send the originating individual a private copy
 of an email he/she is going to receive from the list in any
 case, so I usually cut-and-paste around so that only the list
 is in To: AFAIK, TB doesn't offer any configurability here,
 neither a reply-to-list button, nor any option to treat a
 list specially on a general reply-to-all.

It's a missing feature in Thunderbird (and some other mail readers that 
call themselves 'modern'), IMO important enough to be called a bug. 
Luckily it seems to be worked on (see 
https://bugzilla.mozilla.org/show_bug.cgi?id=45715 and 
https://bugzilla.mozilla.org/show_bug.cgi?id=233417).

Meanwhile I work around the issue by subscribing to mailing lists 
indirectly via gmane. I like the interface offered by newsgroups better 
anyway for this kind of stuff.

-- 
If I have been able to see further, it was only because I stood
on the shoulders of giants.  -- Isaac Newton

Roel Schroeven

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


Re: [Tutor] Replying to the tutor-list

2007-02-16 Thread Carroll, Barry
Greetings:

I just thought I'd throw my own hat into the ring.  I'm trying out my
new, asbestos-free, flame-retardant underwear.  ;^)

 -Original Message-
 Date: Fri, 16 Feb 2007 08:14:29 -0500
 From: Michael P. Reilly [EMAIL PROTECTED]
 Subject: Re: [Tutor] Replying to the tutor-list
 To: ALAN GAULD [EMAIL PROTECTED]
 Cc: Tutor tutor@python.org
 Message-ID:
   [EMAIL PROTECTED]
 Content-Type: text/plain; charset=iso-8859-1
 
 On 2/16/07, ALAN GAULD [EMAIL PROTECTED] wrote:
 
   However, the standard behavior at the time was that
   replies went back to the mailing list, not to the original sender.
 
  But the mailing list was the original sender so it was all
wonderfully
  consistent. Reply  goes to sender only, which happens
  to be the list server...
 
  Ah, the good ol' days :-)
 
  Alan g.
 
  Alan,
 
 The issue is not what the mailing list does, but what the user expects
and
 should do.  

I agree.  However, it seems to me that the expectation in this case is
divided into two contradictory positions.  (The division seems pretty
even to me, but that's not necessarily a critical point.)

 Listserv was the first mailing list system from 23 years ago.
 The users expected, as standard behavior, that replies would go to the
 mailing list, not to the original sender.  You had made a claim that
more
 than 10 years ago (when listserv was still in use) that the standard
 behavior was that mailing lists was that users would reply to the
original
 sender.  I'm just offering up one, very well-known example to refute
that.

Again, I agree.  That is an excellent counter-example.  To me, it
demonstrates that this division of expectation existed from the
beginning of the technology.  
 
 Myself, I'm not a person who cares how the mailing list goes.  I'll
adapt.
 But it does irk me when standards are applied because of
 misunderstandings
 of applications.  For example, the usual convention is that people
attach
 their comments below the respondent's.  At my work, they have tried to
 convince me that the standard is to put it above simply because
Outlook
 does that.

Don't get me started on that.  I just got out of a minor fire fight on
another forum over that one. :^(

 When making arguments, please make the arguments on a technical basis,
not
 on this was how it has been done in the past.  

I would agree with this, too, if this were a technical issue.  But it's
not.  Read on.  

 If that was the case, then
 all the stuff you get in your mailbox isn't spam since spam related
only
 to cross-posting on newsgroups (anyone remember the Spam Wars?).
However,
 the general collective has decided to expand the standard definition.
 
 Times change, standards can evolve. Sometimes not for the better.
Make an
 argument for keeping the standards how they should be technically,
not
 historically.
 

While I agree that appeals to historical authority aren't very helpful
in cases like this, assertions of technical superiority are equally
unproductive.  Again, IMHO, this not a technical issue.  Ring vs. bus
vs. star network topology is a technical issue.  This is an issue of
convenience, which is intensely personal.  The rightness or wrongness of
either position is subjective (purely so, I believe) and technical
discussion does not clarify.  That's why discussions like this so often
turn into religious wars (as this one nearly did a few posts back).  

There is a technical issue that relates, however.  Some posters have
touched on it.  Modern mail and news software should be flexible enough
to accommodate the user's preference in this regard.  A few are,
apparently.  Most are not.  Why not?  What should be done about it?  Who
has a Python implementation that makes the default Reply to: behavior
configurable?  Which is the most flexible?  How can it be improved?
These are questions that benefit from technical discussion.  I'd like to
see more posts on these topics, and less on whose personal preference is
correct.

-Arcege
 --
 There's so many different worlds,
 So many different suns.
 And we have just one world,
 But we live in different ones.
 --
 

Regards,
 
Barry
[EMAIL PROTECTED]
541-302-1107

We who cut mere stones must always be envisioning cathedrals.

-Quarry worker's creed

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


Re: [Tutor] Replying to the tutor-list

2007-02-15 Thread Andre Engels

2007/2/14, Alan Gauld [EMAIL PROTECTED]:


Because hitting Reply and sending to a list would only be
consistent if the list was the originator of the message.
Some mailing lists do implement this bizarre and
non-standard email behaviour but thankfully the Python
community doesn't! This behaviour has been standard
in email tools for over 25 years, let's not try to change
it now!



It's getting to be the majority of mailing lists that do it the other way,
and I find it quite irritating that this list does not - I have had several
times that I sent a mail, and after sending it, sometimes long after sending
it, I realize I sent it to the sender instead of the list, so I send a
second message after it. Who knows how often I have failed to do that?

--
Andre Engels, [EMAIL PROTECTED]
ICQ: 6260644  --  Skype: a_engels
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Replying to the tutor-list

2007-02-15 Thread Rikard Bosnjakovic
On 2/15/07, Andre Engels [EMAIL PROTECTED] wrote:

 It's getting to be the majority of mailing lists that do it the other way,
 and I find it quite irritating that this list does not - I have had several
 times that I sent a mail, and after sending it, sometimes long after sending
 it, I realize I sent it to the sender instead of the list, so I send a
 second message after it. Who knows how often I have failed to do that?

I second that.

This list is the only one I've seen that does not utilize a
reply-to-tag in the postings. Lots of my replies has gone to the
poster in question and not to the list (which was the intention).


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


Re: [Tutor] Replying to the tutor-list

2007-02-15 Thread Rikard Bosnjakovic
On 2/14/07, Mike Hansen [EMAIL PROTECTED] wrote:

 The following tutor faq has an explanation:

 http://www.python.org/infogami-faq/tutor/tutor-why-do-my-replies-go-to-t
 he-person-who-sent-the-message-and-not-to-the-list/

I think the argument in that explanation sucks.

A asks something, B replies A. C replies to B's post, correcting him
on a few things and at the same time asks A some new questions.

There is no point in letting B having the sole post sent to his mailbox.

And these flamewars that the FAQ describes occur too seldom for it
to be an argument of the sake of not having a reply-to-tag.

Just my two cents.


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


Re: [Tutor] Replying to the tutor-list

2007-02-15 Thread Richard Querin

On 2/14/07, Mike Hansen [EMAIL PROTECTED] wrote:



 The following tutor faq has an explanation:

 http://www.python.org/infogami-faq/tutor/tutor-why-do-my-replies-go-to-t
 he-person-who-sent-the-message-and-not-to-the-list/




It seems like this is designed for the 5% case when it makes the other 95%
of normal reply cases more difficult. I would (like to) think that the vast
majority of replies are meant for all eyes. I would think it's the
responsibility of the person replying if he/she wants to respond privately
only rather than making that the defacto default.

Hitting reply in Gmail responds only back to the sender and not to the list.
I've been corrected (politely I might add) on more than one occasion.

Either way, it's a good list though. ;)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Replying to the tutor-list

2007-02-15 Thread Alan Gauld
Richard Querin [EMAIL PROTECTED] wrote

  The following tutor faq has an explanation:
 
  http://www.python.org/infogami-faq/tutor/tutor-why-do-my-replies-go-to-t
  he-person-who-sent-the-message-and-not-to-the-list/

 It seems like this is designed for the 5% case when it makes the 
 other 95%
 of normal reply cases more difficult.

I dunno about you but 95% of my email is private, only
about 5% comes from mailing lists.

 I would (like to) think that the vast majority of replies are
 meant for all eyes.

Thats why its called Rely *ALL*. Private is Reply, Public is Reply 
ALL.
Thats what email tools have done from day one.

 responsibility of the person replying if he/she wants to respond 
 privately
 only rather than making that the defacto default.

The default is hit ReplyAll.
Only use Reply when you specifically want to send privately...
If there is only one sender ReplyAll will only send to them (unless
you have asked it to send to you too, but thats nearly always
configurable and only useful if you don't automatically create a
Sent entry)

 Hitting reply in Gmail responds only back to the sender and not to 
 the list.

That's true of the vast majority of mail tools.
Its what the email usability standard says is supposed to be the
meaning of those buttons. Thatsd why they are named that way.

 I've been corrected (politely I might add) on more than one 
 occasion.

So just use ReplyAll all the time... unless you only want to send to
the sender. (What I have noticed is that some web based mail tools
don't seem to have ReplyAll as the default which is downright 
unfriendly
design...)

I'm really confused about why mailing lists seem to be moving in
this direction. It adds nothing and makes a very inconsistent user
experience IMHO.

(But then, I'm equally confused as to why anyone would prefer a web
forum over a news group, and that seems to happen too. Newsgroups
are far more powerful and web forums add zero so far as I can tell...)

Alan G
An internet old-timer :-)



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


Re: [Tutor] Replying to the tutor-list

2007-02-15 Thread Richard Querin

On 2/15/07, Alan Gauld [EMAIL PROTECTED] wrote:




I dunno about you but 95% of my email is private, only
about 5% comes from mailing lists.



Yeah, me too, but I guess it seems easier to just hit 'reply' 100% of the
time and have it go to the right recipient. My point really was that 95% of
the time, the recipient is everyone in the mailing list, and only 5% of the
time do I want to privately respond to a mailing list item.

I've just noticed that Gmail doesn't even show a reply-all button if there
is only one sender. If there is a cc included then it becomes available.
Either way I will just remember to hit reply-all. No big whup.

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


Re: [Tutor] Replying to the tutor-list

2007-02-15 Thread Andre Engels

2007/2/15, ALAN GAULD [EMAIL PROTECTED]:


  realize I sent it to the sender instead of the list,
 so I send a second message after it.

So do you find it odd when dealing with normal email
and you hit reply and it only goes to the sender?



No, because it is sent by the sender to me, not to some list.

Or do you not use email beyond one to one and list

membership? Surely you are introducing inconsistent
behaviour between your private mail and your list mail??



No, in my private mail I hit 'reply' and I reply. In my mailing lists I hit
'reply' and reply.

I seriously cannot fathom why anyone would want a tool

that makes things operate two differenmt ways, one for
normal email and one for mailing lists. They all come into
the same mailbox, I want them all to work the same way!



Well, what is the 'same way'? When I reply, I reply. When I get something
from a person, and reply it goes to that person. When I read something on a
newsgroup and reply, it goes to that newsgroup. When I read something on a
forum and reply, it goes to that forum.

As a matter of interest, what happens if you hit ReplyAll

on the other style lists? I assume that would work as I
would expect and send to the list and sender?
If so how do you send to the sender only?



Change the address by hand. That's hard to do, it's true, but the number of
times I want to reply in person to a message on a list is so low that that's
no problem at all.


--
Andre Engels, [EMAIL PROTECTED]
ICQ: 6260644  --  Skype: a_engels
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Replying to the tutor-list

2007-02-15 Thread Bill Campbell
The major reason for not setting Reply-To: thelist is that it makes it
*SLIGHTLY* more difficult to post something to the list and replys should
go to the sender.  IHMO, one should have to go to a little bit of effort
before posting a message that may go to thousands of recipients.

Using the ``mutt'' mailer, this effort is simply pressing ``L'' instead of
``r'' when posting to the list and adding the listname to the subscribe
section of ~/.muttrc, hardly a major inconvenience.

http://www.unicom.com/pw/reply-to-harmful.html

...
Bill
--
INTERNET:   [EMAIL PROTECTED]  Bill Campbell; Celestial Software, LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
FAX:(206) 232-9186  Mercer Island, WA 98040-0820; (206) 236-1676

``We believe...that a mugger will kill you in the half-second it takes to
draw from the holster, but won't harm you while you dial the police on your
cell phone, talk to the dispatcher and wait half an hour for officers to
arrive.'' -- Gun-Control Net-work Credo
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Replying to the tutor-list

2007-02-15 Thread Luke Paireepinart
Bill Campbell wrote:
 The major reason for not setting Reply-To: thelist is that it makes it
 *SLIGHTLY* more difficult to post something to the list and replys should
 go to the sender.  IHMO, one should have to go to a little bit of effort
 before posting a message that may go to thousands of recipients.

 Using the ``mutt'' mailer, this effort is simply pressing ``L'' instead of
 ``r'' when posting to the list and adding the listname to the subscribe
 section of ~/.muttrc, hardly a major inconvenience.
   
It's not the inconvenience but the fact that it's nonstandard, as far as 
every mailing list i've been on except this.
You don't pick people to mail specifically on the mailing list, why 
should you reply to specific people, unless you hit a special button?
I didn't get the e-mail from you.  You posted the e-mail to the list and 
i received it because I'm a member of the list.
The list is the sender.  It aggregates posts to me.  When I reply it 
should put my post in the same thread, one level below and immediately after
the previous person's post, like i expect it to.
It has retrained me to use reply-all, but I still don't like it.
Also you end up CCing copies of your e-mails to everyone.
   http://www.unicom.com/pw/reply-to-harmful.html

 ...
 Bill
 --
 INTERNET:   [EMAIL PROTECTED]  Bill Campbell; Celestial Software, LLC
 URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
 FAX:(206) 232-9186  Mercer Island, WA 98040-0820; (206) 236-1676

 ``We believe...that a mugger will kill you in the half-second it takes to
 draw from the holster, but won't harm you while you dial the police on your
 cell phone, talk to the dispatcher and wait half an hour for officers to
 arrive.'' -- Gun-Control Net-work Credo
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor

   

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


Re: [Tutor] Replying to the tutor-list

2007-02-15 Thread Bill Campbell
On Thu, Feb 15, 2007, Luke Paireepinart wrote:
Bill Campbell wrote:
 The major reason for not setting Reply-To: thelist is that it makes it
 *SLIGHTLY* more difficult to post something to the list and replys should
 go to the sender.  IHMO, one should have to go to a little bit of effort
 before posting a message that may go to thousands of recipients.

 Using the ``mutt'' mailer, this effort is simply pressing ``L'' instead of
 ``r'' when posting to the list and adding the listname to the subscribe
 section of ~/.muttrc, hardly a major inconvenience.
   
It's not the inconvenience but the fact that it's nonstandard, as far as 
every mailing list i've been on except this.

Hardly non-standrd.  The option for this in the Mailman MLM is:

 Where are replies to list messages directed? Poster is strongly
 recommended for most mailing lists.

I've been maintaining various technical mailing lists for over twenty years
now, and see this same thread come up many times.

Having the Reply-To: to the original poster minimizes the probability of
somebody sending mail to a list that was intended for the original poster
(which may be private).  The only advantage of having it set to the list is
it makes it easier for lazy people to send nonsense to hundreds of people.

As I said in my original message, it should require a little bit of effort
to send messages to hundreds or thousands of recipients.

Bill
--
INTERNET:   [EMAIL PROTECTED]  Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
FAX:(206) 232-9186  Mercer Island, WA 98040-0820; (206) 236-1676

It is necessary for the welfare of society that genius should be
privileged to utter sedition, to blaspheme, to outrage good taste, to
corrupt the youthful mind, and generally to scandalize one's uncles.
-- George Bernard Shaw
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Replying to the tutor-list

2007-02-15 Thread Terry Carroll
On Thu, 15 Feb 2007, Bill Campbell wrote:

 Having the Reply-To: to the original poster minimizes the probability of
 somebody sending mail to a list that was intended for the original poster
 (which may be private).  

Well, no.  It minimizes the probability of someone sending mail to a list.  

It equally minimizes that probability, regardless of whether the mail was 
intended to go to the list or privately to the original poster.

Most replies to this list are intended to go to the list.  At least a 
couple times a week we see messages from one of the more helpful tutors 
saying please reply to the list, not just to me.  Just yesterday, I 
received an email in reply to one of my messages that was intended to 
assist the person I had replied to.  The intended recepient never got the 
email, because the sender, no doubt relying on the default, did not reply 
to the list.

Having reply-to go to the list is having it go to the most commonly 
preferred recipient.

So, minimizing the probability that the mail will go to the list, when 
most mail is intended to go to the list, is, I think, a Bad Thing.  Not 
that Bad a Thing, in the grand scheme of things, but a Bad Thing 
nonetheless.

 The only advantage of having it set to the list is it makes it easier
 for lazy people to send nonsense to hundreds of people.

That's way out of line.  The advantage of having it go to the list is to
make the default coincide with the usual intent; and that's what defaults
are for.

This is true regardless of whether the replying party is lazy or not;  
and regardless of whether the replying post is nonsense or not.

As I said, I have no particular dog in this fight.  The list uses a 
reply-to mechanism that I don't think makes sense, so I fixed it for 
myself with procmail.  But it's pretty arrogant to think that the divide 
of opinion on here is not a reasonable one, and that anyone who doesn't 
agree with your position must be lazy or writing nonsense.

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


[Tutor] Replying to the tutor-list

2007-02-14 Thread Rikard Bosnjakovic
All texts that I reply to this list are automatically sent to the
author, or - by selecting Reply all in my mail client - the
tutorlist gets a CC.

Why is there no reply-to-tag in all the posts, making the list
recipient at all times?


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


Re: [Tutor] Replying to the tutor-list

2007-02-14 Thread Mike Hansen
 

 -Original Message-
 From: [EMAIL PROTECTED] 
 [mailto:[EMAIL PROTECTED] On Behalf Of Rikard Bosnjakovic
 Sent: Wednesday, February 14, 2007 9:21 AM
 To: tutor@python.org
 Subject: [Tutor] Replying to the tutor-list
 
 All texts that I reply to this list are automatically sent to the
 author, or - by selecting Reply all in my mail client - the
 tutorlist gets a CC.
 
 Why is there no reply-to-tag in all the posts, making the list
 recipient at all times?
 
 
 -- 
 - Rikard.
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 

The following tutor faq has an explanation:

http://www.python.org/infogami-faq/tutor/tutor-why-do-my-replies-go-to-t
he-person-who-sent-the-message-and-not-to-the-list/

Or 

http://tinyurl.com/uns5q

Mike
-

  NOTICE:  This e-mail transmission and any documents or files attached to
  it contain information for the sole use of the above-identified individual or 
entity.

  Its contents may be privileged, confidential, and exempt from disclosure 
under the law.
  Any dissemination, distribution, or copying of this communication is strictly 
prohibited.

  Please notify the sender immediately if you are not the intended recipient.

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


Re: [Tutor] Replying to the tutor-list

2007-02-14 Thread Dave Kuhlman
On Wed, Feb 14, 2007 at 05:20:44PM +0100, Rikard Bosnjakovic wrote:

 Why is there no reply-to-tag in all the posts, making the list
 recipient at all times?

Believe it or not -- The email reader that I use (mutt on a FreeBSD
machine that I telnet/ssh into) has a reply-to-list operation. 
That's what I've used to post this message.  So, you can tell me if
it is doing the right thing.

You might look for a similar feature in your email reader.

In the mutt email reader, I've actually had to do a little
configuration to tell mutt which lists I want to use this feature
on.

I just checked.  It looks like when I use the reply-to-list
operation in mutt, the only recipient is [EMAIL PROTECTED]

Dave


-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Replying to the tutor-list

2007-02-14 Thread J or M Montgomery
On Wed, 14 Feb 2007 09:08:38 -0800
Dave Kuhlman [EMAIL PROTECTED] wrote:

 On Wed, Feb 14, 2007 at 05:20:44PM +0100, Rikard Bosnjakovic wrote:
 
  Why is there no reply-to-tag in all the posts, making the list
  recipient at all times?

I use Sylpheed 2.2.7 in a Linux box. There are 'Reply' and 'Reply all' options. 
when I send a message using 'Reply it goes only to the Tutor List. Using the 
'Reply all choice it goes to the prior poster with a copy to the list.

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


Re: [Tutor] Replying to the tutor-list

2007-02-14 Thread Alan Gauld
Rikard Bosnjakovic [EMAIL PROTECTED] wrote 

 All texts that I reply to this list are automatically sent to the
 author, or - by selecting Reply all in my mail client - the
 tutorlist gets a CC.

Yep, that makes sense. It's how mail tools work in a sane world.
You Reply and it goes to the sender.
You ReplyAll and it goes to everyone who received the message, 
including the list.

Normal email behaviour.

 Why is there no reply-to-tag in all the posts, making the list
 recipient at all times?

Because hitting Reply and sending to a list would only be 
consistent if the list was the originator of the message.
Some mailing lists do implement this bizarre and 
non-standard email behaviour but thankfully the Python 
community doesn't! This behaviour has been standard 
in email tools for over 25 years, let's not try to change 
it now!

IMHO of course :-)

Alan G.

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


Re: [Tutor] Replying to the tutor-list

2007-02-14 Thread Terry Carroll
On Wed, 14 Feb 2007, Rikard Bosnjakovic wrote:

 All texts that I reply to this list are automatically sent to the
 author, or - by selecting Reply all in my mail client - the
 tutorlist gets a CC.
 
 Why is there no reply-to-tag in all the posts, making the list
 recipient at all times?

I also prefer that, but it's a matter of taste, and this lists tastes 
don't run that way.

I use procmail to add a Reply-To: header, and then dump the email into my 
separate Python in-box:

# Python tutor list
:0:
* [EMAIL PROTECTED]
{
:0hf
| $FORMAIL -A Reply-To: tutor@python.org

:0:
inbox.python

}


(I've got a FORMAIL=/usr/bin/formail earlier in my .procmailrc)

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


Re: [Tutor] List to dictionary

2006-12-07 Thread Luke Paireepinart
Dick Moores wrote:
 At 09:53 PM 12/6/2006, Luke Paireepinart wrote:
 # Remove duplicates from a list:
  L = [1,2,2,3,3,3]
  [x for x in L if x not in locals()['_[1]'].__self__]
 [1,2,3]

 Why not
  L = [1,2,2,3,3,3]
  list(set(L))
 [1, 2, 3]
Because the other methods (via set or dict keys) don't preserve order, 
and the list comprehension version does.
The others may have preserved order in this case, but won't always.
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List to dictionary

2006-12-07 Thread Kent Johnson
Luke Paireepinart wrote:
 How about this :D
 
 # Remove duplicates from a list:
 L = [1,2,2,3,3,3]
 [x for x in L if x not in locals()['_[1]'].__self__]
 [1,2,3]
 
 [accessed at 
 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204297 ]

The problems with this are, it is not portable, even across versions of 
CPython (try the above in Python 2.4) and it will have poor performance 
for lists with many unique entries because it searches the entire list 
for each addition.

These recipes are interesting, particularly the discussion for the first 
one:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/438599

Raymond Hettinger (who should know, he is a Python performance guru) 
said in 2002 that this is the fastest order-preserving method:
def uniq(alist)# Fastest order preserving
 set = {}
 return [set.setdefault(e,e) for e in alist if e not in set]

Perhaps there is now a faster method using a real set, but maybe not 
because set doesn't have a method that adds to the set and returns the 
value.

Kent

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


[Tutor] List to dictionary question

2006-12-06 Thread Morpheus
I'm new to programming, and trying to learn the Python language.  
The following code does what I want it to do, but I have not idea how it
works.  

def scanList(names,temp):
for i in names:
temp[i] = 0
print temp

Names = []
temp = {}

I have a list of names (Names[]) and want to remove duplicate names in
the list.  Here is what I think is happening (please correct me if I'm
wrong, or using the wrong technical terminology):  I'm passing the
variables Names and temp as arguments to the scanList function.  The
statement (for i in names:) is an iteration going through each item in
the list.  The next statement (temp[i] = 0) is where I get confused.
Can someone please explain what is happening here.  

Thanks for your help.


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


Re: [Tutor] List to dictionary question

2006-12-06 Thread Mike Hansen
 

 -Original Message-
 From: [EMAIL PROTECTED] 
 [mailto:[EMAIL PROTECTED] On Behalf Of Morpheus
 Sent: Wednesday, December 06, 2006 9:00 AM
 To: tutor@python.org
 Subject: [Tutor] List to dictionary question
 
 I'm new to programming, and trying to learn the Python language.  
 The following code does what I want it to do, but I have not 
 idea how it
 works.  
 
 def scanList(names,temp):
 for i in names:
 temp[i] = 0
 print temp
 
 Names = []
 temp = {}
 
 I have a list of names (Names[]) and want to remove duplicate names in
 the list.  Here is what I think is happening (please correct me if I'm
 wrong, or using the wrong technical terminology):  I'm passing the
 variables Names and temp as arguments to the scanList function.  The
 statement (for i in names:) is an iteration going through each item in
 the list.  The next statement (temp[i] = 0) is where I get confused.
 Can someone please explain what is happening here.  
 
 Thanks for your help.
 

temp is a dictionary. Dictionaries have unique keys. scanList goes
through each item in names and sets the key of the temp dictionary to
the item. If there are more than one item of the same value, it just
sets the key of the dictionary again. The statement temp[i] = 0 is
setting the value of the key 'i' to 0. (temp[key] = value) To get your
list without duplicates just get the keys of the dictionary after the
list has been run through function.
temp.keys()

If you have names = [1,2,3,2,4]
The temp dictionaries keys become 1,2,3,4.
In the scanList function above
temp[1] = 0
temp[2] = 0
temp[3] = 0
temp[2] = 0 - It's just setting the same key to zero again.
temp[4] = 0

temp.keys() 
[1,2,3,4]

I hope that makes some sense. 

I think a more explicit way of removing duplicates from a list is using
sets.

In [1]: x = ['a', 'a', 'b', 'c', 'd', 'e', 'f', 'f', 'f']

In [2]: se = set(x)

In [3]: se
Out[3]: set(['a', 'c', 'b', 'e', 'd', 'f'])

In [4]: sel = list(se)

In [5]: sel
Out[5]: ['a', 'c', 'b', 'e', 'd', 'f']
-

  NOTICE:  This e-mail transmission and any documents or files attached to
  it contain information for the sole use of the above-identified individual or 
entity.

  Its contents may be privileged, confidential, and exempt from disclosure 
under the law.
  Any dissemination, distribution, or copying of this communication is strictly 
prohibited.

  Please notify the sender immediately if you are not the intended recipient.

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


[Tutor] List to dictionary

2006-12-06 Thread Morpheus
I'm new to programming, and trying to learn the Python language.  
The following code does what I want it to do, but I have not idea how it
works.  

def scanList(names,temp):
for i in names:
temp[i] = 0
print temp

Names = []
temp = {}

I have a list of names (Names[]) and want to remove duplicate names in
the list.  Here is what I think is happening (please correct me if I'm
wrong, or using the wrong technical terminology):  I'm passing the
variables Names and temp as arguments to the scanList function.  The
statement (for i in names:) is an iteration going through each item in
the list.  The next statement (temp[i] = 0) is where I get confused.
Can someone please explain what is happening here.  

Thanks for your help.


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


Re: [Tutor] List to dictionary

2006-12-06 Thread Dick Moores
At 09:53 PM 12/6/2006, Luke Paireepinart wrote:

  I have a list of names (Names[]) and want to remove duplicate names in
  the list.  [snip]
 
 
  The way I usually do this is something like:
 
  outDict = dict(map(lambda x: (x, 1), inList))
  names = outDict.keys()
  names.sort()
 
How about this :D

# Remove duplicates from a list:
  L = [1,2,2,3,3,3]
  [x for x in L if x not in locals()['_[1]'].__self__]
[1,2,3]

Why not
  L = [1,2,2,3,3,3]
  list(set(L))
[1, 2, 3]
?

That's a real question.

Dick Moores


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


Re: [Tutor] List manipulation

2006-09-16 Thread Srinivas Iyyer
Dear Kent and Bob,
thank you for your solutions. It helped, however,
based on your suggestions, I intended to solve a
chromosome walking problem. I posted my question on
subject name:
'Limitation of range() function in Walking problem'. 

Thanks again. 
Sri

--- Kent Johnson [EMAIL PROTECTED] wrote:

 Srinivas Iyyer wrote:
  Thank you Bob for your email.
  Sorry for the confusion. 
  here is what I ment:
  
  test = ['10\t15', '16\t20', '25\t35', '45\t50',
  '55\t60', '61\t65', '75\t80']
 
  I would get:
  10  20
  25  35
  45  50
  55  65
  75  80
 
 Here is my take on it:
 
 test = ['10\t15', '16\t20', '25\t35',
 '45\t50','55\t60', '61\t65', '75\t80']
 
 pairs = [ map(int, x.split('\t')) for x in test ]
 
 i = iter(pairs)
 
 last = i.next()
 
 for current in i:
  if current[0] == last[1]+1:
  last = [last[0], current[1]]
  else:
  print last
  last = current
 
 print last
 
 
 You can also wrap this in a generator which yields
 the desired pairs, so 
 they can be printed or put in a list or whatever:
 
 def compress(pairs):
  i = iter(pairs)
 
  last = i.next()
 
  for current in i:
  if current[0] == last[1]+1:
  last = [last[0], current[1]]
  else:
  yield last
  last = current
 
  yield last
 
 
 for pair in compress(pairs):
  print pair
 
 
 Kent
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


<    1   2   3   4   5   6   7   >