Re: [Tutor] problem with using set

2011-10-13 Thread Prasad, Ramit
Original Message-
From: tutor-bounces+ramit.prasad=jpmorgan@python.org 
[mailto:tutor-bounces+ramit.prasad=jpmorgan@python.org] On Behalf Of 
Prasad, Ramit
Sent: Thursday, October 13, 2011 10:09 AM
To: tutor@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'
==
After further thought, I think you might want to keep one of each character and 
not remove them all. If so, see below.


>>> string = 'microsoft'
>>> dct = {}
>>> for letter in string:
... dct[ letter ] = string.count( letter )
... 
>>> for letter, count in dct.iteritems():
... reversed_string = string[::-1]  # reverse string to leave first 
occurrence
... reversed_string = reversed_string.replace( letter, '', count-1 )
... string = reversed_string[::-1] # return it to normal
... 
>>> string
'microsft'

If you do not care which duplicate character is left you can skip the lines 
with [::-1] and it will leave the last one in the string.


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@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem with using set

2011-10-13 Thread Dave Angel

On 10/13/2011 10:44 AM, Praveen Singh wrote:



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)??

The set() function converts its input into a set.  A set is not ordered 
in the usual way, but in such a way as to rapidly find whether a 
particular item exists already in the set, and insert it if not.  
Inserting a new item might totally rearrange the existing ones, set 
doesn't promise anything at all about the order.  (Dictionaries are 
similar, but while set has only a key, dict has both key and value)


A set has no value to the assignment as stated.  Stick to lists.


--

DaveA

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


Re: [Tutor] problem with using set

2011-10-13 Thread delegbede
+1 Ramit. 
Sent from my BlackBerry wireless device from MTN

-Original Message-
From: "Prasad, Ramit" 
Sender: tutor-bounces+delegbede=dudupay@python.org
Date: Thu, 13 Oct 2011 11:09:24 
To: tutor@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@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem with using set

2011-10-13 Thread Prasad, Ramit
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@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem with using set

2011-10-13 Thread delegbede
What exactly do you intend to get as an output?
Sent from my BlackBerry wireless device from MTN

-Original Message-
From: Praveen Singh 
Sender: tutor-bounces+delegbede=dudupay@python.org
Date: Thu, 13 Oct 2011 10:44:53 
To: 
Subject: [Tutor] problem with using set

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


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