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

22 Feb, 2024 05:51 AM
22 Feb, 2024 01:11 AM
21 Feb, 2024 05:27 PM
21 Feb, 2024 04:49 PM
16 Feb, 2024 05:24 PM

 

06 Feb, 2024 04:45 AM
19 Jan, 2024 09:50 AM
13 Jan, 2024 03:47 AM
05 Jan, 2024 05:10 PM
05 Jan, 2024 04:54 PM
04 Jan, 2024 11:49 PM