Python Forum
.Set - Unable to understand the statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
.Set - Unable to understand the statement
#1
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
Reply
#2
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.
Reply
#3
(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.
Reply
#4
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.
Reply
#5
(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.
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to understand the meaning of the line of code. jahuja73 0 314 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  Unable to understand how given code takes a fixed input value, without inputting. jahuja73 4 2,713 Jan-28-2021, 05:22 PM
Last Post: snippsat
  Unable to understand a statement in an existing code ateestructural 1 2,238 Aug-01-2020, 09:38 PM
Last Post: deanhystad
  I don't understand this return statement 357mag 4 2,776 Jul-10-2019, 07:02 PM
Last Post: perfringo
  if conditional not firing - unable to understand why simms7400 7 3,384 Oct-20-2018, 12:13 PM
Last Post: stullis
  I don't understand the parameters in a statement that uses the sh library RedSkeleton007 1 2,646 Apr-11-2018, 07:55 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020