4 回答
TA贡献1874条经验 获得超12个赞
您的函数可以简单如下:
def get_word_containing(string, char):
words = [word for word in string.split() if char in word]
return words
string = "I only want the $1000"
print(get_word_containing(string, "$"))
输出:
['$1000']
TA贡献1839条经验 获得超15个赞
我将稍微修改 @Biplob 函数来打印字符串:
def get_word_containing(myStr, char):
for x in myStr.split():
if char in x:
print(x)
mystring = "I only want the $1000"
get_word_containing(mystring, "$")
TA贡献1895条经验 获得超7个赞
import re
a = "I only want the $1000"
list = re.findall("[$]\w+", text)
print(list)
上面的代码将为您提供字符串中以 $ 开头的所有单词的数组
TA贡献1786条经验 获得超11个赞
import re
def get_word_containing(myStr, char):
list = re.findall("["+char+"]\w+", myStr)
return list;
mystring = "I only $200 want the $1000"
ouput = get_word_containing(mystring, "$")
print(ouput);
所以它会给我们 ['$200', '$1000']
添加回答
举报