-1

I am just entering to the python world, hence the level of my question.

Can anyone tell me what Python function should I use to compare values stored in one column in an attribute table with values stored within a script's dictionary{}.

Format of the values in table is as follow: "2000, 5000", next row "3000, 6000"... etc. - this is a string. Format of my dictionary: dictio = {"NW": ["1000, 5000"], "NX": ["2000, 5000"], ...}

How to take value from the table compare with dictionary and get dictionary Key ('NX') as an output.

Many thanks

PS. I am working on ArcGis 10.0

To clarify, I will try to give more details that I have been asked for in comments below: I am trying to create a simplified(!) tool that converts XY coordinates to BrithishNatioanlGrid. The dictionary contains fixed number of items, where keys and values are always the same. Format of the table will be always the same but number of rows in a table may be different each time as they represent polygons in the table (attribute table of polygon shapefile). Field name in the table is "XY". Once I got a list of keys I am doing to concatenate it with other text value, stored in gis table.

Extract from dictionary: Dictio = {"NA": ["0, 9000"], "NB": ["1000, 9000"], "NC": ["2000, 9000"], "ND": ["3000, 9000"],"NF": ["0, 8000"], "NG": ["1000, 8000"], "NH": ["2000, 8000"], "NJ": ["3000, 8000"], "NK": ["4000, 8000"],"NL": ["0, 7000"], "NM": ["1000, 7000"], "NN": ["2000, 7000"], "NO": ["3000, 7000"], "NR": ["1000, 6000"], "NS": ["2000, 6000"], "NT": ["3000, 6000"], "NU": ["4000, 6000"], "NW": ["1000, 5000"]}

Extract from attribute table:

enter image description here

What I want to do is: take value from ‘XY’ ->check against dictionary, and find the same value -> take key of the value -> stick it in front of “coord” value -> save it in “NGR” column (in brackets you can see desired output).

I hope it is a little bit clearer now.

  • Not a direct answer to your question, I understand that, but one option that might be slightly easier to work with and may or may not even be slightly more efficient (depending on your database engine, indexes, etc...) would be to re-create the dictionary as a table in your database and then just perform a table join. Make the new table ObjectID, Key(NW,NX...), Value and then just use a table join to get the key value for each row, if available... just a thought to achieve the same results if that's an option – John Feb 21 '14 at 15:36
  • thanks for reply, you are right it is not a difficult task when using 'join' tool but I need to achieve the same using Python only as this script is going to be used by other users (i hope). My dictionary has been created using existing shapefile – ChrisL Feb 21 '14 at 15:48
  • In that case, you may want to look at something along the lines of setting up a search cursor to loop through each record in attribute table and for each record take that field's value and loop through the dictionary comparing field value against dictionary entry value. When they match, you take and record, print, whatever... the key from that dictionary entry. You could even record the key along with the OID for that record and then run an update cursor and update each OID with the matching dictionary key if needed... again just a thought. – John Feb 21 '14 at 15:59
  • I am a little unclear on the overall goal here, and what the data is meant to represent. Is the dictionary hard-coded? How many rows are in the table? What field in the table contains the values you described (e.g. "2000, 5000")? What are you going to do with the dictionary key (e.g. 'NX') once you have it? There is also a chance that this may be closed as "general software development", but I get the feeling that you are more looking for help with the arcpy side than the general Python side, is that right? If so, you need to clarify what makes this a GIS-specific question. – blah238 Feb 21 '14 at 20:04
  • I should be a little bit more precise,I am trying to create a simplified(!)tool that converts XY coordinates to BrithishNatioanlGrid. Answering your questions: 1) Yes,dictionary contains fixed number of items,where keys and values are always the same. 2) number of rows in a table will change each time. 3) field name is "XY". 4) I am doing to concatenate it with other text value, stored in gis table. 5) I am not sure, it is mix of arcgis table with python dictionary, so I would say 50/50. I hope it is a little bit clearer now. thanks for any help. – ChrisL Feb 23 '14 at 11:22
1

It seems like you are trying to do a reverse look up on the dictionry, which I dont think is possible. So you have two options:

  1. rebuild your dictionary: dictio = {"1000, 5000":"NW", "2000, 5000":"NX", ...}

  2. loop through your dictionary to test the values:

edit: OK i think i have a better idea of what you are trying to do. this will create a new dictionary (result_dictionary) with the xy as the key and the old key+cord as the value

from arcpy import SearchCursor
cur = SearchCursor("your shp/feature with attributes to test here")
result_dictionary={}
dictio={"NW": "1000, 5000", "NX": "2000, 5000"}
for row in cur:#loops through your attributes
    xy, cord=str(row.XY), str(row.cord)
    for i in dictio.keys():#loop through the dictionary keys
        for j in dictio[i]:#for each key, loop through the values (as there may be more than 1
            if attribute_value ==j:#test if they match
                result = i+cord
                result_dictionary[xy]=result
                print xy+" = "+result #what ever process you want to do when there is a match here
  • @ Hotpepper - rebuilding dictionary generate "unhashable type: 'list'" error. Your sample code does work indeed, but instead of “print ‘MATCH’” I need to obtain the dictionary keys for each pair. Any ideas how to do that? – ChrisL Feb 25 '14 at 11:45
  • the key value is tied to the j variable. So you can use that to either print, write out to text file or update a value in your attributes, etc. – Hotpepper Feb 25 '14 at 20:32
  • A list object is not a valid key type for a dictionary (source: wiki.python.org/moin/DictionaryKeys). What if you just remove the brackets? They don't seem to serve any purpose. – blah238 Feb 26 '14 at 20:59
0

Just to update... I bypassed the dictionary issue by using amateur, long way but it works well. I have created if/elif block that looks:

if  XY == "0, 9000":
  NGR = "NA"
elif  XY == "1000, 9000":
  NGR = "NB" 

etc

And now my NGR calculator is working as it should.

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.