8 줄의 코드를 단지 2 줄로 변환 할 수있었습니다.목록 이해 최적화
최초의 지능형리스트 나에게 폴더를 얻고, 두 번째는 나에게 특정 필터의 파일을 가져옵니다
hideTheseFolders=[".thumb",".mayaSwatches","RECYCLER","$AVG"]
fileFilters=["ma","jpg","png","mb",'iff','tga','tif']
newLst=[]
import os
locationTxt="E:\box\scripts"
[newLst.append(each) for each in os.listdir(locationTxt) if os.path.isdir(os.path.join(locationTxt,each)) and each not in hideTheseFolders]
[newLst.append(os.path.basename(os.path.join(locationTxt,each))) for nfile in fileFilters for each in os.listdir(locationTxt) if each.endswith(nfile)]
이제 위의 코드에서 마지막 두 줄이
locationTxt
에서 같은 디렉토리 내에서 찾고, 거기에 의미 아마도 마지막 두 줄을 병합 할 수있는 방법 일 것입니다. 어떤 제안?
왜 목록 이해력 내에서 목록에 추가하고 있습니까? – Marcin
두 줄을 사용하는 것이 좋습니다. 이미 각기 다른 것들이 많이 있습니다 (어쨌든 표준 선 길이에 맞지도 않습니다). 그리고 각각 다른 것이 있습니다. 아마 당신은'newLst.extend()'를 각 comprehension의 결과로 할 수 있습니다. – DNA
왜 이것을 두 줄로 병합 하시겠습니까? –