Task with Dictionary

1073
12
05-05-2017 01:11 AM
JohnMcoy
New Contributor III

Hi, 

Could you explain me, how to change field value with dictionaries? 

for example : 

dict = {"A" : "A-150",

            "B" : "B-200",

            "C" : "C-250"}

And i want change A with A-150, B with B-200

Thanks ! 

0 Kudos
12 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Are you trying to retrieve the value? 

dict = {"A" : "A-150",
            "B" : "B-200",
            "C" : "C-250"}

dict['A']  # get its value
'A-150'
0 Kudos
JohnMcoy
New Contributor III

I know this, but what statement should be, if I have 2000 Fields in Values Column ? Every field in this column has  A, B or C value. How to change values with dict in all 2000 fields ? 

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor
def get_val(val):
    """ get a dictionary value"""
    dict = {"A" : "A-150", "B" : "B-200", "C" : "C-250"}
    return dict[val]

# python parser
# field calculator code
# field calculator expression for a text field

get_val(!YourFieldNameHere!)
JoshuaBixby
MVP Esteemed Contributor

2000 fields?  Do you mean 2000 rows?  Your statement "2000 Fields in Values Column" is confusing since field and column describes the same aspect of a table.

0 Kudos
JohnMcoy
New Contributor III

Sorry, yeah I mean rows ... So like I said, I have 2000 rows and ~ 25 columns.  One column is Values. So what expression would be the best ? Values column should have A-150, B-200, C-250 values. Not A,B,C. And I dont know solution for that... 

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

the field calculator expression I posted will work unless your table is different... post a snapshot of what you are working with so people can better understand

NeilAyres
MVP Frequent Contributor

Or set it to anything.

dict["A"] = "Anything you want it to be"
0 Kudos
JohnMcoy
New Contributor III

Any ideas ? 

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

well it is clear now that you want to do this in a script, I thought you were wanting to do this in the field calculator.  That is the part that wasn't clear.

You need to use the function (ie def ) with your dictionary to make the change within your tabletocsv function but I am not sure what it is doing there.  Does this work without the conversion or is it an attempt to piece a script together?

0 Kudos