I assume this is a basic Python coding question, but I have scoured the internet and spent several hours trying on my own. I have about 2000 cells in my FID_1_1 column that I need to select and code as "1" in another attribute column (exp100mWA). I have figured out how to do this with Python for 1 cell (27), but can't figure out how to do it for all of them. I have the list of numbers, but it doesn't like any way I try to code it, commas, spaces, ampersands, etc. Here's my field calculator box.
2 Answers
This will depend on how big your list of records is - if it's very long it may be unmanageable to list them out.
def myCalc(myfid, mylist):
if myfid in mylist:
return 1
else:
return 0
And the expression:
myCalc(!FID_1_1!, [27, 50, 77, 103])
Which will return a 1
in the exp100mWA
column if the value in FID_1_1
is in the list - in my example the list is 27, 50, 77, 103
, so if the value in FID_1_1
is any of those values, it will return 1
, otherwise 0
.
-
As a 'cherry on top' you could populate the list using a search cursor and call the list as a global in myCalc. As stated if it's a long list you wouldn't like to populate it manually - both tedious and possible typos. Note that python lists can be very long, up to the amount of addressable memory (4GiB as ArcGis is a 32bit process) minus the amount currently utilized. Oct 17, 2016 at 5:06
-
@Laura If this answer has worked for you please consider upvoting and marking as the answer. See What should I do when someone answers my question?– Midavalo ♦Oct 17, 2016 at 18:35
Even though @Midavalo's answer to this question is the most concise (clearer) way of doing this, an alternative to this would be using a one-liner, again by using Python parser (without populating the code block, straight into the expression):
{True:1,False:0}[!FID_1_1! in [27, 50, 77, 103]]
I should admit though, the one-liners can be confusing for learners and does not reflect the same structure as creating a function
and calling it with its arguments
(essentially what you are trying to do as stated in your questions and what @Midavalo suggests). The arguments to the def
that you want to use are there in an implied way.
This is the same as writing a code block like:
def some_def(sought_value,search_list):
what_I_want_to_return_after_truth_check = {True : 1, False : 0}
if_my_value_is_in_the_search_list = sought_value in search_list
return what_I_want_to_return_after_truth_check [if_my_value_is_in_the_search_list]
-
1Can we see this in context of def myCalc(myfid, mylist)? An explanation of how this expands would help too.. One liners can be very confusing for learners; even some more experienced persons (myself included) find some one line operations cryptic. Oct 17, 2016 at 5:10
-
1
{}
button that enables you to format any highlighted code nicely. That way your code is available to future searches and can be copy/pasted for testing.