2014-11-05 33 views
-1

這是我當前的代碼裏面:的Python如何擺脫小寫的元素列表

def poisci_pare(besedilo):   
    sents = besedilo.split('.') 
    noviseznam = [sent.split() for sent in sents if sent] 
    return noviseznam 

這將返回:

poisci_pare("You are cool Anna. Johnny and I.") 
>>>output: [["You", "are", "cool", "Anna"], ["Johnny", "and", "I"]] 

我怎麼會改變我的功能,因此它會刪除小寫單詞和只有大寫單詞的返回列表?比如我要做到這一點:

poisci_pare("You are cool Anna. Johnny and I.") 
>>>output: [["You","Anna"], ["Johnny", "I"]] 

回答

0

考慮大小寫混合的單詞列表:

words = ["You", "are", "cool", "Anna"] 

您可以排除小寫與理解的那些:

words = [word for word in words if not word.islower()] 
+0

這不起作用,因爲我必須在列表中有一個列表,我有[[「你」,「是」,「酷」,「安娜」]],我會如何繼續這樣做列出現有列表中的列表? – peroxy 2014-11-05 22:15:37

0

這裏我試過的東西:

s = "You are cool Anna. Johnny and I." 
[ re.findall('[A-Z][a-z]*',x) for x in s.split('.')[:-1] ] 

輸出:

[['You', 'Anna'], ['Johnny', 'I']] 

提供sentence總是以結束'。

+0

它有時有效,但是當我把它作爲一個字符串(這是在斯洛文尼亞,我必須用這些句子測試我的assingment)它不起作用,如果你試圖把s ='Na vratih sta se prva pojavila安娜在彼得,menda「slučajno」。 Peter se je sicervečvrtel okrog Nives。'它會返回:[[''Na','Ana','Peter','Peter','Nives']] 這是怎麼回事?句子中的「」是否會成爲問題? – peroxy 2014-11-05 22:30:01

+0

@NejcPisk Python 2或Python 3?字節字符串或Unicode字符串?根據這些,'č'也可能是問題。 – 2014-11-05 23:10:46