2 回答

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超3個(gè)贊
列表.txt:
Sports TV is the home of football videos.
Complex game to follow.
home of football
因此:
content = ['Sports', 'Nature', 'Football']
path = 'list.txt'
with open(path) as auto:
print([[x.lower() for x in content if x.lower() in line.lower()] for line in auto])
輸出:
[['sports', 'football'], [], ['football']]
由于:
第 1 行有sports和football
第 2 行沒(méi)有來(lái)自?xún)?nèi)容列表的匹配元素
第 3 行有 football

TA貢獻(xiàn)1807條經(jīng)驗(yàn) 獲得超9個(gè)贊
您目前正在打印整行
嘗試:
content = ['Sports', 'Nature', 'Football']
path = filename
with open(path) as auto:
for line in auto:
for x in content:
if x.lower() in line.lower():
print(x)
添加回答
舉報(bào)