Re: [Tutor] IndexError and appending to lists [Was: Re: Need Help on Assignment]

2005-08-23 Thread Danny Yoo


> input = open('/home/tom/Python/Input/SPY3.txt', 'r')
> N=0
> s = 'boo'
> date =[]
> T = [0,0,0,0,0,0]   #don't know why this is necessary
  ^

Ok, let's talk about that comment there.  Why six zeros?

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


Re: [Tutor] Tk canvas question

2005-08-23 Thread 铁石

>You can use the itemcget() method of the canvas to retrieve the text:
>
> >>> root = Tk()
> >>> canvas=Canvas(root)
> >>> t=canvas.create_text(10, 10, text='Hello', tag='1')
> >>> canvas.pack()
> >>> canvas.itemcget('1', 'text')
>'Hello'
>
>There does seem to be something strange with reusing the tags - if I create a 
>new element with tag '1' I can't retrieve it by tag:
>
> >>> canvas.delete('1')
> >>> t=canvas.create_text(10, 10, text='Goodbye', tag='1')
> >>> canvas.itemcget('1', 'text')
>''
> >>> canvas.find_withtag('1')
>()
>
>even though if I ask what tags are on the item it says '1':
>
> >>> t
>2
> >>> canvas.itemcget(2, 'text')
>'Goodbye'
> >>> canvas.itemcget(2, 'tags')
>'1'
> >>> canvas.find_withtag('1')
>

  Thank's for your help,kent! the itemcget method is exactly what I want!
In the first email,I mix the object id and tag,the increasing 
number I talk about is the object id.In the old source,I coded like :
tagnum=self.find_closest(event.x,event.y)
and then use this tagnum as a tag to find the file name in my dictionary.
I had hope that this id would be count form 1 again ,but it won't as
I had found out. A silly mistake I had make!
  now, as your help,I bind the tag and object id in my dictionary:
icon=self.create_image(10, i*self.dist_y, image=self.fileIcon, 
anchor=NW,tag=str(i))

txt=self.create_text(30,i*self.dist_y,text=elem,anchor=NW,tag=str(i))
self.bindings[i]=txt
and then use itemcget to get the filename.
k=self.itemcget (CURRENT, "tag")
l=int(k.split()[0])
elem=self.itemcget(self.bindings[l],"text")
 I had try find_withtag(l) here and get only one number at the first directory
  (num,)
  if i change to other directory and del tags:
   self.dtag(str(i)) and tag the new items with the same tag
   () is return. 
This is strange!
no matter what, my problem had served thank's alot!

winglion
2005-08-24



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


Re: [Tutor] IndexError and appending to lists [Was: Re: Need Help on Assignment]

2005-08-23 Thread Tom Strickland
Danny,

Here's the working program.

Tom

*

#!/usr/bin/python2.4
input = open('/home/tom/Python/Input/SPY3.txt', 'r')
N=0
s = 'boo'
date =[]
T = [0,0,0,0,0,0]   #don't know why this is necessary
open = []
close = []
hi = []
lo = []
vol = []
while s:
s = input.readline()
if s == '':break
s = s[:-2]
T[N] = s.split(',')
date.append(T[N][0])
open.append(float(T[N][1]))
hi.append(float(T[N][2]))
lo.append(float(T[N][3]))
close.append(float(T[N][4]))
vol.append(float(T[N][5]))
N+=1
print N
for i in range(N):
T[i]
print T[i]
print date[i], open[i], hi[i], lo[i], close[i], vol[i]
print T[1][2], T[0][0]
z = (hi[2] +lo[2])/2.0
print z
   

***


Danny Yoo wrote:

>On Tue, 23 Aug 2005, Tom Strickland wrote:
>
>  
>
>>I changed the program in accordance with your advice and it now runs
>>perfectly!!!
>>
>>
>
>Hi Tom,
>
>That's very good to hear; glad it's working now.
>
>If you don't mind, can you post up what you have now?  I left out some
>other suggestions to the program because they were more about style and
>Python idioms.  But now that you have a working program, it might be
>useful to cover those.
>
>Good luck!
>
>
>
>  
>


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


Re: [Tutor] IndexError and appending to lists [Was: Re: Need Help on Assignment]

2005-08-23 Thread Danny Yoo


On Tue, 23 Aug 2005, Tom Strickland wrote:

> I changed the program in accordance with your advice and it now runs
> perfectly!!!

Hi Tom,

That's very good to hear; glad it's working now.

If you don't mind, can you post up what you have now?  I left out some
other suggestions to the program because they were more about style and
Python idioms.  But now that you have a working program, it might be
useful to cover those.

Good luck!

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


Re: [Tutor] IndexError and appending to lists [Was: Re: Need Help on Assignment]

2005-08-23 Thread Tom Strickland
Danny,

I changed the program in accordance with your advice and it now runs 
perfectly!!!

Thanks for the education!

Tom Strickland

Danny Yoo wrote:

>[Danny]
>  
>
>>>Anyway, this doesn't answer the problem: how do we add elements to a
>>>list? In Python, we can accumulate elements in a list by append()ing:
>>>  
>>>
>
>[code cut]
>
>
>[Tom]
>  
>
>>So, for example, would I use the following in my "while" loop:
>>
>>  date.append(T[N][0])
>>
>>Before the loop, date would have to be defined as date =[], is this
>>correct?
>>
>>
>
>Yes.  Each of the list variables that you're accumulating should each be
>initialized to an empty list.
>
>Good luck!
>
>
>
>  
>


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


Re: [Tutor] IndexError and appending to lists [Was: Re: Need Help on Assignment]

2005-08-23 Thread Danny Yoo

[Danny]
> >Anyway, this doesn't answer the problem: how do we add elements to a
> >list? In Python, we can accumulate elements in a list by append()ing:

[code cut]


[Tom]
> So, for example, would I use the following in my "while" loop:
>
>   date.append(T[N][0])
>
> Before the loop, date would have to be defined as date =[], is this
> correct?

Yes.  Each of the list variables that you're accumulating should each be
initialized to an empty list.

Good luck!

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


Re: [Tutor] IndexError and appending to lists [Was: Re: Need Help on Assignment]

2005-08-23 Thread Tom Strickland
Danny,

Thanks for your comments and your help. I've added my comments to your 
text below. Hopefully it will be in red so you can easily identify them.

Tom

Danny Yoo wrote:

>Hi Tom,
>
>Before we continue: it looks like you're starting to learn Python.  Have
>you gone through one of the tutorials here?
>
>http://wiki.python.org/moin/BeginnersGuide/NonProgrammers
>
>Alan Gauld's tutorial is especially nice, but all of the tutorials there
>should be useful.  If you go through any one of them, each should help
>address the issues you're running into.
>  
>
Tom's Comments:  I have one of the tutorials and I'm also reading 
Learning Python by Lutz & Ascher.

>
>Ok, let's look at the program.  I'll try to make constructive criticism
>along the way.
>
>  
>
>>input = open('/home/tom/Python/Input/SPY3.txt', 'r')
>>N=0
>>s = 'boo'
>>date =['hoo']
>>T = [0,0,0,0,0,0]   #don't know why this is necessary
>>
>>
>
>There are an awful number of global variables here, and their names do not
>make it clear what the role of each variable is.  What is 's', and what is
>'T'?  And why does date contain the string 'hoo'?
>  
>
Tom's Comments: N is the counter which I initialize here. s is a string 
and I've intiialized it to 'boo' so that it passed the first "while" 
test and entered the loop. The "date" variable is defined just to see if 
it made any difference. I had previously not defined it, and the program 
hung up on the "date" assignment so I tried assigning it a value but it 
didn't help. T is the matrix consisting of several rows of six columns 
each. If I leave out this definition of T =[0,0,0,0,0,0] the program 
hangs up on the line: T[N] = s.split(',')

>Also, the original code was double-spaced; you don't need to do that.
>You're not writing a report that's graded based on filling 80 lines, are
>you?  *grin*
>  
>

Tom's Comments: The original code was written using Eric. In going from 
Eric to Word to Thunderbird e-mail the double space crept in. It's not 
in the original. Also, the line numbers were lost along the way.

>
>
>  
>
>>open = close = hi = lo = vol = [1.0]  #is this necessary?
>>
>>
>
>This looks highly suspicious.
>  
>

Tom's Comments: I just put this in to see if it would help. It didn't. I 
thought it might since Python didn't like for me to use T without first 
assigning it some values.

>In Python, names are just references to things.  That means that all of
>these names --- open, close, hi, low, vol --- are just aliases for the
>same list thing.  For example:
>
>##
>  
>
maui = hawaiian = ['pizza']
hawaiian[0] = 'ham and pineapple pizza'
hawaiian


>['ham and pineapple pizza']
>  
>
maui


>['ham and pineapple pizza']
>##
>
>Notice that because 'maui' and 'hawaiian' are refering to the same list,
>when we change the list, we see that reflected here in both 'maui' and
>'hawaiian'.
>  
>

Tom's Comments: Good point! I didn't realize this.

>
>
>  
>
>>while s:
>>s = input.readline()
>>if s == '':break
>>s = s[:-2]
>>T[N] = s.split(',')
>>date[N] = T[N][0]
>>open[N] = T[N][1]
>>hi[N] = T[N][2]
>>lo[N] = T[N][3]
>>close[N] = T[N][4]
>>vol[N] = T[N][5]
>>N+=1
>>
>>
>
>Ok, there's a problem here.
>
>One thing you may need to know is that Python's lists do not automatically
>expand as we try to enter elements into them.  For example:
>
>##
>  
>
names = ['knuth', 'morris']
names[2] = 'pratt'


>Traceback (most recent call last):
>  File "", line 1, in ?
>IndexError: list assignment index out of range
>##
>
>This breaks because the list is defined contain two elements: trying to
>assign to names[2] goes out of the list bounds, and that's an error in
>Python.  In practice, this "array out of bounds" check is often a Very
>Good Thing because index errors can be evidence of a logical program
>error.
>
>
>Anyway, this doesn't answer the problem: how do we add elements to a list?
>In Python, we can accumulate elements in a list by append()ing:
>
>##
>  
>
names = []
names.append('john')
names.append('lisa')
names.append('erik')
names.append('tina')
names


>['john', 'lisa', 'erik', 'tina']
>##
>
>Does this make sense?
>  
>
Tom's Comments: Yes! So, for example, would I use the following in my 
"while" loop:

  date.append(T[N][0])

Before the loop, date would have to be defined as date =[], is this correct?

>
>
>
>  
>

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


[Tutor] IndexError and appending to lists [Was: Re: Need Help on Assignment]

2005-08-23 Thread Danny Yoo

Hi Tom,

Before we continue: it looks like you're starting to learn Python.  Have
you gone through one of the tutorials here?

http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

Alan Gauld's tutorial is especially nice, but all of the tutorials there
should be useful.  If you go through any one of them, each should help
address the issues you're running into.


Ok, let's look at the program.  I'll try to make constructive criticism
along the way.

> input = open('/home/tom/Python/Input/SPY3.txt', 'r')
> N=0
> s = 'boo'
> date =['hoo']
> T = [0,0,0,0,0,0]   #don't know why this is necessary

There are an awful number of global variables here, and their names do not
make it clear what the role of each variable is.  What is 's', and what is
'T'?  And why does date contain the string 'hoo'?

Also, the original code was double-spaced; you don't need to do that.
You're not writing a report that's graded based on filling 80 lines, are
you?  *grin*



> open = close = hi = lo = vol = [1.0]  #is this necessary?

This looks highly suspicious.

In Python, names are just references to things.  That means that all of
these names --- open, close, hi, low, vol --- are just aliases for the
same list thing.  For example:

##
>>> maui = hawaiian = ['pizza']
>>> hawaiian[0] = 'ham and pineapple pizza'
>>> hawaiian
['ham and pineapple pizza']
>>> maui
['ham and pineapple pizza']
##

Notice that because 'maui' and 'hawaiian' are refering to the same list,
when we change the list, we see that reflected here in both 'maui' and
'hawaiian'.



> while s:
> s = input.readline()
> if s == '':break
> s = s[:-2]
> T[N] = s.split(',')
> date[N] = T[N][0]
> open[N] = T[N][1]
> hi[N] = T[N][2]
> lo[N] = T[N][3]
> close[N] = T[N][4]
> vol[N] = T[N][5]
> N+=1

Ok, there's a problem here.

One thing you may need to know is that Python's lists do not automatically
expand as we try to enter elements into them.  For example:

##
>>> names = ['knuth', 'morris']
>>> names[2] = 'pratt'
Traceback (most recent call last):
  File "", line 1, in ?
IndexError: list assignment index out of range
##

This breaks because the list is defined contain two elements: trying to
assign to names[2] goes out of the list bounds, and that's an error in
Python.  In practice, this "array out of bounds" check is often a Very
Good Thing because index errors can be evidence of a logical program
error.


Anyway, this doesn't answer the problem: how do we add elements to a list?
In Python, we can accumulate elements in a list by append()ing:

##
>>> names = []
>>> names.append('john')
>>> names.append('lisa')
>>> names.append('erik')
>>> names.append('tina')
>>> names
['john', 'lisa', 'erik', 'tina']
##

Does this make sense?


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


Re: [Tutor] Need Help on Assignment (fwd)

2005-08-23 Thread Danny Yoo

[Forwarding to Tutor; next time, please also use Reply-to-All in your
email client.  That way, everyone can help.]


-- Forwarded message --
Date: Tue, 23 Aug 2005 17:19:43 -0500
From: Tom Strickland <[EMAIL PROTECTED]>
To: Danny Yoo <[EMAIL PROTECTED]>
Subject: Re: [Tutor] Need Help on Assignment


>Do you mind if you show us what you've tried so far?
>
>Also, show us the exact syntax error and the stack trace that Python
>reports.  It'll help us to see what's really going on.  Your paraphrase of
>the situation is useful, but it's obscuring information that we need to
>see to be able to duplicate your problem.
>
>If your program is short, just copy-and-paste it into your reply.


Danny,

Here's a copy of the current version of the program and the terminal
output. Hope this helps!

Thank you!

Tom Strickland

***

*_PROGRAM:_*



#!/usr/bin/python2.4

input = open('/home/tom/Python/Input/SPY3.txt', 'r')

N=0

s = 'boo'

date =['hoo']

T = [0,0,0,0,0,0]   #don't know why this is necessary

open = close = hi = lo = vol = [1.0]  #is this necessary?

while s:

s = input.readline()

if s == '':break

s = s[:-2]

T[N] = s.split(',')

date[N] = T[N][0]

open[N] = T[N][1]

hi[N] = T[N][2]

lo[N] = T[N][3]

close[N] = T[N][4]

vol[N] = T[N][5]

N+=1

print N

for i in range(N):

T[i]

print T[i]

print date[i], open[i], hi[i], lo[i], close[i], vol[i]

print T[1][2], T[0][0]







*_TERMINAL OUTPUT:_*

*_ _*

[EMAIL PROTECTED] ~]$ cd Python/SourceCode

[EMAIL PROTECTED] SourceCode]$ ./ispy.py

Traceback (most recent call last):

  File "./ispy.py", line 13, in ?

date[N] = T[N][0]

IndexError: list assignment index out of range

[EMAIL PROTECTED] SourceCode]$


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


[Tutor] Counting help

2005-08-23 Thread Python
listofnames = nameofsender[0], listofnames

does not add a name to a list.  Rather it creates a tuple of the new
name and the list and then binds the tuple to the list name.  That's why
you wind up with the lisp style list.

To add a name to the head of the list use
listofnames.insert(0, nameofsender[0])


If you are using a version of Python that supports sets, using sets
would be much simpler since the duplicates get discarded automatically.

import sets # python2.3
setofnames = sets.Set()
while.
setofnames.add(nameofsender[0])

len(setofnames) # count of distinct names

-- 
Lloyd Kvam
Venix Corp

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


Re: [Tutor] Remove a number from a string

2005-08-23 Thread Kent Johnson
Alan G wrote:
>>Suppose i have a string '347 liverpool street'.
>>I want to remove all the numbers coming at the starting of the 
>>string.
>>I can think of a few ways but whats the cleanest way to do it?
> 
> 
> If you know they are always a contiguous string themn a simple split() 
> call will do it:
> 
> s = ' '.join(s.split()[1:])

or just 
s = s.split(None, 1)[1]

As Luis pointed out, you may want to define your requirements a bit more 
closely to help choose between the different approaches. For example, what do 
you want to get from
347-10 liverpool street
347A liverpool street
347 A liverpool street
123 45th street
#1 liverpool street

Kent

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


Re: [Tutor] Remove a number from a string

2005-08-23 Thread Alan G
> Suppose i have a string '347 liverpool street'.
> I want to remove all the numbers coming at the starting of the 
> string.
> I can think of a few ways but whats the cleanest way to do it?

If you know they are always a contiguous string themn a simple split() 
call will do it:

s = ' '.join(s.split()[1:])

If the numbes may form a more complex pattern then a regex would be 
better.
Use the regex to find the end index and use a slice around the index.

Alan G. 

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


Re: [Tutor] Need Help on Assignment

2005-08-23 Thread Danny Yoo


On Tue, 23 Aug 2005, Tom Strickland wrote:

> I have imported a text file consisting of lines of csv.

[problem statement cut]

> Python doesn't like the ways I've attempted to make these assignments.


Do you mind if you show us what you've tried so far?

Also, show us the exact syntax error and the stack trace that Python
reports.  It'll help us to see what's really going on.  Your paraphrase of
the situation is useful, but it's obscuring information that we need to
see to be able to duplicate your problem.

If your program is short, just copy-and-paste it into your reply.

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


Re: [Tutor] Sort a Set

2005-08-23 Thread Terry Carroll
On Tue, 23 Aug 2005, Kent Johnson wrote:

> Actually the reverse parameter is new in 2.4 too, you have to do that in
> a separate step also:

Doh!


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


[Tutor] Need Help on Assignment

2005-08-23 Thread Tom Strickland
I have imported a text file consisting of lines of csv. Each line 
consists of six items separated by commas. I have converted this file 
into a matrix of string variables, T[i]. Now I want to extract each of 
the individual strings, convert five them to floats, and save them in 
individual vectors. That is, I'd like to convert each T[i] into date[i] 
(a string), open[i] (a float) etc. Python doesn't like the ways I've 
attempted to make these assignments.

T[i], which is a six-column matrix, works fine. However, if I say 
date[i] = T[i][0], it doesn't like this. I've tried several variations 
on this but with no success. Things like:  [date[i],open[i], etc] = T[i] 
didn't work either.

What do I need to do to make this type of assignment?

Thank you.


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


Re: [Tutor] Remove a number from a string

2005-08-23 Thread Byron
Shitiz Bansal wrote:

> Hi,
> Suppose i have a string '347 liverpool street'.
> I want to remove all the numbers coming at the starting of the string.
> I can think of a few ways but whats the cleanest way to do it?
>  
> Shitiz


Here's a function that can do what you're wanting to accomplish:

Byron
---

def removeNums(addr):
text = addr.split()
revisedAddr = ""
for item in text:
try:
i = int(item)
except:
revisedAddr += item + " "
return revisedAddr.strip()

# Test the function...  ;-)
address = "5291 E. 24rd Ave."
print removeNums(address)




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


Re: [Tutor] Counting help

2005-08-23 Thread Byron
Hi Scott,

The site ( http://www.greenteapress.com ) has a wonderful tutorial on it 
for Python that quickly teaches one (within a few minutes) how to work 
with dictionaries.  I would highly recommend that you check it out.  
It's well worth it...

Byron


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


Re: [Tutor] Counting help

2005-08-23 Thread Kent Johnson
Luis N wrote:
> Ideally, you would put your names into a list or dictionary to make
> working with them easier. If all you're trying to do is count them
> (and your list of names is long), you might consider a dictionary
> which you would use like so:
> 
> #This is just the first thing I considered.
> 
> l = ['a list of names']
> 
> d = {}
> 
> for name in namelist:
> if d.has_key(name):
> x = d.get(name)
> d[name] = x + 1
> else:
> d[name] = 1 

dict.get() allows a default argument that will be used if the key doesn't 
exist, so this can be shortened to
for name in namelist:
  d[name] = d.get(name, 0) + 1

Kent

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


Re: [Tutor] Counting help

2005-08-23 Thread Kent Johnson
Scott Oertel wrote:
> The next problem I have though is creating the dict,
> 
> i have a loop, but i can't figure out how to compile the dict,  it is 
> returning this: ('Joey Gale', ('Scott Joe', 'This is lame' )))
> 
> 
> listofnames = []
> while (cnt < number[1][0]):
> if (date[2] == today[2]):
> test = regex.findall(M.fetch(int(number[1][0]) - cnt, 
> '(BODY[HEADER.FIELDS (FROM)])')[1][0][1].rstrip())
> cnt += 1
> if (nameofsender != []):
> print nameofsender[0]
> listofnames = nameofsender[0], listofnames

I think you want 
  listofnames.append(nameofsender[0])
which will add nameofsender[0] to the list. What you have -
  listofnames = nameofsender[0], listofnames
is making a tuple (a pair) out of the new name and the old list, and assigning 
it to listofnames. Kind of like CONS in LISP - but Python lists are more like 
arrays than like LISP lists.

Kent

> else:
> no_name += 1
> else: break
> 
> 
> 
> 
> 
> ___
> 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] Counting help

2005-08-23 Thread Alan G

>I have extracted a list of names, i.e.
>
> "Joe Smith"
> "Joe Smith"
> "Jack Smith"
> "Sam Love"
> "Joe Smith"
>
> I need to be able to count the occurances of these names and I 
> really don't have any idea where to begin.

The classic way to do this kind of thing is with a dictionary:

names = [
 "Joe Smith",
 "Joe Smith",
 "Jack Smith",
 "Sam Love",
 "Joe Smith"]

counts = {}

for name in names:
if name in counts:
counts[name] += 1
else: counts[name] = 1

print counts

You could also use a list comprehension combined with the list
count() method but I doubt if its much faster.

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld 

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


Re: [Tutor] Sort a Set

2005-08-23 Thread Hugo González Monteverde
I've done:

undup = []
for i in list:
 if i not in undup:
 undup.append(i)

Which is simple an not very pythonic, but does the trick.

Hugo



Jonas Melian wrote:
> I get a list of repeated numbers  [24, 24, 24, 16, 16, 15, 15 ]
> Is possible get it without repeated numbers, without using set()?
> 
> If I use set, then the list is unsorted and i cann't sorting it.
> 
> For get the values i use:
> 
> [x[0] for x in cardTmp]
> 
> or:
> 
> from itertools import imap
> for i in imap(lambda x: x[0], cardTmp): print i
> 
> A idea it would be create a generator that will return elements one by 
> one, and then it would be possible know if that element is in the new 
> list. But generators are in 2.4
> 
> 
> Python 2.3.5
> 
> ___
> 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] Counting help

2005-08-23 Thread Scott Oertel




Scott Oertel wrote:

  
  
Byron wrote:
  
Luis N wrote:

  

  Ideally, you would put your names into a list or dictionary to make
working with them easier. If all you're trying to do is count them
(and your list of names is long), you might consider a dictionary
which you would use like so:

#This is just the first thing I considered.

l = ['a list of names']

d = {}

for name in namelist:
   if d.has_key(name):
   x = d.get(name)
   d[name] = x + 1
   else:
   d[name] = 1  




100% agreed.  I have used this approach before and it works great... 

Byron

  
  
Thanks for the snipplet, it's perfect for what I'm doing, I wasn't
aware of the has_key() or get(), this is very usefull.
  
  
-Scott Oertel
  
  
  
  
  

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

The next problem I have though is creating the dict, 

i have a loop, but i can't figure out how to compile the dict,  it is
returning this: ('Joey Gale', ('Scott Joe', ('This is lame' )))


listofnames = []
while (cnt < number[1][0]):
    if (date[2] == today[2]):
    test = regex.findall(M.fetch(int(number[1][0]) - cnt,
'(BODY[HEADER.FIELDS (FROM)])')[1][0][1].rstrip())
    cnt += 1
    if (nameofsender != []):
    print nameofsender[0]
    listofnames = nameofsender[0], listofnames
    else:
    no_name += 1
    else: break




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


Re: [Tutor] Counting help

2005-08-23 Thread Scott Oertel




Scott Oertel wrote:

  
  
Byron wrote:
  
Luis N wrote:

  

  Ideally, you would put your names into a list or dictionary to make
working with them easier. If all you're trying to do is count them
(and your list of names is long), you might consider a dictionary
which you would use like so:

#This is just the first thing I considered.

l = ['a list of names']

d = {}

for name in namelist:
   if d.has_key(name):
   x = d.get(name)
   d[name] = x + 1
   else:
   d[name] = 1  




100% agreed.  I have used this approach before and it works great... 

Byron

  
  
Thanks for the snipplet, it's perfect for what I'm doing, I wasn't
aware of the has_key() or get(), this is very usefull.
  
  
-Scott Oertel
  
  
  
  
  

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

The next problem I have though is creating the dict, 

i have a loop, but i can't figure out how to compile the dict,  it is
returning this: ('Joey Gale', ('Scott Joe', 'This is lame' )))


listofnames = []
while (cnt < number[1][0]):
    if (date[2] == today[2]):
    test = regex.findall(M.fetch(int(number[1][0]) - cnt,
'(BODY[HEADER.FIELDS (FROM)])')[1][0][1].rstrip())
    cnt += 1
    if (nameofsender != []):
    print nameofsender[0]
    listofnames = nameofsender[0], listofnames
    else:
    no_name += 1
    else: break




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


Re: [Tutor] Counting help

2005-08-23 Thread Scott Oertel




Byron wrote:

  Luis N wrote:

  
  
Ideally, you would put your names into a list or dictionary to make
working with them easier. If all you're trying to do is count them
(and your list of names is long), you might consider a dictionary
which you would use like so:

#This is just the first thing I considered.

l = ['a list of names']

d = {}

for name in namelist:
   if d.has_key(name):
   x = d.get(name)
   d[name] = x + 1
   else:
   d[name] = 1  


  
  
100% agreed.  I have used this approach before and it works great... 

Byron

  

Thanks for the snipplet, it's perfect for what I'm doing, I wasn't
aware of the has_key() or get(), this is very usefull.


-Scott Oertel






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


Re: [Tutor] Counting help

2005-08-23 Thread Byron
Luis N wrote:

>Ideally, you would put your names into a list or dictionary to make
>working with them easier. If all you're trying to do is count them
>(and your list of names is long), you might consider a dictionary
>which you would use like so:
>
>#This is just the first thing I considered.
>
>l = ['a list of names']
>
>d = {}
>
>for name in namelist:
>if d.has_key(name):
>x = d.get(name)
>d[name] = x + 1
>else:
>d[name] = 1  
>

100% agreed.  I have used this approach before and it works great... 

Byron


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


Re: [Tutor] Counting help

2005-08-23 Thread Luis N
On 8/23/05, Scott Oertel <[EMAIL PROTECTED]> wrote:
> I have extracted a list of names, i.e.
> 
> "Joe Smith"
> "Joe Smith"
> "Jack Smith"
> "Sam Love"
> "Joe Smith"
> 
> I need to be able to count the occurances of these names and I really
> don't have any idea where to begin.
> 
> Any ideas?  excuse me this is my first post to this list, I hope I
> included enough information.
> 
> 
> -Scott Oertel

Ideally, you would put your names into a list or dictionary to make
working with them easier. If all you're trying to do is count them
(and your list of names is long), you might consider a dictionary
which you would use like so:

#This is just the first thing I considered.

l = ['a list of names']

d = {}

for name in namelist:
if d.has_key(name):
x = d.get(name)
d[name] = x + 1
else:
d[name] = 1  

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


Re: [Tutor] Remove a number from a string

2005-08-23 Thread Kent Johnson
Shitiz Bansal wrote:
> Hi,
> Suppose i have a string '347 liverpool street'.
> I want to remove all the numbers coming at the starting of the string.
> I can think of a few ways but whats the cleanest way to do it?

With str.lstrip():

 >>> '347 liverpool street'.lstrip('0123456789')
' liverpool street'

or, if you want to strip the space as well:

 >>> '347 liverpool street'.lstrip('0123456789 ')
'liverpool street'

With a regular expression:

 >>> import re
 >>> re.sub('^[0-9]+', '', '347 liverpool street')
' liverpool street'

or

 >>> re.sub('^[0-9 ]+', '', '347 liverpool street')
'liverpool street'

If you are doing this a lot my guess is that a compiled re will be faster but 
that's just a guess...

Kent

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


Re: [Tutor] Remove a number from a string

2005-08-23 Thread Luis N
On 8/23/05, Shitiz Bansal <[EMAIL PROTECTED]> wrote:
> Hi, 
> Suppose i have a string '347 liverpool street'. 
> I want to remove all the numbers coming at the starting of the string. 
> I can think of a few ways but whats the cleanest way to do it? 
>   
> Shitiz
> 

I believe this question to be rather variable in its answer. If all
your strings are of the form "### word word", then you could safetly
use 'split', as in:

>>> s = '347 liverpool street'

def splitter(s):
l = s.split()
try:
i = int(l[0])
return i
except ValueError:
print "String segment not a number"

>>> splitter(s)
347

But, if your string is also of the form "347-10 liverpool street",
then your problem is more complex.

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


[Tutor] Remove a number from a string

2005-08-23 Thread Shitiz Bansal
Hi,
Suppose i have a string '347 liverpool street'.
I want to remove all the numbers coming at the starting of the string.
I can think of a few ways but whats the cleanest way to do it?
 
Shitiz__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


Re: [Tutor] Sort a Set

2005-08-23 Thread Kent Johnson
Terry Carroll wrote:
> Sorry, I missed you were on 2.3.x, and I think sorted() is new with 2.4.  
> You'd instead have to do the sort in a separate step:
> 
> 
l=[24, 24, 15, 16, 16, 15, 24]
l=list(set(l))
l.sort(reverse=True)
l
> 
> [24, 16, 15]

Actually the reverse parameter is new in 2.4 too, you have to do that in a 
separate step also:
l=list(set(l))
l.sort()
l.reverse()

Kent

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


[Tutor] Counting help

2005-08-23 Thread Scott Oertel
I have extracted a list of names, i.e.

"Joe Smith"
"Joe Smith"
"Jack Smith"
"Sam Love"
"Joe Smith"

I need to be able to count the occurances of these names and I really 
don't have any idea where to begin.

Any ideas?  excuse me this is my first post to this list, I hope I 
included enough information.


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


Re: [Tutor] Sort a Set

2005-08-23 Thread Terry Carroll
On Tue, 23 Aug 2005, Terry Carroll wrote to Jonas:

> I don't know if you're in a position to rely on the sortedness of the 
> input data, but even if not, this works:
> 
> >>> l=[24, 24, 15, 16, 16, 15, 24]
> >>> l=sorted(list(set(l)), reverse=True)
> >>> l
> [24, 16, 15]

Sorry, I missed you were on 2.3.x, and I think sorted() is new with 2.4.  
You'd instead have to do the sort in a separate step:

>>> l=[24, 24, 15, 16, 16, 15, 24]
>>> l=list(set(l))
>>> l.sort(reverse=True)
>>> l
[24, 16, 15]

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


Re: [Tutor] Sort a Set

2005-08-23 Thread Terry Carroll
On Tue, 23 Aug 2005, Jonas Melian wrote:

> I get a list of repeated numbers  [24, 24, 24, 16, 16, 15, 15 ]
> Is possible get it without repeated numbers, without using set()?
> 
> If I use set, then the list is unsorted and i cann't sorting it.

Converting it to a set will eliminate dupes, and converting it back to 
list will make it sortable.

I don't know if you're in a position to rely on the sortedness of the 
input data, but even if not, this works:

>>> l=[24, 24, 15, 16, 16, 15, 24]
>>> l=sorted(list(set(l)), reverse=True)
>>> l
[24, 16, 15]

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


Re: [Tutor] reading excel and access files

2005-08-23 Thread Jeff Peery
Great, this is lots of good info. thanks everyone for your input!
 
JeffKent Johnson <[EMAIL PROTECTED]> wrote:
Jeff Peery wrote:> hello, can python read excel and access files? If so where do I go to > read about how this would work? thanks.There are some resources here that might be helpful:http://www.python.org/pypi?%3Aaction=search&name=&version=&summary=&description=&keywords=excel&_pypi_hidden=0Kent___Tutor maillist - Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using join instead of string.joinfield

2005-08-23 Thread Harper, Gina
Easy.  
Suppose you want to join a list of words with hyphens.
>>> a_list = ['this', 'is', 'a', 'list', 'of', 'words']
>>> sep_char = '-'
>>> print sep_char.join(a_list)
this-is-a-list-of-words
>>> 
*g*
-Original Message-
From: Bernard Lebel [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, August 23, 2005 11:04 AM
To: Python Tutor list
Subject: [Tutor] Using join instead of string.joinfield


Hello,

The documentation says that the built-in join method now replaces the
string.joinfield function.  However how do you achieve the same
operation? The join method accepts only one argument, that is, the list
of strings to join. How do you then specify the separating character?


Thanks
Bernard
___
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] Using join instead of string.joinfield

2005-08-23 Thread Kent Johnson
Bernard Lebel wrote:
> Hello,
> 
> The documentation says that the built-in join method now replaces the
> string.joinfield function.  However how do you achieve the same
> operation? The join method accepts only one argument, that is, the
> list of strings to join. How do you then specify the separating
> character?

join() is a method of a string object. The string you invoke it on is the 
separating character. It may seem a bit strange but it works:

 >>> l=['hello', 'world', 'how are you?']
 >>> ', '.join(l)
'hello, world, how are you?'

Kent

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


Re: [Tutor] Using join instead of string.joinfield

2005-08-23 Thread Bernard Lebel
Oh. Thanks a lot for that, now that you told me, the doc about join
makes lot more sense!


Thanks
Bernard


On 8/23/05, Harper, Gina <[EMAIL PROTECTED]> wrote:
> Easy.
> Suppose you want to join a list of words with hyphens.
> >>> a_list = ['this', 'is', 'a', 'list', 'of', 'words']
> >>> sep_char = '-'
> >>> print sep_char.join(a_list)
> this-is-a-list-of-words
> >>>
> *g*
> -Original Message-
> From: Bernard Lebel [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, August 23, 2005 11:04 AM
> To: Python Tutor list
> Subject: [Tutor] Using join instead of string.joinfield
> 
> 
> Hello,
> 
> The documentation says that the built-in join method now replaces the
> string.joinfield function.  However how do you achieve the same
> operation? The join method accepts only one argument, that is, the list
> of strings to join. How do you then specify the separating character?
> 
> 
> Thanks
> Bernard
> ___
> 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] Using join instead of string.joinfield

2005-08-23 Thread Bernard Lebel
Hello,

The documentation says that the built-in join method now replaces the
string.joinfield function.  However how do you achieve the same
operation? The join method accepts only one argument, that is, the
list of strings to join. How do you then specify the separating
character?


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


Re: [Tutor] Sort a Set

2005-08-23 Thread Kent Johnson
Jonas Melian wrote:
> I get a list of repeated numbers  [24, 24, 24, 16, 16, 15, 15 ]
> Is possible get it without repeated numbers, without using set()?
> 
> If I use set, then the list is unsorted and i cann't sorting it.
> 
> A idea it would be create a generator that will return elements one by 
> one, and then it would be possible know if that element is in the new 
> list. But generators are in 2.4

No, generators and iterators (for which generators are a shortcut) were 
introduced in Python 2.2, though to use a generator in 2.2 you must include
from __future__ import generators
in your code. Generator *expressions* - which create an iterator instead of a 
list - are new with 2.4.

Here is a simple generator that returns a single element from each run of items 
in a sequence; this works in Python 2.3.5:

 >>> def unique(seq):
 ...   last = object()  # unique marker for no last object
 ...   for item in seq:
 ... if item != last:
 ...   yield item
 ... last = item
 ...
 >>> nums = [24, 24, 24, 16, 16, 15, 15 ]
 >>> list(unique(nums))
[24, 16, 15]

Kent

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


[Tutor] Sort a Set

2005-08-23 Thread Jonas Melian
I get a list of repeated numbers  [24, 24, 24, 16, 16, 15, 15 ]
Is possible get it without repeated numbers, without using set()?

If I use set, then the list is unsorted and i cann't sorting it.

For get the values i use:

[x[0] for x in cardTmp]

or:

from itertools import imap
for i in imap(lambda x: x[0], cardTmp): print i

A idea it would be create a generator that will return elements one by 
one, and then it would be possible know if that element is in the new 
list. But generators are in 2.4


Python 2.3.5

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


Re: [Tutor] Tk canvas question

2005-08-23 Thread Kent Johnson
铁石 wrote:
>I am writing a resource manager like program !
> the files of directory is display on canvas and taged.
> the tag and file name is keep in a dictionary!
> The tag is a increaseing number from 1,so I build the 
> dictionary like (1:file1,2:file2,..).
> While geting into another directory,I try to remove all 
> object and tags on the canvas so that the tags would be 
> count form 1 again.But I found out the tag number is 
> increasing, not recount form 1. My dictionary can be 
> organize that simple.

This sounds like a bug in your program; you should be able to start over with 
the tags. Can you show us some code?

>I want to know is there a way to get the tag of canvas 
> recount form 1 again? or even more simple ,get the filename 
> I had wrote in canvas directly? 

You can use the itemcget() method of the canvas to retrieve the text:

 >>> root = Tk()
 >>> canvas=Canvas(root)
 >>> t=canvas.create_text(10, 10, text='Hello', tag='1')
 >>> canvas.pack()
 >>> canvas.itemcget('1', 'text')
'Hello'

There does seem to be something strange with reusing the tags - if I create a 
new element with tag '1' I can't retrieve it by tag:

 >>> canvas.delete('1')
 >>> t=canvas.create_text(10, 10, text='Goodbye', tag='1')
 >>> canvas.itemcget('1', 'text')
''
 >>> canvas.find_withtag('1')
()

even though if I ask what tags are on the item it says '1':

 >>> t
2
 >>> canvas.itemcget(2, 'text')
'Goodbye'
 >>> canvas.itemcget(2, 'tags')
'1'
 >>> canvas.find_withtag('1')

Maybe you could just remember the handles to the text items instead of using 
tags?

Kent

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