So I am currently working on a tool that the user needs to apply a query in order to the run the tool for the queried data. I am trying to get around that by creating a blank dictionary in the script and then appending data to it. The data comes from a field in a feature class. I have never done this before but in theory it should work the same as appending to lists correct?
hubFeatures = r'hub_locations'
hubDict = {}
These are my working variables for this. The field that I want the data from to append to the dictionary is called 'NAME' in the hubFeatures FC. I was thinking of using cursors to achieve this. Unless there is an alternative approach. The idea is to then use the items in the dictionary to apply a query to the necessary layers and complete the functionality of the tool using a for loop. That part I can do. Need some assistance in setting up the dictionary.
Solved! Go to Solution.
If I'm understanding your request, I think this will get you what you need. I learned it's easy to enumerate a cursor for an index or key field. Here's the expanded code so you can see what's happening:
valueDict = {}
with arcpy.da.SearchCursor(sourceFC, ["NAME"]) as cursor:
for enum, row in enumerate(cursor):
valueDict[enum] = row[0]
This will build a dictionary like this:
{
0: "Some Name",
1: "Another Name",
2: "Name Example",
3: "And Another"
}
For an example of populating a dictionary, see Turbo Charging Data Manipulation with Python Cursors and Dictionaries (starting with example 1). You will see code like this:
# Use list comprehension to build a dictionary from a da SearchCursor
valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sourceFC, sourceFieldsList)}
You mention using the NAME field. If it is to be used as the key, what will you use as the value?
a 'dictionary comprehension' or just 'dict comprehension'
dictionary order is now preserved in python 3.6 (ie if you are using PRO)
I want to use the NAME field's values to be the value in the dictionary and a numeric value starting at 0 as the key if that is possible?
I am unfortunately still using ArcGIS for Desktop and thus do not have access to python 3.6.
Did you try Randy's suggestion? It should work without problem
I did. While it does create a dictionary and populates it with a value, the NAME field value becomes the key and the value portion is empty. I'm trying to make it where the key is a numeric integer and the NAME field value is the value in the dictionary.
hubFeatures = r'hub_locations'
fieldList = "NAME"
hubDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(hubFeatures, fieldList)}
using:
print hubDict
I get:
{u'Hub Name': ()}
My desired output:
{1: 'Hub Name'}
How do I get to that? Reason I want to get that to be the output is because I want to use this for applying queries before doing the core function of the tool, which is conducting a network analysis.
If I'm understanding your request, I think this will get you what you need. I learned it's easy to enumerate a cursor for an index or key field. Here's the expanded code so you can see what's happening:
valueDict = {}
with arcpy.da.SearchCursor(sourceFC, ["NAME"]) as cursor:
for enum, row in enumerate(cursor):
valueDict[enum] = row[0]
This will build a dictionary like this:
{
0: "Some Name",
1: "Another Name",
2: "Name Example",
3: "And Another"
}
Blake that seems to get it in the format I want it in.
Do you know why the u appears before the value from the NAME field?
{0: u'Hub Name'}
Unicode ...
Indeed.
The 'u' in front of the string values means the string has been represented as unicode. Letters before strings here are called "String Encoding declarations". Unicode is a way to represent more characters than normal ascii can manage.
python - What does the 'u' symbol mean in front of string values? - Stack Overflow