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
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 326 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Trying to understand strings and lists of strings Konstantin23 2 752 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,506 May-01-2023, 09:06 PM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,748 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Counting number of words and organize for the bigger frequencies to the small ones. valeriorsneto 1 1,658 Feb-05-2021, 03:49 PM
Last Post: perfringo
  Counting Number of Element in a smart way quest 2 1,991 Nov-09-2020, 10:24 PM
Last Post: quest
  Counting the values ​​in the dictionary Inkanus 7 3,593 Oct-26-2020, 01:28 PM
Last Post: Inkanus
  counting items in a list of number combinations Dixon 2 2,064 Feb-19-2020, 07:06 PM
Last Post: Dixon
  Pass Dictionary to Method is not possible Frank123456 1 2,344 Aug-19-2019, 10:18 AM
Last Post: buran
  Counting number of occurrences of a single digit in a list python_newbie09 12 5,457 Aug-12-2019, 01:31 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

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