我想寫,檢查信件是否是一個字的一部分,例如蟒蛇檢查是否字的文字部分字符串
"u" part of word in "i c u" = false
"u" part of word in "umbrella" = true
我想寫,檢查信件是否是一個字的一部分,例如蟒蛇檢查是否字的文字部分字符串
"u" part of word in "i c u" = false
"u" part of word in "umbrella" = true
>>> 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 word
到letter.lower() in word.lower()
功能敏感或nt。
這不適用於單詞「我」。 –
@limelights只要它是一個單詞的一部分,而不是整個事物,因爲這些例子暗示 – jamylak
它不會爲小寫字母'i'起作用,或者對於這個事實任何1個字母的單詞,即使只有很少的字。 –
假設你的意思是「一個字」是「對任何一方認爲是一個‘單詞字符’至少一個字符」,這會工作:
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
>>> word = 'i c u'
>>> letter = 'u'
>>> letter in word.split(' ')
True
>>> word = 'umbrella'
>>> letter in word.split(' ')
False
定義「字」。是「一個」字嗎? – georg