Professional Documents
Culture Documents
and dictionaries
Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python
Learning objectives
Distinguish between lists and dictionaries
Create and modify a Python list
Initialize a Python dictionary
Set keys and values for a dictionary
Manage data using lists and dictionaries
Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 3-2
What you should already know about lists
Python lists are:
Ordered collections
Stored in variables
Indexed
Accessed by position
fc = fcList[0:-1]
Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 3-3
What you can do with lists
List functions work for numbers and strings
sort()– Sorts in sequence from low to high
Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 3-4
What you can do with lists (cont’d)
Change the order in the list
sort(None, None, 1)– Sorts the list in reverse
Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 3-5
Adding items to a list
append() stateList = ["Texas"]
stateList.append("Utah")
Adds a single item print stateList
to end of the list
>>> ['Texas', 'Utah']
extend()
stateList.extend(["Iowa", "Ohio"])
Adds multiple items print stateList
to end of the list
>>> ['Texas', 'Utah', 'Iowa', 'Ohio']
A Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 3-6
Finding number of items in a list
Determine total number of items
len()– Returns the length
numItems = len(stateList)
print numItems
>>> 5
>>> 2
Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 3-7
Python dictionaries
Dictionaries are: To initialize a
dictionary:
Unordered collections
newD = {}
Stored in variables as key : value pairs
Access by key
print newD ["x"]
>>> 1279359
A Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 3-8
What you can do with dictionaries
Determine number of items numItems = len(myDct)
print numItems
len()– Returns the length
>>> 4
Check for existence of a key
has_key()
# Key : Value
myDct["Brazil"] = "Brasilia"
myDct["Bolivia"] = "La Paz"
myDct["Guyana"] = "Georgetown"
myDct["Suriname"] = "Paramaribo"
A Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 3-9
Accessing a dictionary sequentially
Dictionary keys are not stored in a particular order
Need to know the key to find a value
Can use a Python list to access keys
Do not rely
on a specific
sequence in
the list of keys!
Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 3-10
Code example: Access a dictionary
Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 3-11
Exercise goals
Create and update a list
Count the items in the list
Assign keys and values to a dictionary
Determine whether a key already exists
Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 3-12
Lesson review
Compare and contrast lists and dictionaries
List Dictionary
A Copyright © 2006–2008 ESRI. All rights reserved. Writing Advanced Geoprocessing Scripts Using Python 3-13