我正在尝试在列表中搜索关键字并显示与所述关键字匹配的所有列表项的位置。我还没有想到任何有用的东西,并想知道是否有人可以帮助我,这是我目前的代码
import os
directory = os.listdir()
print(directory)
pyFilename = '.py'
if pyFilename in directory:
print("working")
答案 0 :(得分:1)
结帐list comprehenssions和enumerate
import os
pyFilename = '.py'
directory = [(position, filename) for position, filename in enumerate(os.listdir())
if filename.endswith(pyFilename)]
print(directory)
答案 1 :(得分:0)
import os
directory = os.listdir()
print(directory)
pyFilename = '.py'
list_location = 0
for z in directory:
if pyFilename in z:
print("working: list location %s" % list_location)
list_location = list_location + 1