0
파일 이름 목록에서 파일 유형을 필터링하는 필터 함수를 만들었습니다.파이썬 : 필터에 사용 된 함수에 두 개의 값 전달
>>> l1
['180px-Cricketball.png', 'AgentVinod_450.jpg', 'Cricketball.bmp', 'Django-1.4', 'Django-1.4.tar.gz', 'Firefox Setup 11.0.exe', 'I-Will-Do-The-Talking-Tonight-(Muskurahat.Com).mp3', 'kahaani-.jpg', 'Never gonna leave this bed.mp3', 'Piya-Tu-Kaahe-Rootha-Re-(Muskurahat.Com).mp3', 'pygame-1.9.1release', 'pygame-1.9.1release.zip', 'pygame-1.9.2a0.win32-py2.7.msi', 'python-2.7.2.msi', 'python-3.1.2.msi', 'Resume.doc', 'selenium-2.20.0', 'selenium-2.20.0.tar.gz', 'sqlite-shell-win32-x86-3071100.zip', 'wxdesign_220a.exe', 'YTDSetup.exe']
>>> def myfilt(subject):
if re.search('.jpg',subject):
return True
>>> filter(myfilt,l1)
['AgentVinod_450.jpg', 'kahaani-.jpg']
잘 작동합니다.
이제 더 유연하게 만들고 싶다고 가정 해 보겠습니다. 함수에 파일 형식을 전달하고 싶습니다. 그래서 지금은 필터 기능을 통해 파일 형식을 통과 어떻게 기능
>>> def myfilt(subject,filetype):
if re.search(filetype,subject):
return True
를 다시 작성?
내가 시도 : 이>>> filter(myfilt(l1,filetype),l1)
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
filter(myfilt(l1,filetype),l1)
File "<pyshell#28>", line 2, in myfilt
if re.search(filetype,subject):
File "C:\Python27\lib\re.py", line 142, in search
return _compile(pattern, flags).search(string)
TypeError: expected string or buffer
의 속삭임 작동합니다. 어떤 아이디어?
대부분의 사람들은'filter'를 싫어하고 거의 모든 상황에 대해 목록 내포를 권장합니다. – jamylak
이것을 위해're'가 필요 없습니다. ['os.path.splitext()'] (http://docs.python.org/library/os.path.html#os.path.splitext)를 사용하십시오. –