Python Forum
get method not counting number of strings in dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get method not counting number of strings in dictionary
#1
Hi,

I've only been learning to code for a couple of weeks now. This is my first post. If I've posted incorrectly please do let me know.

So, I'vebeen typing this code:

counts = dict()
names = {"x", "y", "z", "x", "a"}
for name in names:
    counts[name] = counts.get(name, 0) + 1
print(counts)
and when running it, I'm getting this output:
Output:
D:\Python\helloworld\venv\Scripts\python.exe D:/Python/helloworld/app.py {'a': 1, 'z': 1, 'x': 1, 'y': 1} Process finished with exit code 0
Why isn't the code correctly counting the number of values in the "names" variable?

Thank you
Reply
#2
You need to wrap you code in python tags, otherwise the indentation is lost.

You create a set named "names" that contains "x", "Y", "Z" and "a". You probably think "names" has two "x"'s because there are two "x"'s in the initializer, but one of the main things that differentiates a set from a list or tuple is it only holds one of each value.

Or did you think "names" was a dictionary because you used the curly brackets? Both sets and dictionaries use {}. The difference between a set and dictionary is the dictionary initializer contains key value pairs separated by ":".

So the answer to your question is "counts" shows only one count for each name because each name only appears once. Replace you curly brackets with square brackes [] to make names a list, and count["x"] will be 2.
Reply
#3
(Jun-13-2020, 11:03 PM)deanhystad Wrote: You need to wrap you code in python tags, otherwise the indentation is lost.

You create a set named "names" that contains "x", "Y", "Z" and "a". You probably think "names" has two "x"'s because there are two "x"'s in the initializer, but one of the main things that differentiates a set from a list or tuple is it only holds one of each value.

Or did you think "names" was a dictionary because you used the curly brackets? Both sets and dictionaries use {}. The difference between a set and dictionary is the dictionary initializer contains key value pairs separated by ":".

So the answer to your question is "counts" shows only one count for each name because each name only appears once. Replace you curly brackets with square brackes [] to make names a list, and count["x"] will be 2.

Thank you! Smile
I'm still becoming familiar with the vocabulary.. but yet replacing the {} with [] worked!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Counting number of words and organize for the bigger frequencies to the small ones. valeriorsneto 1 334 Feb-05-2021, 03:49 PM
Last Post: perfringo
  Counting Number of Element in a smart way quest 2 480 Nov-09-2020, 10:24 PM
Last Post: quest
  Counting the values ​​in the dictionary Inkanus 7 1,005 Oct-26-2020, 01:28 PM
Last Post: Inkanus
  counting items in a list of number combinations Dixon 2 742 Feb-19-2020, 07:06 PM
Last Post: Dixon
  Pass Dictionary to Method is not possible Frank123456 1 1,023 Aug-19-2019, 10:18 AM
Last Post: buran
  Counting number of occurrences of a single digit in a list python_newbie09 12 2,128 Aug-12-2019, 01:31 PM
Last Post: perfringo
  Finding multiple strings between the two same strings Slither 1 1,018 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  Counting the number of letters in a string alphabetically TreasureDragon 2 1,130 Mar-07-2019, 01:03 AM
Last Post: TreasureDragon
  Counting the number of files related to particular format ambush 3 1,276 Nov-05-2018, 08:58 AM
Last Post: buran
  Counting number of characters in a string Drone4four 1 1,748 Aug-16-2018, 02:33 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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