다중 중첩 구문과 관련된 일반적인 질문이 있습니다. "복잡한 네 스팅"(> 3/4 레이어)의 경우 특히 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:
그리고 난 정말 코드 처리의 고기를 얻습니다. 솔직하게, 나는 이러한 중첩 된 조건문과 반복문을 피하는 방법을 생각할 수 없다. 제가 누락 된 것이 있습니까? "for"대신 "while"을 사용해야합니까?
내 특정 코드가 작동합니다. 그것은 아주 빨리 움직이지 않습니다. 27 개의 데이터베이스를 반복하여 각각의 내용을 새 대상 데이터베이스에 추가합니다. 내 파이썬은 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 에 하나의 APPEND를 잡힐 것 같다 12 시간 이상 걸립니다. 내 원래의 질문을 토대로하기 위해서, 이것이 깊이 중첩되어 있기 때문일 수 있습니까? 아니면 그렇지 않은가? 그리고 데이터는 단순히 처리하는 데 시간이 필요합니까?
왜 함수로 분해하지 않습니까? –
당신이 여기서 지적했음을 유감스럽게 생각합니다. 코드 검토로 리디렉션되어야합니다. (http://meta.stackoverflow.com/questions/253975/be-careful-when-recommending-code-review-to 원래 게시물에 전체 코드와 작업 코드가 있었기 때문에 처음부터. – iled
나에게 귀찮은 가치는 보이지 않지만, 얼마나 큰 작업인지를 고려할 때, 병렬 처리를 위해 해체 할 생각입니다. –