2

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.

enter image description here

  • 1
    Could you provide a simplified example of what you are trying to accomplish? A screenshot from a spreadsheet will even be sufficient. – Aaron Oct 16 '16 at 21:22
  • Please always include code as text rather than pictures of code. There is an edit button beneath your question which will enable you to do that and a {} 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. – PolyGeo Oct 16 '16 at 21:28
  • 1
    What's in your list? I think that you will need to do a join at some stage to resolve your list to features or compile your list like [27,next,next] and use in operator like if (FID_1_1 in [27,28,50]): (for example) for values 27, 28 and 50; it depends how long your list is, and personal preference, as to what's the best approach. – Michael Stimson Oct 16 '16 at 21:57
4

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. – Michael Stimson Oct 17 '16 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 '16 at 18:35
2

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]
  • 1
    Can 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. – Michael Stimson Oct 17 '16 at 5:10
  • 1
    [0,1][ !FID_1_1! in (27,50,77,103)] will do as well – FelixIP Oct 17 '16 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.