Select to view content in your preferred language

Alternative to concatenate field values

4086
14
05-08-2016 08:12 AM
PeterWilson
Frequent Contributor

I'm creating a single feature class that will represent environmental areas within my study area. The environmental feature class is made up multiple feature classes that was merged, then union-ed . Each of the feature classes that were merged can contain a field "TYPE_RIV", "TYPE_WET", "TYPE_CBA", "TYPE_PROTECT".

What I'm trying to achieve is a better way of populating a new field "TYPE" that will concatenate the TYPE field (i.e. "TYPE_RIV", "TYPE_WET" etc.) values automatically based on the input environmental feature classes as this can differ from study area to study area. In other words in some cases there will be "TYPE_RIV", "TYPE_WET" and other cases there will be "TYPE_RIV", "TYPE_WET", "TYPE_CBA" lastly "TYPE_RIV", "TYPE_WET", "TYPE_CBA", "TYPE_PROTECT"

Once I've merged and union-ed the input feature classes, I'd like to identify the "TYPE_*" fields and concatenate the field fields into the new "TYPE" fields as per below:

TYPE_Field_Populated.png

End Result Required:

type_codeblock.png

Current Code Block to Populate "TYPE" field

I can easily get the "TYPE_*" fields using s a list fields search, but how can an automatically populate the "TYPE" field concatenating the fields correctly based on the fields within the list as the if statements are hard coded and not sure how to get around the following.

0 Kudos
14 Replies
JoshuaBixby
MVP Esteemed Contributor

The str.join() method works well when concatenating like this, and one doesn't have to remove extra commas:

>>> flds = ["hi  ", 2, None, "", "  bye"]
>>> ",".join(str.strip(str(x)) for x in flds if x not in ["",None])
'hi,2,bye'
0 Kudos
PeterWilson
Frequent Contributor

Hi Joshua

The  "spaces" and "," are required between the strings i.e. "Rivers, Wetlands". The removal of the "spaces" and "," are those at the end of the concatenated string i.e. "Rivers, Wetlands,space"

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Just add a space after the comma with the join method:

>>> flds = ["hi  ", 2, None, "", "  bye"]  
>>> ", ".join(str.strip(str(x)) for x in flds if x not in ["",None])  
'hi, 2, bye'  
0 Kudos
DanPatterson_Retired
MVP Emeritus

Joshua, yes, join is useful as well.  I am just used to slicing since I work with arrays mostly for geometry and attributes.  You could leave the strip out if you wanted to retain blank spaces should {} be used

On a simpler case, the mini-formatting language offers some nice options

>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> b = ", ".join(str.strip(str(i)) for i in a)
>>> c = ("{}, "*len(a)).format(*a)[:-2]
>>> c1 = ("{!s:>03}, "*len(a)).format(*a)[:-2]
>>> c2 = ("{!r:>03}, "*len(a)).format(*a)[:-2]
>>> b
'0, 1, 2, 3, 4, 5, 6, 7, 8, 9'
>>> c
'0, 1, 2, 3, 4, 5, 6, 7, 8, 9'
>>> c1
'000, 001, 002, 003, 004, 005, 006, 007, 008, 009'

especially if you are replicating an input like a join string, the ("{...}"*len(...)) is  an alternate.

0 Kudos
WesMiller
Deactivated User

Use range and a field list see below.

field_list = ["field1","field2","field3"]
build = ""
for x in range(len(field_list)):
  build+=str(x)
print build
'012'
0 Kudos