Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sets in Python 2.4.2

Status
Not open for further replies.

eamc

Technical User
Sep 3, 2001
83
US
In Pyton 2.4.2, set1=set(['abracadabra']) results in 'abracadabra' as a member, while set2=set('abracadabra') results in 'a' 'r' 'b' 'c' as members. How do I get the input from a raw_input query into a set1 type set? How should the input be made?

Edwin M.
 
A string is a special case of a sequence. Since set expects a sequence as its argument, it splits it into component elements. The first example you gave, since the argument is a sequence containing a sequence is the correct way to accomplish what you want.

As a side note, if you'd tried you second example passing in a number instead of a string (which is a sequence) you would have gotten an exception indicating that a sequence is required.
 
eamc,

set1 has a list as it's basic element - so if you want to get a string entered by the user in response to a raw_input, this may be the way to go:

set1 = set([raw_input("What's the magic word?: ")])

Unfortunately, I don't have 2.4 installed (yet) so can't verify this, but if the above doesn't work, the solution below is bound to:

x = [raw_input("What's the magic word?: ")]
set1 = set(x)

Hope this helps,

- Problemsolver
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top