我有關於乘法嵌套語句的一般問題。對於「複雜嵌套」(> 3/4層),什麼是更好的方法,尤其是迭代AND使用if語句時更是如此?for循環中嵌套的if語句
我有很多文件,其中一些在子目錄中,另一些在根目錄中。有許多目錄我想從中提取數據集並追加到目標數據集(主數據集)。
for special_directory in directorylist:
for dataset in special_directory:
if dataset in list_of_wanted:
some_code
if it_already_exists:
for feature_class in dataset:
if feature_class in list_of_wanted:
,然後我真正進入碼處理的肉。坦率地說,我想不出一種方法來避免這些嵌套的條件語句和循環語句。有什麼我失蹤?我應該使用「while」而不是「for」嗎?
我的實際特定代碼有效。它不會很快移動。它遍歷27個數據庫,將每個數據庫的內容追加到新的目標數據庫。我的python已經運行了36個小時,並且通過了4/27。提示?
我在GIS堆疊交換張貼了這個,但我的問題實在是太一般爲它屬於那裏:question and more specific code
任何提示嗎?這方面的最佳做法是什麼?這已經是代碼的一個子集。這會從其他腳本生成的列表中的地理數據庫中查找它們中的數據集和要素類。第三個腳本查找存儲在地理數據庫中的要素類(即不在數據集內)。
ds_wanted = ["Hydrography"]
fc_wanted = ["NHDArea","NHDFlowline","NHDLine","NHDWaterbody"]
for item in gdblist:
env.workspace = item
for dsC in arcpy.ListDatasets():
if dsC in ds_wanted:
secondFD = os.path.join(gdb,dsC)
if arcpy.Exists(secondFD):
print (secondFD + " exists, not copying".format(dsC))
for fcC in arcpy.ListFeatureClasses(feature_dataset=dsC):
if fcC in fc_wanted:
secondFC2 = os.path.join(gdb,dsC, fcC)
if arcpy.Exists(secondFC2):
targetd2 = os.path.join(gdb,dsC,fcC)
# Create FieldMappings object and load the target dataset
#
print("Now begin field mapping!")
print("from {} to {}").format(item, gdb)
print("The target is " + targetd2)
fieldmappings = arcpy.FieldMappings()
fieldmappings.addTable(targetd2)
# Loop through each field in the input dataset
#
inputfields = [field.name for field in arcpy.ListFields(fcC) if not field.required]
for inputfield in inputfields:
# Iterate through each FieldMap in the FieldMappings
for i in range(fieldmappings.fieldCount):
fieldmap = fieldmappings.getFieldMap(i)
# If the field name from the target dataset matches to a validated input field name
if fieldmap.getInputFieldName(0) == inputfield.replace(" ", "_"):
# Add the input field to the FieldMap and replace the old FieldMap with the new
fieldmap.addInputField(fcC, inputfield)
fieldmappings.replaceFieldMap(i, fieldmap)
break
# Perform the Append
#
print("Appending stuff...")
arcpy.management.Append(fcC, targetd2, "NO_TEST", fieldmappings)
else:
arcpy.Copy_management(fcC, secondFC2)
print("Copied " +fcC+ "into " +gdb)
else:
pass
else:
arcpy.Copy_management(dsC,secondFD) # Copies feature class from first gdb to second gdb
print "Copied "+ dsC +" into " + gdb
else:
pass
print "{} does not need to be copied to DGDB".format(dsC)
print("Done with datasets and the feature classes within them.")
看來要真正在arcpy.management.Append 抓住我有一些經驗公平使用此功能,儘管這是一個比典型的表架構更大(更多的記錄,更多的字段),單追加需要12個小時以上。建立在我原來的問題上,這可能是因爲它是如此深嵌套?或者情況並非如此,數據只需要時間來處理?
你爲什麼不把它分解成函數? –
我很抱歉你在這裏指出,也許你應該已經[重定向到代碼評論](http://meta.stackoverflow.com/questions/253975/be-careful-when-recommending-code-review-to因爲你在原始文章中有完整的工作代碼。 – iled
對於我來說,看起來並不值得麻煩,因爲考慮到這個任務有多大,我會試着將它分解爲並行處理。 –