How to put string in a set as an individual item?
Asked Answered
D

5

12

I have a string

sample = "http://www.stackoverflow.com"

I want convert this string to a set

final = {"http://www.stackoverflow.com"}

I tried the following code:

final = set(sample)

But i got the wrong out as

{u'.', u'/', u':', u'a', u'b', u'c', u'e', u'h', u'i', u'k', u'l', u'n', u'p', u's', u't', u'w'}

I also used

final  = ast.literal_eval(Sample)

and I got this

SyntaxError: invalid syntax

Is there any other solution for this

Deming answered 28/11, 2014 at 16:10 Comment(4)
possible duplicate of Python convert set to string and vice versaFacultative
I tried answers of both Machael and Lutz but I don't know for which answer shall i mark it as correctDeming
So I will mark according to the oldest postDeming
You can upvote more than one answer.Doorkeeper
D
13

Just do it:

In [1]: s = "http://www.stackoverflow.com"

In [2]: f = {s}

In [3]: type(f)
Out[3]: builtins.set

In [4]: f
Out[4]: {'http://www.stackoverflow.com'}
Doorkeeper answered 28/11, 2014 at 16:17 Comment(0)
M
9
sample = "http://www.stackoverflow.com"
final = set((sample, ))
Markitamarkka answered 28/11, 2014 at 16:17 Comment(0)
B
7

The set() class ,which is also considered a built-in type, accepts an iterable and returns the unique items from that iterable in a set object. Here since strings are considered a form of iterable --of characters-- you can't just call it on your string. Instead, you can either put the string object literally inside a set while defining it or if you're forced to use set() you can put it inside another iterable such as list or tuple before you pass it to set().

In [14]: s = {'sample string'}                                                                                                                                                                              

In [15]: s                                                                                                                                                                                                  
Out[15]: {'sample string'}

In [16]: s = set(['sample string'])                                                                                                                                                                         

In [17]: s                                                                                                                                                                                                  
Out[17]: {'sample string'}
Blackcap answered 28/11, 2014 at 16:14 Comment(0)
S
0

The easiest way is final = {sample}.

Sideward answered 23/3, 2020 at 14:41 Comment(0)
T
0

you can do it with:

final = set(sample.split())

And if you had a string separated by spaces, it would leave you each word separated within the set.

example= "www.example.com www.natsoft.co www.google.com"

result = set(example.split())
print(result)

{'www.example.com', 'www.natsoft.co', 'www.google.com'}
Thrombophlebitis answered 9/6, 2022 at 3:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.