Python -Suche in einer sortierten Liste, wenn der Schlüssel ein Teil einer Zeichenfolge mithilfe von Halbzeit ist
Posted: 31 Aug 2025, 12:42
Ich habe eine Liste von Pfadnamen (DirName+Dateiname), sortiert nach Dateinamen.
Code: Select all
import os, bisect
lst=[]
for (dp, dn, fn) in os.walk(rep):
for name in fn:
lst.append(os.path.join(dp, name)
# sort list by filenames
lst.sort(key=lambda p: os.path.basename(p))
f = "C001567.jpg"
i = bisect.bisect_left(lst, f) # ???
if i != len(lst) and lst[i] == f:
print(f"found : {lst[i]}")