一行代码,为您提供 text 中 pattern 出现(重叠)的次数(可以是字符串或列表) ),可能看起来像这样:
nbOccurences = sum(1 for i in xrange(len(text)-len(pattern)) if text[i:i+len(pattern)] == pattern)
def matchIt(yourString, yourPattern):
"""find the number of times yourPattern occurs in yourString"""
Are you allowed to use the following?
yourString.count(yourPattern)
In your case you could create myString as a real string of 10000 characters and the pattern also as a string and then count the occurence in a simple pythonic way.
EDIT
A one-liner that gives you the number of (overlapping) occurences of pattern in text (which can be either a string or a list), could look like this:
nbOccurences = sum(1 for i in xrange(len(text)-len(pattern)) if text[i:i+len(pattern)] == pattern)
def matchIt(needle, haystack):
"""
@param needle: string, text to seek
@param haystack: string, text to search in
Return number of times needle is found in haystack,
allowing overlapping instances.
Example: matchIt('abab','ababababab') -> 4
"""
lastSeenAt = -1
timesSeen = 0
while True:
nextSeen = haystack.find(needle, lastSeenAt+1)
if nextSeen==-1:
return timesSeen
else:
lastSeenAt = nextSeen
timesSeen += 1
import itertools
class FindableList(list):
def find(self, sub, start=None, end=None):
"""
@param sub: list, pattern to look for in self
@param start: int, first possible start-of-list
If not specified, start at first item
@param: end: int, last+1 possible start-of-list
If not specified, end such that entire self is searched
Returns;
Starting offset if a match is found, else -1
"""
if start is None or start < 0:
start = 0
# N.B. If end is allowed to be too high,
# zip() will silently truncate the list comparison
# and you will probably get extra spurious matches.
lastEnd = len(self) - len(sub) + 1
if end is None or end > lastEnd:
end = lastEnd
rng = xrange if xrange else range
iz = itertools.izip
isl = itertools.islice
for pos in rng(start, end):
if all(a==b for a,b in iz(sub, isl(self, pos, end))):
return pos
# no match found
return -1
# Generate a list
randIn = lambda x: int(x*random.random())
myString =[randIn(2) for i in range(10000)]
pattern = [1,0,0]
result = matchIt(pattern, myString)
print("The sample had {0} matches and its size was {1}.\n".format(result, len(myString)))
Ok - a standard(-ish) string search:
def matchIt(needle, haystack):
"""
@param needle: string, text to seek
@param haystack: string, text to search in
Return number of times needle is found in haystack,
allowing overlapping instances.
Example: matchIt('abab','ababababab') -> 4
"""
lastSeenAt = -1
timesSeen = 0
while True:
nextSeen = haystack.find(needle, lastSeenAt+1)
if nextSeen==-1:
return timesSeen
else:
lastSeenAt = nextSeen
timesSeen += 1
but you want to do this to a list of numbers? No problem; we just need to make a list class with a find() method, like so:
import itertools
class FindableList(list):
def find(self, sub, start=None, end=None):
"""
@param sub: list, pattern to look for in self
@param start: int, first possible start-of-list
If not specified, start at first item
@param: end: int, last+1 possible start-of-list
If not specified, end such that entire self is searched
Returns;
Starting offset if a match is found, else -1
"""
if start is None or start < 0:
start = 0
# N.B. If end is allowed to be too high,
# zip() will silently truncate the list comparison
# and you will probably get extra spurious matches.
lastEnd = len(self) - len(sub) + 1
if end is None or end > lastEnd:
end = lastEnd
rng = xrange if xrange else range
iz = itertools.izip
isl = itertools.islice
for pos in rng(start, end):
if all(a==b for a,b in iz(sub, isl(self, pos, end))):
return pos
# no match found
return -1
# Generate a list
randIn = lambda x: int(x*random.random())
myString =[randIn(2) for i in range(10000)]
pattern = [1,0,0]
result = matchIt(pattern, myString)
print("The sample had {0} matches and its size was {1}.\n".format(result, len(myString)))
def create_weighted(self, G):
g = cycle_graph(4)
e = g.edges()
source = [u for u,v in e]
dest = [v for u,v in e]
weight = [s+10 for s in source]
ex = zip(source, dest, weight)
G.add_weighted_edges_from(ex)
return G
Similar question but main focus on graph libraries here and similar question but in C#, maybe useful.
The files that are relevant to this question are ./networkx/generators/degree_seq.py (997 lines, about generating graps with given degree sequence) and ./networkx/algorithms/mixing.py (line 20, function degree_assortativity(G) about probability based graphs) and also note that its source code refer to 92 references, not sure whether you want to reinvent the wheel. For igraph, please, read the line 835 of the file convert.c about weighted edges. You can get the source for Networkx here and the source for igraph here. Please, note that the former is under BSD license and done in Python while igraph under GNU (GPL) and done in C.
To get started with Networkx, a helpful line about creating weighted graph from its jUnits test_convert_scipy.py -file:
def create_weighted(self, G):
g = cycle_graph(4)
e = g.edges()
source = [u for u,v in e]
dest = [v for u,v in e]
weight = [s+10 for s in source]
ex = zip(source, dest, weight)
G.add_weighted_edges_from(ex)
return G
So to create your Markov Chain, help about directed weighted graph here, something like this perhaps:
or perhaps there is some ready markov chain generation tool as there are for some other stochastic processes, more here. Cannot find an algorithm to analyze the graph with excepted value or do trials with different sets as in your example, perhaps there is none, and you must stick with other repliers' solutions.
发布评论
评论(3)
您可以使用以下内容吗?
在您的情况下,您可以将
myString
创建为 10000 个字符的真实字符串,并将pattern
创建为字符串,然后以简单的 Python 方式计算出现次数。EDIT
一行代码,为您提供
text
中pattern
出现(重叠)的次数(可以是字符串或列表) ),可能看起来像这样:Are you allowed to use the following?
In your case you could create
myString
as a real string of 10000 characters and thepattern
also as a string and then count the occurence in a simple pythonic way.EDIT
A one-liner that gives you the number of (overlapping) occurences of
pattern
intext
(which can be either a string or a list), could look like this:好的 - 标准(-ish)字符串搜索:
但是您想对数字列表执行此操作吗?没问题;我们只需要使用 find() 方法创建一个列表类,如下所示:
然后示例如下所示
,您的代码将变为:
Ok - a standard(-ish) string search:
but you want to do this to a list of numbers? No problem; we just need to make a list class with a find() method, like so:
then the example looks like
and your code becomes:
这还没有准备好。
类似的问题,但主要关注图形库此处 和类似的问题,但在 C# 中,可能有用。
与此问题相关的文件是
./networkx/generators/ Degree_seq.py
(997 行,关于生成具有给定度数序列的图)和./networkx/algorithms/mixing .py(第20行,关于基于概率的图的函数 Degree_assortativity(G))
,还要注意它的源代码引用了92个参考文献,不确定是否要重新发明轮子。对于 igraph,请阅读文件convert.c
中有关加权边的第 835 行。您可以在此处获取 Networkx 的源代码 和 igraph 在这里。请注意,前者在 BSD 许可下并在 Python 中完成,而 igraph 在 GNU (GPL) 下并在 C 中完成。要开始使用 Networkx,请参阅有关从其 jUnits 创建加权图的有用行
test_convert_scipy.py -file:
因此,要创建马尔可夫链,请帮助了解有向加权图此处,也许是这样的:
或者也许有一些现成的马尔可夫链生成工具,就像其他一些随机过程一样,更多此处。找不到算法来分析具有异常值的图表或使用不同的集合进行试验在您的示例中,也许没有,您必须坚持使用其他回复者的解决方案。
This is not ready.
Similar question but main focus on graph libraries here and similar question but in C#, maybe useful.
The files that are relevant to this question are
./networkx/generators/degree_seq.py
(997 lines, about generating graps with given degree sequence) and./networkx/algorithms/mixing.py (line 20, function degree_assortativity(G) about probability based graphs)
and also note that its source code refer to 92 references, not sure whether you want to reinvent the wheel. For igraph, please, read the line 835 of the fileconvert.c
about weighted edges. You can get the source for Networkx here and the source for igraph here. Please, note that the former is under BSD license and done in Python while igraph under GNU (GPL) and done in C.To get started with Networkx, a helpful line about creating weighted graph from its jUnits
test_convert_scipy.py
-file:So to create your Markov Chain, help about directed weighted graph here, something like this perhaps:
or perhaps there is some ready markov chain generation tool as there are for some other stochastic processes, more here. Cannot find an algorithm to analyze the graph with excepted value or do trials with different sets as in your example, perhaps there is none, and you must stick with other repliers' solutions.