Python Forum
.Set - Unable to understand the statement - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: .Set - Unable to understand the statement (/thread-28758.html)



.Set - Unable to understand the statement - ateestructural - Aug-02-2020

I have the following code which I have inherited it from someone else.
vocab_filename = 'vocab.txt'
vocab = load_doc(vocab_filename)
vocab = vocab.split()
vocab = set(vocab)
I know in the steps above: document gets loaded into the memory
(vocab = load_doc(vocab_filename)
, split into words
vocab = vocab.split()
but I do not follow what this last line is actually doing;

vocab = set(vocab)
Can someone help me understand what the above line does?

Thanks a lot, in advance


RE: .Set - Unable to understand the statement - ndc85430 - Aug-02-2020

It takes vocab and creates a set from whatever is in it. Sets are collections that are unordered and contain unique items (and those items must be hashable). See the docs.


RE: .Set - Unable to understand the statement - ibreeden - Aug-02-2020

(Aug-02-2020, 01:00 PM)ateestructural Wrote:
vocab = set(vocab)
Can someone help me understand what the above line does?

It creates a set of the list.
A set is much like a list, but it contains no duplicates.


RE: .Set - Unable to understand the statement - deanhystad - Aug-02-2020

Asking for help is not going to get you very far. It is far more instructional to figure this out for yourself.

When I don't know what is going on I resort to adding print statements. This is the first thing I would do to your code if I didn't know Python at all:
vocab_filename = 'vocab.txt'
print(type(vocab_filename), vocab_filename)
vocab = load_doc(vocab_filename)
print(type(vocab), vocab)
vocab = vocab.split()
print(type(vocab), vocab)
vocab = set(vocab)
print(type(vocab), vocab)
I use "type(thing)" because knowing the type of a thing is the first step in understanding what the thing does. Once you know the type you can google the type and find out more information.


RE: .Set - Unable to understand the statement - ndc85430 - Aug-02-2020

(Aug-02-2020, 01:13 PM)ibreeden Wrote: A set is much like a list

Not really, since sets don't have a notion of order of the elements, can only contain hashable items, support different operations (union, intersection, etc.) and finding an item is done in constant time.


RE: .Set - Unable to understand the statement - deanhystad - Aug-02-2020

I disagree. A set is very much like a list. It is a container class that holds multiple values. You can iterate through a set. Set is mutable like a list. You can add and remove things from a set. You can test if a set contains a value, like a list. If you were to list the similarities and the differences the two are more similar than not.

In this particular example I think set is mostly being used to eliminate duplicats in vocab. Maybe some of the set operations will be used in the application, but I doubt it.