Python如何摆脱列表中的小写元素

时间:2014-11-05 21:46:55

标签: python list lowercase

这是我目前的代码:

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"]]

2 个答案:

答案 0 :(得分:0)

给出一个带有大小写混合的单词列表:

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

您可以通过理解排除小写的那些:

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

答案 1 :(得分: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始终以'。'

结尾