53 — Sets: unique values
Guide 36 counted every word, repeats and all. A set keeps
only the unique values, so set(words) answers a different question: how many
different words are there?
Steps
Create a small text file,
story.txt, next to your program:
run it →the quick brown fox jumps over the lazy dog the fox and the dog are friends quick and lazy are fun words to sayRead it and split it (guide 36);
set(words)drops the repeats, solen(set(words))counts the different words:
run it →use file latest text = file_read("story.txt") words = text.split() show f"total words: {len(words)}" show f"different words: {len(set(words))}"The story has 24 words but only 15 different ones. A set has no order and no repeats, so the same word can never sit inside twice.
Sets have no positions, so
my_set[0]fails.sorted(...)(guide 39) converts one back to an ordered list, andinchecks membership:
run it →unique_words = set(["the", "dog", "the", "cat"]) for word in sorted(unique_words): show " " + word show f"dog present? {'dog' in unique_words}"It prints
cat,dog,the— alphabetical, each once — thenTrue.Strings become sets of characters too, and the space counts as a member:
run it →letters = set("hello world") show sorted(letters)It prints
[' ', 'd', 'e', 'h', 'l', 'o', 'r', 'w']— eight members.The full program reads the story, builds both sets, and reports them. Save
sets.nme:
run it →# sets.nme — sets keep only unique values. # Run: nme r sets # The file story.txt must exist in the same folder. use file latest text = file_read("story.txt") words = text.split() show f"total words in story.txt: {len(words)}" unique_words = set(words) show f"different words: {len(unique_words)}" show "" show "the different words, sorted:" show sorted(unique_words) show f"is 'fox' in the story? {'fox' in unique_words}" show f"is 'zebra' in the story? {'zebra' in unique_words}" show "" sentence = "hello world" letters = set(sentence) show f"sentence: {sentence}" show f"members inside set(sentence): {len(letters)}" show sorted(letters) show "" show "list vs set:" show f" list length (with repeats): {len(words)}" show f" set length (no repeats): {len(unique_words)}"Run
nme r setswithstory.txtin the folder:total words in story.txt: 24 different words: 15 the different words, sorted: ['and', 'are', 'brown', 'dog', 'fox', 'friends', 'fun', 'jumps', 'lazy', 'over', 'quick', 'say', 'the', 'to', 'words'] is 'fox' in the story? True is 'zebra' in the story? False sentence: hello world members inside set(sentence): 8 [' ', 'd', 'e', 'h', 'l', 'o', 'r', 'w'] list vs set: list length (with repeats): 24 set length (no repeats): 15The list holds every word; the set holds one copy of each. Membership checks use the same
ina dict uses for keys.Korean writes the same program with
파일 사용 최신,파일읽기, and말해; the full Korean program is in the Korean guide.
Try it yourself
Add a line to story.txt that reuses an old word, like the dog and the fox jump; the total word count grows, but the different-word count may not. Then
build a letter set from your own name with set("your name") and count it.
What you learned
set(words)keeps one copy of each value, with no order and no repeats.len(set(...))counts the different values, not all of them.sorted(a_set)converts the set back into an ordered list.'fox' in a_setasks whether a value is inside, like a dict's keys.set("hello world")works on a string, where the space is a member too.