Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python how to
#5
(Oct-08-2018, 11:10 AM)DeaD_EyE Wrote:
results = collections.defaultdict(int)
Read the docs about defaultdict and test it.

You can do thinks like:

results['key_which_does_not_exist'] = 1 #or for counting
results['key_which_does_not_exist'] += 1 # if the key does not exist, it's 0

I think that collections.Counter will be a better choice. One of the advantages of Counter is that you may apply arithmetic operations between objects of that type
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Messages In This Thread
python how to - by mmmmm - Oct-08-2018, 01:11 AM
RE: python how to - by ichabod801 - Oct-08-2018, 01:59 AM
RE: python how to - by DeaD_EyE - Oct-08-2018, 11:10 AM
RE: python how to - by volcano63 - Oct-08-2018, 12:11 PM
RE: python how to - by zhruser - Oct-08-2018, 12:08 PM
RE: python how to - by buran - Oct-08-2018, 12:26 PM

Forum Jump:

User Panel Messages

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