Re: [Tutor] A simple list question...

2006-09-08 Thread Alan Gauld
> 1. How can I easily strip out the newline characters from the 
> elements
> of cleanedlist?

You can use the string strip() method.

> 2. Is there a better way to achieve my objective (ie. a list method
> for generating the cleaned list?)

You can convert the list to a Set.

>>> L = [1,2,1,3,4,2]
>>> s = set(L)
>>> s
set([1, 2, 3, 4])
>>>

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] A simple list question...

2006-09-07 Thread Richard Querin
On 9/7/06, Kent Johnson <[EMAIL PROTECTED]> wrote:

> No, it doesn't. You are confused somewhere; my guess is your original
> data has newlines.

Sorry, my bad. When I created the original list I was splitting a
string in two pieces. The latter portion of the string had a newline
at the end. I had taken the slice with [index:] instead of [index:-1].

Thanks for the help and the tips regarding other ways to do it.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A simple list question...

2006-09-07 Thread John Fouhy
On 08/09/06, Richard Querin <[EMAIL PROTECTED]> wrote:
> I've got a list of strings. There are some duplicates. I want a list
> of only the unique entries in that list. So I do the following:
>
> mylist = ['project1' , 'project2', 'project3', 'project4', 'project1']
>
> d = {}
>
> for item in mylist:
>d[item] = None
>
> cleanedlist = d.keys()
>
> But d.keys() seems to add '\n' to each entry in cleanedlist.

Um.  I'm not in a position to test your code right now, but I can't
think of any reason why it would do that..

> 1. How can I easily strip out the newline characters from the elements
> of cleanedlist?

You could do [s.strip() for s in cleanedlist] -- the .strip() string
method will strip whitespace from both ends of the string.

> 2. Is there a better way to achieve my objective (ie. a list method
> for generating the cleaned list?)

cleanedlist = list(set(mylist))

If you don't have python 2.4+, you will need to import the sets module.

Also, depending on what you are doing with cleanedlist, you could just
leave it as a set.  Or even construct mylist as a set.

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


Re: [Tutor] A simple list question...

2006-09-07 Thread Kent Johnson
Richard Querin wrote:
> I've got a list of strings. There are some duplicates. I want a list
> of only the unique entries in that list. So I do the following:
> 
> mylist = ['project1' , 'project2', 'project3', 'project4', 'project1']
> 
> d = {}
> 
> for item in mylist:
> d[item] = None
> 
> cleanedlist = d.keys()
> 
> 
> But d.keys() seems to add '\n' to each entry in cleanedlist.

No, it doesn't. You are confused somewhere; my guess is your original 
data has newlines. Using your code above exactly:

In [1]: mylist = ['project1' , 'project2', 'project3', 'project4', 
'project1']

In [2]: d = {}

In [3]: for item in mylist:
...: d[item] = None
...:

In [4]: cleanedlist = d.keys()

In [5]: cleanedlist
Out[5]: ['project4', 'project1', 'project3', 'project2']

No newlines here.

> 2. Is there a better way to achieve my objective (ie. a list method
> for generating the cleaned list?)

If you don't care about the order of items in the new list, just convert 
to a set and back (essentially a more concise version of what you did 
with a dict):

In [6]: list(set(mylist))
Out[6]: ['project4', 'project1', 'project3', 'project2']

Kent

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


[Tutor] A simple list question...

2006-09-07 Thread Richard Querin
I've got a list of strings. There are some duplicates. I want a list
of only the unique entries in that list. So I do the following:

mylist = ['project1' , 'project2', 'project3', 'project4', 'project1']

d = {}

for item in mylist:
d[item] = None

cleanedlist = d.keys()


But d.keys() seems to add '\n' to each entry in cleanedlist.

1. How can I easily strip out the newline characters from the elements
of cleanedlist?
2. Is there a better way to achieve my objective (ie. a list method
for generating the cleaned list?)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor