2017-07-23 30 views
0

我有一個像下面這樣的段落列表,我想提取在同一段落中包含兩個特定單詞的段落。python閱讀文本文件並找到包含兩個特定單詞的段落

[" Electronically monitored security systems are tailored to our customers' specific needs and involve the installation and use on a\ncustomer's premises of devices designed for intrusion detection and access control, as well as reaction to various occurrences or\nconditions, such as movement, fire, smoke, flooding, environmental conditions, industrial processes and other hazards. These\ndetection devices are connected to microprocessor-based control panels, which communicate to a monitoring center (located remotely\nfrom the customer's premises) where alarm and supervisory signals are received and recorded. In most systems, control panels can\nidentify the nature of the alarm and the areas within a building where the sensor was activated. Depending upon the type of service\nfor which the subscriber has contracted, monitoring center personnel respond to alarms by relaying appropriate information to the\nlocal fire or police departments, notifying the customer or taking other appropriate action, such as dispatching employees to the\ncustomer's premises. In some instances, the customer may monitor the system at its own premises or the system may be connected to\nlocal fire or police departments.", " Whether systems are monitored by the customer at its premises or connected to one of our monitoring centers, we usually provide\nsupport and maintenance through service contracts. Systems installed at customers' premises may be owned by us or by our customers.",' We market our electronic security services to commercial and residential customers through both a direct sales force and an\nauthorized dealer network. A separate national accounts sales force services large commercial customers. We also utilize advertising\nand direct mail to market our services.',' We provide residential electronic security services primarily in North America, Europe and South Africa, with a growing presence\nin the Asia-Pacific region. Our commercial customers include financial institutions, industrial and commercial businesses, federal,\nstate and local governments, defense installations, and health care and educational facilities. Our customers are often prompted to\npurchase security systems by their insurance carriers, which may offer lower insurance premium rates if a security system is\ninstalled or require that a system be installed as a condition to coverage. It has been our experience that the majority of\ncommercial and residential monitoring contracts are renewed after their initial terms. In general, relocations account for the\nlargest number of residential discontinuances while business closures comprise the largest single factor impacting commercial\ncontract attrition.', " We are the leader in anti-theft systems. The majority of the world's leading retailers use our systems to protect against\nshoplifting and employee theft. We manufacture these SENSORMATIC electronic article surveillance systems and generally sell them\nthrough our direct sales force in North and South America, Europe, Australia, Asia and South Africa. A growing trend in the loss\nprotection"] 

我寫了下面的代碼,但它沒有給我想要的。它給出了包含客戶或系統的段落。

for pg in paragraphs: 
    pfls = [] 
    pg= pg.replace("\n", ' ') 
    if 'customers' in pg and 'system' in pg: 
    print('1',pg) 

我的代碼有什麼問題?

回答

1

在python中,縮進非常重要。縮進描述了代碼塊。 在您的示例中,最後三行沒有正確縮進,並且僅在循環結束時執行,並且僅測試最後一段。

for pg in paragraphs: 
    pfls = [] 

    pg= pg.replace("\n", ' ') 
    if 'customers' in pg and 'system ' in pg: 
     print('1',pg) 

將實現你想要的。

編輯:這樣做的一個骯髒的方式是通過在測試這個詞後加空格,像這樣的隔離字system

for pg in paragraphs: 
    pfls = [] 

    pg= pg.replace("\n", ' ') 
    if 'customers' in pg and 'system ' in pg: 
     print('1',pg) 

實現這一目標的一個更好的方法,是通過使用常規表達:

import re 

systemPattern = re.compile(r"\bsystem\b") 
customersPattern = re.compile(r"\bcustomers\b") 

for pg in paragraphs: 
    pfls = [] 

    pg= pg.replace("\n", ' ') 
    if systemPattern.search(pg) and customersPattern.search(pg): 
     print('1',pg) 
+0

謝謝!我意識到這是我在原始問題上的編輯問題,但我會離開這個。非常感謝。 – TTaa

相關問題