如何计算Python中字符串中给定子字符串的出现次数?
例如:
>>> 'foo bar foo'.numberOfOccurrences('foo')
2
Run Code Online (Sandbox Code Playgroud) 我有一个文本文件.我需要一个句子列表.
如何实施?有许多细微之处,例如在缩写中使用点.
我的旧正则表达式很糟糕.
re.compile('(\. |^|!|\?)([A-Z][^;?\.<>@\^&/\[\]]*(\.|!|\?) )',re.M)
Run Code Online (Sandbox Code Playgroud) 如何在Python中的字符串中找到多次出现的字符串?考虑一下:
>>> text = "Allowed Hello Hollow"
>>> text.find("ll")
1
>>>
Run Code Online (Sandbox Code Playgroud)
所以第一次出现的ll是1,如预期的那样.我如何找到它的下一个出现?
同样的问题对列表有效.考虑:
>>> x = ['ll', 'ok', 'll']
Run Code Online (Sandbox Code Playgroud)
如何查找所有ll索引?
我正在使用BeautifulSoup在特定页面上查找用户输入的字符串.例如,我想看看字符串'Python'是否位于页面上:http://python.org
当我使用:
find_string = soup.body.findAll(text='Python')
find_string返回[]
但是当我使用:
find_string = soup.body.findAll(text=re.compile('Python'), limit=1)
find_string [u'Python Jobs']按预期返回
这两个语句之间的区别是,当要搜索的单词有多个实例时,第二个语句会起作用
我刚开始使用IPython笔记本中的pandas并遇到以下问题:当DataFrame从CSV文件读取很小时,IPython Notebook会在一个漂亮的表视图中显示它.当DataFrame它很大时,这样的东西就是输出:
In [27]:
evaluation = readCSV("evaluation_MO_without_VNS_quality.csv").filter(["solver", "instance", "runtime", "objective"])
In [37]:
evaluation
Out[37]:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 333 entries, 0 to 332
Data columns:
solver 333 non-null values
instance 333 non-null values
runtime 333 non-null values
objective 333 non-null values
dtypes: int64(1), object(3)
Run Code Online (Sandbox Code Playgroud)
我希望看到数据框的一小部分作为表格,以确保它的格式正确.我有什么选择?
我试图找到"|"的所有出现 在一个字符串中.
def findSectionOffsets(text):
startingPos = 0
endPos = len(text)
for position in text.find("|",startingPos, endPos):
print position
endPos = position
Run Code Online (Sandbox Code Playgroud)
但是我收到一个错误:
for position in text.find("|",startingPos, endPos):
TypeError: 'int' object is not iterable
Run Code Online (Sandbox Code Playgroud) 我想从一个字符串中创建一个句子列表然后将它们打印出来.我不想用NLTK来做这件事.因此,它需要在句子末尾的句点分割,而不是在小数,缩写或名称的标题上,或者如果句子有.com这是尝试正则表达式不起作用.
import re
text = """\
Mr. Smith bought cheapsite.com for 1.5 million dollars, i.e. he paid a lot for it. Did he mind? Adam Jones Jr. thinks he didn't. In any case, this isn't true... Well, with a probability of .9 it isn't.
"""
sentences = re.split(r' *[\.\?!][\'"\)\]]* *', text)
for stuff in sentences:
print(stuff)
Run Code Online (Sandbox Code Playgroud)
示例输出的示例
Mr. Smith bought cheapsite.com for 1.5 million dollars, i.e. he paid a lot for it.
Did he mind?
Adam Jones Jr. thinks he …Run Code Online (Sandbox Code Playgroud) 我想替换字符串中第n个子串的出现.
必须有一些与我想做的事情相同的东西
mystring.replace("substring", 2nd)
实现这一目标的最简单,最恐怖的方法是什么?
为什么不重复:我不想使用正则表达式这种方法,我发现的类似问题的大多数答案只是正则表达式剥离或真正复杂的功能.我真的希望尽可能简单而不是正则表达式解决方案.
仅使用python正则表达式,如何查找和替换句子中第n个单词的出现?例如:
str = 'cat goose mouse horse pig cat cow'
new_str = re.sub(r'cat', r'Bull', str)
new_str = re.sub(r'cat', r'Bull', str, 1)
new_str = re.sub(r'cat', r'Bull', str, 2)
Run Code Online (Sandbox Code Playgroud)
我上面有一句话,"cat"这个词出现在句子中两次.我希望第二次出现的'猫'改为'公牛',留下第一个'猫'字.我的最后一句话看起来像是:"猫鹅鼠马猪公牛".在我上面的代码中,我试过3次不能得到我想要的东西.
我开始意识到python中lambda表达式的价值,特别是涉及函数式编程map,函数返回函数等等.但是,我也一直在函数中命名lambdas,因为:
当我遇到满足上述标准的情况时,我一直在编写一个名为lambda的表达式,以便干燥并缩小范围功能.例如,我正在编写一个在某些numpy数组上运行的函数,我需要对传递给函数的所有数组进行适度的繁琐索引(可以很容易地放在一行上).我编写了一个名为lambda的表达式来进行索引,而不是编写整个其他函数,或者在整个函数定义中多次复制/粘贴索引.
def fcn_operating_on_arrays(array0, array1):
indexer = lambda a0, a1, idx: a0[idx] + a1[idx]
# codecodecode
indexed = indexer(array0, array1, indices)
# codecodecode in which other arrays are created and require `indexer`
return the_answer
Run Code Online (Sandbox Code Playgroud)
这是滥用python的lambdas吗?我应该吮吸它并定义一个单独的功能吗?
可能值得链接功能内部功能.