2013-04-15 47 views

回答

6
>>> text = "i c u" 
>>> letter = "u" 
>>> any(letter in word and len(word) > 1 for word in text.split()) 
False 
>>> text = "umbrella" 
>>> any(letter in word and len(word) > 1 for word in text.split()) 
True 

你可以,如果你的情況下根據變化letter in wordletter.lower() in word.lower()功能敏感或nt。

+0

這不適用於單詞「我」。 –

+0

@limelights只要它是一個單詞的一部分,而不是整個事物,因爲這些例子暗示 – jamylak

+0

它不會爲小寫字母'i'起作用,或者對於這個事實任何1個字母的單詞,即使只有很少的字。 –

0

假設你的意思是「一個字」是「對任何一方認爲是一個‘單詞字符’至少一個字符」,這會工作:

import re 
def letter_in_a_word(letter, words): 
    return bool(re.search(ur'\w{0}|{0}\w'.format(letter), words)) 

letter_in_a_word('u', 'i c u') # False 
letter_in_a_word('u', 'umbrella') # True 
letter_in_a_word('u', 'jump') # True 
0
>>> word = 'i c u' 
>>> letter = 'u' 
>>> letter in word.split(' ') 
True 
>>> word = 'umbrella' 
>>> letter in word.split(' ') 
False