count occurrences of entry in list

mdlferguson's Avatar


25 Mar, 2013 02:08 PM

I was having an issue with counting the occurrences of an item in a list. eg
list= 'cat,cat,dog,rabbit,dog,cat' and i want result 'cat 3,dog 2, rabbit 1.
This is a very simplified example. What i actually wanted to do is:
I have a list of points, they are connected by a number of lines. i want the point to be larger based on the number of connections. each line from a point has 2 points of its own, so if there are 8 lines leading from a point there should be 9 points at that location.(1 for initial point and 1 for each line)

Any suggestions for a simpler way to achieve this would be welcome, but i used a small python script.
# from collections import defaultdict
def occurDict(listx):
counts = defaultdict(int) for item in listx: counts[item] += 1 return dict(counts) # to use this you need to create a node with 'listx' as an input and also load "" from the same folder. is part of python, you should easily find it online.
I have been away from python for a while and was never brilliant at it.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:


Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

Recent Discussions

29 May, 2024 05:47 AM
29 May, 2024 05:15 AM
29 May, 2024 05:12 AM
28 May, 2024 07:37 PM
25 May, 2024 11:52 AM


24 May, 2024 07:39 AM
16 May, 2024 02:11 AM
14 May, 2024 01:29 AM
12 May, 2024 07:46 PM
05 May, 2024 04:38 PM
03 May, 2024 06:50 AM