簡體   English   中英

Python:我需要打印才能返回唯一的句子

[英]Python: I need print to return only unique sentence

我正在嘗試獲取一個包含10個句子(所有單詞)的txt文件,並將其作為命令行參數傳遞給python腳本。 我想打印包含dic列出的單詞的句子。 下面的腳本可以找到匹配的句子,但是它會與找到匹配的單詞一樣多次打印句子。

有另一種方法可以用來執行此操作嗎? 另外,我不希望輸出用行(\\ n)分隔

import sys

dic=["april","aprils","ask","aug","augee","august","bid","bonds","brent","buy","call","callroll","calls","chance","checking","close","collar","condor","cover"]

f=open(sys.argv[1])

for i in range(0,10):
line=f.readline()    
words=line.split()
if len(words) > 3:
    for j in words:
        if j in dic:
            print(line)

輸出:

eighty two is what i am bidding on the brent

eighty two is what i am bidding on the brent

eighty two is what i am bidding on the brent

call on sixty five to sixty seventy

call on sixty five to sixty seventy

call on sixty five to sixty seventy

call on sixty five to sixty seventy

call on sixty five to sixty seventy

no nothing is going on double

i am bidding on the option for eighty five

i am bidding on the option for eighty five

recross sell seller selling sept

recross sell seller selling sept

recross sell seller selling sept

recross sell seller selling sept

recross sell seller selling sept

blah blah blah blah close

要求的輸出:

eighty two is what i am bidding on the brent
call on sixty five to sixty seventy
no nothing is going on double
i am bidding on the option for eighty five
recross sell seller selling sept
blah blah blah blah close
  1. 抑制輸出中的重復行

    print(line)語句之后添加一個break ,因此字典單詞上的for循環被中斷

  2. 禁止換行

    多余的換行符由f.readline()引起,因為它將在返回的字符串的末尾包含\\n 您可以使用line.strip()刪除它,但是最好for line in f語法中使用for line in f

這是代碼:

for line in f:    
    words=line.split()
    if len(words) > 3:
        for j in words:
            if j in dic:
                print(line)
                break

我建議為您的單詞詞典創建一set ,並為文件的每一行包含第二set單詞。 然后,您可以使用&比較集合以獲取它們的交集,或兩者的共同詞。 這比遍歷列表以查找類似單詞更有效。

import sys

dic=set(["april","aprils","ask","aug","augee","august","bid","bonds","brent","buy","call","callroll","calls","chance","checking","close","collar","condor","cover"])

filename = sys.argv[1]

with open(filename) as f:
    for line in f:
        s = set(line.split())
        if s & dic:
            print(line.strip())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM