Solved

How to calculate distinct values within a list using Python?


Badge

Can anyone show me how I can calculate the number of unique values within the list of each record using Python?

For example, what is the count of "Primary" or "Repair"?

I know that I can do this easily by exploding the list and go from there. But since I have over 1mil lists, I think the best way to do it is to take advantage of Python to improve performance. Thanks in advance!

icon

Best answer by david_r 24 June 2019, 21:17

View original

2 replies

Userlevel 4

Have you looked at the ListHistogrammer? Would that get you what you need?

Badge

Have you looked at the ListHistogrammer? Would that get you what you need?

I will take a look at that transformer.

But this solved my problem:

 

import fme

from collections import Counter

import fmeobjects

my_list = feature.getAttribute('Business_Points_List{}.ODP Category')

for x in range(0,20):

print Counter(my_list).keys()[x]

print Counter(my_list).values()[x]

feature.setAttribute(Counter(my_list).keys()[x], Counter(my_list).values()[x])

 

Reply