Select to view content in your preferred language

Concatenate Distinct Values with Dissolve

2003
10
Jump to solution
10-17-2022 12:12 PM
NathanBaier1
New Contributor III

I am using the concatenate function in the dissolve tool but need the output results to be distinct. Where the concatenate would yield A, B, B, C, D I would want the result to remove the duplicated B. A, B, C. If you run this gp tool via python is there a way to add a distinct function to the code?

0 Kudos
1 Solution

Accepted Solutions
NathanBaier1
New Contributor III

here is the solution:

var field = $feature.CONCATENATE_Acquisition_Source
var spfield = Split(field,"; ")
var dist = distinct(spfield)
return Concatenate(dist, "; ")

View solution in original post

10 Replies
NathanBaier1
New Contributor III

Another option would be a field calculator after the gp tool that remove duplicates from the text string.

0 Kudos
DanPatterson
MVP Esteemed Contributor

using as many python functions as I can 😉

a = 'A, B, B, C, D'
z0 = ", ".join(sorted(list(set(a.replace(" ", ""))))[1:])
z0
'A, B, C, D'

you will have to replace `a` with !YourFieldName!  with the ! marks

If the string is formatted differently you can modify to suit


... sort of retired...
0 Kudos
NathanBaier1
New Contributor III

I am not sure how to apply this code. Through notebook or field calculator? Would this apply to each row in a table?

0 Kudos
NathanBaier1
New Contributor III

When I run this through notebook here is what it looks like:

NathanBaier1_0-1666042560173.png

Do I need to define the dataset prior to the field?

0 Kudos
DanPatterson
MVP Esteemed Contributor

It is a field calculator expression and when I mean replace `a` in the expression, then I mean substitute it with your field name


... sort of retired...
0 Kudos
NathanBaier1
New Contributor III

I have been trying that and I cannot get the format to work. Not sure if I have this formatted correct but when I make a = field I get an invalid syntax. 

NathanBaier1_0-1666100224608.png

 

0 Kudos
DanPatterson
MVP Esteemed Contributor
expression
func(!CONCAT....!)  # enter your field name... too long for me to type

code block

def func(a):
    return ", ".join(sorted(list(set(a.replace(" ", ""))))[1:])

... sort of retired...
NathanBaier1
New Contributor III

this expression ran but it did not produce the desired result. Instead of cleaning out it created a bunch of new coma delimited data. 

NathanBaier1_0-1666117502649.png

 

0 Kudos
NathanBaier1
New Contributor III

here is the solution:

var field = $feature.CONCATENATE_Acquisition_Source
var spfield = Split(field,"; ")
var dist = distinct(spfield)
return Concatenate(dist, "; ")