count occurrences of entry in list

mdlferguson's Avatar

mdlferguson

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 "collections.py" from the same folder.
collections.py 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

Generic

? 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

01 Oct, 2024 07:59 AM
30 Sep, 2024 11:37 PM
30 Sep, 2024 11:11 AM
30 Sep, 2024 02:37 AM
28 Sep, 2024 10:33 AM

 

26 Sep, 2024 06:41 AM
24 Sep, 2024 12:32 AM
24 Sep, 2024 12:27 AM
13 Sep, 2024 12:07 AM
12 Sep, 2024 11:54 PM
07 Sep, 2024 05:16 AM