[Tutor] problem with using set

delegbede at dudupay.com delegbede at dudupay.com
Thu Oct 13 17:18:24 CEST 2011


+1 Ramit. 
Sent from my BlackBerry wireless device from MTN

-----Original Message-----
From: "Prasad, Ramit" <ramit.prasad at jpmorgan.com>
Sender: tutor-bounces+delegbede=dudupay.com at python.org
Date: Thu, 13 Oct 2011 11:09:24 
To: tutor at python.org<tutor at python.org>
Subject: Re: [Tutor] problem with using set

Approach:-
>>> a='hello'
>>> set(a)
set(['h', 'e', 'l', 'o']), so i am thinking somehow if i remove 'l' and i join the string, i will get the desired output.

2.
>>> a='microsoft'
>>> set(a)
set(['c', 'f', 'i', 'm', 'o', 's', 'r', 't'])
>>> print ''.join(set(a))
cfimosrt

When i print "Hello", i get the output as "helo"(in same sequence) but when i write "microsoft" i get this-"cfimosrt". So, it means i can't predict the outcome of set(a)??
=========================================================================

Are you required to use set? If you are not, I think the following will be easier. 
>>> 'hello'.replace( 'l', '' )
'heo'
>>> 'microsoft'.replace( 'o', '' )
'micrsft'

If you require set, you can do:

>>> s = set("microsoft")
>>> s
set(['c', 'f', 'i', 'm', 'o', 's', 'r', 't'])
>>> s.remove('o')
>>> string = []
>>> for letter in 'microsoft':
...     if letter in s:
...         string.append( letter )
...     
>>> ''.join( string )
'micrsft'


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list