本文整理汇总了Python中string.count函数的典型用法代码示例。如果您正苦于以下问题:Python count函数的具体用法?Python count怎么用?Python count使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了count函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: writeArray
def writeArray (xy, file=None, format='%g ', comments=[], commentChar='#'):
""" Write a numeric array xy to file (or stdout if unspecified).
(format must have one, two, or xy.shape[1] specifiers, line feed is appended if necessary.) """
if file:
if hasattr(file,'closed'):
if file.mode=='w': out=file
else: print 'ERROR --- IO.writeArray: file "' + file + '" opened in readmode!'
else:
out=open(file,'w')
# print command line as first line to output file
if not (sys.argv[0]=='' or 'ipython' in sys.argv[0]):
sys.argv[0] = os.path.basename(sys.argv[0])
out.write (commentChar + ' ' + join(sys.argv) + '\n' + commentChar + '\n')
else: out = sys.stdout
for com in comments: out.write ( '%s %s\n' % (commentChar, strip(com)) )
if len(xy.shape)==1:
if count(format,'\n')==0: format = format.rstrip()+'\n'
for i in range(xy.shape[0]): out.write (format % xy[i] )
elif len(xy.shape)==2:
npc = count(format,'%')
if npc==1:
format = xy.shape[1] * format
elif npc==2 and xy.shape[1]>2:
f2 = rfind(format,'%')
format = format[:f2] + (xy.shape[1]-1) * (' '+format[f2:])
elif npc!=xy.shape[1]:
print "ERROR --- IO.writeArray: check format (number of format specs does'nt match number of columns in data)"
return
if count(format,'\n')==0: format = format.rstrip()+'\n'
for i in range(xy.shape[0]): out.write (format % tuple(xy[i,:]) )
else:
print 'ERROR --- IO.writeArray: writing arrays with more than 2 dimensions not supported!'
if not out.closed: out.close()
开发者ID:jaymz07,项目名称:spectral-line-fit,代码行数:35,代码来源:IO.py
示例2: deg2HMS
def deg2HMS(ra='', dec='', round=False):
import string
RA, DEC= '', ''
if dec:
if string.count(str(dec),':')==2:
dec00=string.split(dec,':')
dec0,dec1,dec2=float(dec00[0]),float(dec00[1]),float(dec00[2])
if '-' in str(dec0): DEC=(-1)*((dec2/60.+dec1)/60.+((-1)*dec0))
else: DEC=(dec2/60.+dec1)/60.+dec0
else:
if str(dec)[0]=='-': dec0=(-1)*abs(int(dec))
else: dec0=abs(int(dec))
dec1=int((abs(dec)-abs(dec0))*(60))
dec2=((((abs(dec))-abs(dec0))*60)-abs(dec1))*60
DEC='00'[len(str(dec0)):]+str(dec0)+':'+'00'[len(str(dec1)):]+str(dec1)+':'+'00'[len(str(int(dec2))):]+str(dec2)
if ra:
if string.count(str(ra),':')==2:
ra00=string.split(ra,':')
ra0,ra1,ra2=float(ra00[0]),float(ra00[1]),float(ra00[2])
RA=((ra2/60.+ra1)/60.+ra0)*15.
else:
ra0=int(ra/15.)
ra1=int(((ra/15.)-ra0)*(60))
ra2=((((ra/15.)-ra0)*60)-ra1)*60
RA='00'[len(str(ra0)):]+str(ra0)+':'+'00'[len(str(ra1)):]+str(ra1)+':'+'00'[len(str(int(ra2))):]+str(ra2)
if ra and dec: return RA, DEC
else: return RA or DEC
开发者ID:rkirkpatrick,项目名称:lcogtsnpipe,代码行数:27,代码来源:lscabsphotdef_old.py
示例3: bitrigramFeature
def bitrigramFeature(self):
bi_template=[]
tri_template=[]
biFeature={}
triFeature={}
for l in string.lowercase:
for m in string.lowercase:
for n in string.lowercase:
tri_template.append(l+m+n)
bi_template.append(l+m)
words=''
for temp in self.sentences:
if self.output==False:
length=len(temp)-3
else:
length=len(temp)-2
for i in range(length):
words +=temp[i]+' '
for elem in bi_template:
biFeature[elem]=float(string.count(words,elem))
for elem in tri_template:
triFeature[elem]=float(string.count(words,elem))
triSum=sum(triFeature.values())
biSum=sum(biFeature.values())
for elem in biFeature:
biFeature[elem]=biFeature[elem]/biSum
for elem in triFeature:
triFeature[elem]=triFeature[elem]/triSum
return biFeature, triFeature
开发者ID:prafulla77,项目名称:SEMEVAL2016,代码行数:29,代码来源:doc_feature.py
示例4: scoreParse
def scoreParse(self, parse, correct):
"""Compares the algorithms parse and the correct parse and assigns a score for that parse.
The score is based upon how far apart the right and left parentheses are from each other
in the two parses and also includes a penalty for parentheses in one parse but not the
other.
"""
score = 0
compCnt = string.count(parse, "(")
realCnt = string.count(correct, "(")
compPos = -1
realPos = -1
for i in range(compCnt):
compPos = string.find(parse, "(", compPos+1, len(parse))
realPos = string.find(correct, "(", realPos+1, len(correct))
score += abs(compPos - realPos)
score += abs(compCnt - realCnt)
compCnt = string.count(parse, ")")
realCnt = string.count(correct, ")")
compPos = -1
realPos = -1
for i in range(compCnt):
compPos = string.find(parse, ")", compPos+1, len(parse))
realPos = string.find(correct, ")", realPos+1, len(correct))
score += abs(compPos - realPos)
score += abs(compCnt - realCnt)
return score
开发者ID:decode,项目名称:Rpkg,代码行数:26,代码来源:MIParser.py
示例5: myFeatures
def myFeatures(string):
all_notes = re.findall(r"[0-9]0? *?/ *?10", string)
if all_notes:
print(all_notes)
all_notes = [int(x.split("/")[0].strip()) for x in all_notes]
mean = np.mean(all_notes)
maxim = np.max(all_notes)
minim = np.min(all_notes)
print(all_notes, mean, maxim, minim)
else:
mean = -1
maxim = -1
minim = -1
return [
len(string),
string.count("."),
string.count("!"),
string.count("?"),
len(re.findall(r"[^0-9a-zA-Z_ ]", string)), # Non aplha numeric
len(re.findall(r"10", string)),
len(re.findall(r"[0-9]", string)),
string.count("<"),
len(re.findall(r"star(s)?", string)),
mean,
maxim,
minim,
len(re.findall(r"[A-Z]", string)),
]
开发者ID:MartinDelzant,项目名称:AdvBigData,代码行数:28,代码来源:fonctions.py
示例6: action
def action(self, user, cmd_list):
t = self.telnet
t.write("\n")
login_prompt = "login: "
response = t.read_until(login_prompt, 5)
if string.count(response, login_prompt):
print response
else:
return 0
password_prompt = "Password:"
t.write("%s\n" % user)
response = t.read_until(password_prompt, 3)
if string.count(response, password_prompt):
print response
else:
return 0
t.write("%s\n" % self.passwd[user])
response = t.read_until(self.command_prompt, 5)
if not string.count(response, self.command_prompt):
return 0
for cmd in cmd_list:
t.write("%s\n" % cmd)
response = t.read_until(self.command_prompt, self.timeout)
if not string.count(response, self.command_prompt):
return 0
print response
return 1
开发者ID:bhramoss,项目名称:code,代码行数:27,代码来源:recipe-52228.py
示例7: ScriptURL
def ScriptURL(target, web_page_url=None, absolute=0):
"""target - scriptname only, nothing extra
web_page_url - the list's configvar of the same name
absolute - a flag which if set, generates an absolute url
"""
if web_page_url is None:
web_page_url = mm_cfg.DEFAULT_URL
if web_page_url[-1] <> '/':
web_page_url = web_page_url + '/'
fullpath = os.environ.get('REQUEST_URI')
if fullpath is None:
fullpath = os.environ.get('SCRIPT_NAME', '') + \
os.environ.get('PATH_INFO', '')
baseurl = urlparse.urlparse(web_page_url)[2]
if not absolute and fullpath[:len(baseurl)] == baseurl:
# Use relative addressing
fullpath = fullpath[len(baseurl):]
i = string.find(fullpath, '?')
if i > 0:
count = string.count(fullpath, '/', 0, i)
else:
count = string.count(fullpath, '/')
path = ('../' * count) + target
else:
path = web_page_url + target
return path + mm_cfg.CGIEXT
开发者ID:OS2World,项目名称:APP-SERVER-MailMan,代码行数:26,代码来源:Utils.py
示例8: fixcode
def fixcode(block, doctype):
# Some HTML preparation
block = Detag(block)
block = LeftMargin(block)
# Pull out title if available
re_title = re.compile('^#\-+ (.+) \-+#$', re.M)
if_title = re_title.match(block)
if if_title:
title = if_title.group(1)
block = re_title.sub('', block) # take title out of code
else: title = ''
#block = string.strip(block) # no surrounding whitespace
# Process the code block with Py2HTML (if possible and appropriate)
if py_formatter and (string.count(title,'.py') or
string.count(title,'Python') or
string.count(title,'python') or
string.count(title,'py_') or
doctype == 'PYTHON'):
fh = open('tmp', 'w')
fh.write(block)
fh.close()
py2html.main([None, '-format:rawhtml', 'tmp'])
block = open('tmp.html').read()
block = code_block % (title, block)
# elif the-will-and-the-way-is-there-to-format-language-X:
# elif the-will-and-the-way-is-there-to-format-language-Y:
else:
block = code_block % (title, '<pre>'+block+'</pre>')
return block
开发者ID:Ax47,项目名称:devSpiral,代码行数:31,代码来源:dmTxt2Html.py
示例9: run
def run(self):
try:
if osflag:
proc=Popen(self.cmd,shell=False,stdin=None,stdout=PIPE,\
stderr=STDOUT,bufsize=0)
else:
from subprocess import STARTUPINFO
si=STARTUPINFO()
si.dwFlags|=1
si.wShowWindow=0
proc=Popen(self.cmd,shell=False,stdin=None,stdout=PIPE,\
stderr=STDOUT,bufsize=0,startupinfo=si)
while 1:
if self.stop_flag:
if osflag: proc.send_signal(signal.SIGKILL)
else: proc.kill()
break
if osflag:
if proc.stdout in select.select([proc.stdout],[],[],1)[0]:
line=proc.stdout.readline()
else: line=' \n'
else: line=proc.stdout.readline()
if not len(line): break
else:
if count(line,'ttl') or count(line,'TTL'): self.retries=0
else: self.retries=self.retries+1
line=' '
sleep(0.5)
proc.poll()
except: pass
开发者ID:chenlong828,项目名称:multipingmonitor,代码行数:30,代码来源:mpm.py
示例10: main
def main():
pathname = EasyDialogs.AskFileForOpen(message="File to check end-of-lines in:")
if not pathname:
sys.exit(0)
fp = open(pathname, "rb")
try:
data = fp.read()
except MemoryError:
EasyDialogs.Message("Sorry, file is too big.")
sys.exit(0)
if len(data) == 0:
EasyDialogs.Message("File is empty.")
sys.exit(0)
number_cr = string.count(data, "\r")
number_lf = string.count(data, "\n")
if number_cr == number_lf == 0:
EasyDialogs.Message("File contains no lines.")
if number_cr == 0:
EasyDialogs.Message("File has unix-style line endings")
elif number_lf == 0:
EasyDialogs.Message("File has mac-style line endings")
elif number_cr == number_lf:
EasyDialogs.Message("File probably has MSDOS-style line endings")
else:
EasyDialogs.Message("File has no recognizable line endings (binary file?)")
sys.exit(0)
开发者ID:Oize,项目名称:pspstacklesspython,代码行数:26,代码来源:checktext.py
示例11: readworkingdirfile
def readworkingdirfile(fname):
# retun the contents of file fname
# if it is a dos file convert it to Mac
fp=open(getworkingdir()+fname,'rb')
try:
data = fp.read()
except MemoryError:
print 'Sorry, file is too big.'
return
if len(data) == 0:
print 'File is empty.'
return
number_cr = string.count(data, '\r')
number_lf = string.count(data, '\n')
#if number_cr == number_lf == 0:
#EasyDialogs.Message('File contains no lines.')
if number_cr == 0:
#EasyDialogs.Message('File has unix-style line endings')
data=string.replace(data,'\n','\r') #make it a mac file
#elif number_lf == 0:
#EasyDialogs.Message('File has mac-style line endings')
elif number_cr == number_lf:
#EasyDialogs.Message('File probably has MSDOS-style line endings')
data=string.replace(data,'\n','') #make it a mac file
#else:
#EasyDialogs.Message('File has no recognizable line endings (binary file?)')
#f=open('Macintosh HD:Python 2.0:santosfiles:gumbhere1','w')
#f.write(data)
fp.close()
return data
开发者ID:le0ra,项目名称:eplusinterface_diagrams,代码行数:30,代码来源:mylib1.py
示例12: load
def load(self,name):
"""
Returns a two dimensional numpy array where a[:,0] is
wavelength in Angstroms and a[:,1] is flux in
counts/sec/angstrom/cm^2
Noisy spectra are smoothed with window_len in the .txt file.
Ergs and AB Mag units are automatically converted to counts.
"""
fname = self.objects[name]['dataFile']
fullFileName = os.path.join(self.this_dir,"data",fname[0])
if (string.count(fullFileName,"fit")):
a = self.loadSdssSpecFits(fullFileName)
else:
a = numpy.loadtxt(fullFileName)
len = int(self.objects[name]['window_len'][0])
if len > 1:
a[:,1] = smooth.smooth(a[:,1], window_len=len)[len/2:-(len/2)]
try:
fluxUnit = self.objects[name]['fluxUnit'][0]
scale = float(fluxUnit.split()[0])
a[:,1] *= scale
except ValueError:
print "error"
ergs = string.count(self.objects[name]['fluxUnit'][0],"ergs")
if ergs:
a[:,1] *= (a[:,0] * self.k)
mag = string.count(self.objects[name]['fluxUnit'][0],"mag")
if mag:
a[:,1] = (10**(-2.406/2.5))*(10**(-0.4*a[:,1]))/(a[:,0]**2) * (a[:,0] * self.k)
return a
开发者ID:stoughto,项目名称:MKIDStd,代码行数:32,代码来源:MKIDStd.py
示例13: parseResults
def parseResults(output):
results = {}
for line in output:
print line,
if string.count(line, "SIGSEG"):
results[0] = ["FAULT", string.strip(line)]
continue
# look for something of the form:
# filename:line:message
msg = string.split(line,":",2)
if len(msg)<3: continue
if msg[0]!=inputfilename: continue
if len(msg[1])==0: continue
if not msg[1][0] in string.digits: continue
# it's in the right form; parse it
linenumber = int(msg[1])
msgtype = "UNKNOWN"
uppermsg = string.upper(msg[2])
if string.count(uppermsg,"ERROR"):
msgtype = "ERROR"
if string.count(uppermsg,"WARNING"):
msgtype = "WARNING"
msgtext = string.strip(msg[2])
ignore = 0
for ignoreExpr in ignoreExprList:
if re.search(ignoreExpr,msgtext)!=None:
ignore = 1
if not ignore:
results[linenumber]=[msgtype,string.strip(msg[2])]
return results
开发者ID:Jason-K,项目名称:sdcc,代码行数:33,代码来源:valdiag.py
示例14: process_request
def process_request(self, req):
req.perm.assert_permission ('WIKI_MODIFY')
page_name = req.args['target_page'][req.args['target_page'].find('wiki')+5:]
p = WikiPage(self.env, page_name )
author_name = req.authname
comment_text = req.args['comment']
comment_parent = req.args['comment_parent']
dt = datetime.now()
comment_date = dt.strftime("%Y-%m-%d %H:%M:%S")
comment_id = "%032x" % random.getrandbits(128)
redirect_url = "%s%s#%s" % (req.base_path, req.args['target_page'],comment_id)
changeset_comment = "%s..." % comment_text[:20]
insertion_index = string.find( p.text, "=%s" % comment_parent )
if ( insertion_index != -1 ):
heads = string.count(p.text,"{{{#!WikiComments",0,insertion_index)
tails = string.count(p.text,"}}}",0,insertion_index)
level = heads - tails
padding = ""
comment_out = '%s{{{#!WikiComments author="%s" date="%s" id="%s""\n%s%s\n%s=%s\n%s}}}\n' \
% (padding, author_name,comment_date,comment_id,padding,comment_text,padding,comment_id,padding)
p.text = p.text[:insertion_index]+comment_out+p.text[insertion_index:]
p.save( author_name, changeset_comment, req.remote_addr )
req.redirect(redirect_url)
开发者ID:jarnik,项目名称:trac-wikicomments,代码行数:26,代码来源:wikicomments.py
示例15: extratabs
def extratabs(self, line):
tabcount = 0
for c in line:
if c <> "\t":
break
tabcount = tabcount + 1
last = 0
cleanline = ""
tags = PyFontify.fontify(line)
# strip comments and strings
for tag, start, end, sublist in tags:
if tag in ("string", "comment"):
cleanline = cleanline + line[last:start]
last = end
cleanline = cleanline + line[last:]
cleanline = string.strip(cleanline)
if cleanline and cleanline[-1] == ":":
tabcount = tabcount + 1
else:
# extra indent after unbalanced (, [ or {
for open, close in (("(", ")"), ("[", "]"), ("{", "}")):
count = string.count(cleanline, open)
if count and count > string.count(cleanline, close):
tabcount = tabcount + 2
break
return tabcount
开发者ID:krattai,项目名称:xbmc-antiquated,代码行数:26,代码来源:Wtext.py
示例16: callback
def callback(match):
if not any([match.group(0).startswith('/%s' % s) for s in self.start_markers]):
return match.group(0)
start, end = match.start(), match.end()
start_line = string.count(text, '\n', 0, start)
start_col = start - string.rfind(text, '\n', 0, start) - 1
if start_col < 0:
start_col = 0
end_line = string.count(text, '\n', 0, end)
end_col = end - string.rfind(text, '\n', 0, end) - 1
offset_line = start_line + 1
raw_text = match.group(0)
# Compute offset column.
idx = raw_text.find('\n') + 1
for offset_col in xrange(idx, len(raw_text)):
if raw_text[offset_col] not in [' ', '\t']:
break
if raw_text[offset_col] == '*':
offset_col += 1
if raw_text[offset_col] in [' ', '\t']:
offset_col += 1
offset_col -= idx
proc_text = self._stripText(raw_text, offset_col)
comments.append(Comment(start_line, start_col, start, end_line, end_col, end,
offset_line, offset_col, proc_text, raw_text))
return match.group(0)
开发者ID:CVroland,项目名称:seqan,代码行数:27,代码来源:file_mgr.py
示例17: fixcode
def fixcode(block, doctype='UNKNOWN'):
# Some XML preparation
block = LeftMargin(block)
# Pull out title if available
re_title = re.compile('^#\-+ (.+) \-+#$', re.M)
if_title = re_title.match(block)
if if_title:
title = if_title.group(1)
block = re_title.sub('', block) # take title out of code
else: title = ''
# Process the code block with dw_colorize (if possible and appropriate)
if py_formatter and (string.count(title,'.py') or
string.count(title,'Python') or
string.count(title,'python') or
string.count(title,'py_') or
doctype == 'PYTHON'):
print ('<p><heading refname="code1" type="code" toc="yes">%s</heading>'
% Typography(title))
print '<code type="section">',
dw_colorize.Parser(block.rstrip()).toXML()
print '</code></p>'
# elif the-will-and-the-way-is-there-to-format-language-X:
# elif the-will-and-the-way-is-there-to-format-language-Y:
else:
block = Detag(block)
print code_block % (Typography(title), block.strip())
开发者ID:Ax47,项目名称:devSpiral,代码行数:27,代码来源:txt2dw.py
示例18: defswarp
def defswarp(namefile, imgname, _combine, gain=''):
import floyds
import string, re, os
if _combine.lower() in ['median']:
_combine = 'MEDIAN'
elif _combine.lower() in ['average']:
_combine = 'AVERAGE'
elif _combine.lower() in ['sum']:
_combine = 'SUM'
swarpfile = floyds.__path__[0] + '/standard/sex/default.swarp'
f = open(swarpfile, 'r')
ss = f.readlines()
f.close()
ff = open(namefile, 'w')
for i in ss:
if string.count(i, 'IMAGEOUT_NAME') == 1:
ff.write('IMAGEOUT_NAME ' + str(imgname) + ' # Output filename \n')
elif string.count(i, 'WEIGHTOUT_NAME') == 1:
ff.write('WEIGHTOUT_NAME ' + str(
re.sub('.fits', '.weight.fits', imgname)) + ' # Output weight-map filename \n')
elif string.count(i, 'COMBINE_TYPE') == 1:
ff.write('COMBINE_TYPE ' + str(_combine) + ' # MEDIAN,AVERAGE,MIN,MAX,WEIGHTED,CHI2 \n')
elif string.count(i, 'GAIN_DEFAULT') == 1:
if gain:
ff.write('GAIN_DEFAULT ' + str(gain) + ' # Default gain if no FITS keyword found \n')
else:
ff.write(i)
else:
ff.write(i)
ff.close()
return namefile
开发者ID:talister,项目名称:FLOYDS_pipeline,代码行数:32,代码来源:util.py
示例19: find_module_for_item
def find_module_for_item(modules, item):
"""Find the matching module for a given item"""
dbg("find_module_for_item: Searching for: %s" % item)
module = None
# We need a shortcut here for root level items
if not ARGUMENTS.standalone and string.count(item, '.') == 1:
dbg("find_module_for_item: Using root-level shortcut")
module = "hs"
# Methods are very easy to shortcut
if string.count(item, ':') == 1:
dbg("find_module_for_item: Using method shortcut")
module = item.split(':')[0]
if not module:
matches = []
for mod in modules:
if item.startswith(mod):
matches.append(mod)
matches.sort()
dbg("find_module_for_item: Found options: %s" % matches)
try:
module = matches[-1]
except IndexError:
err("Unable to find module for: %s" % item)
dbg("find_module_for_item: Found: %s" % module)
return module
开发者ID:Habbie,项目名称:hammerspoon,代码行数:30,代码来源:build_docs.py
示例20: remove_polyA
def remove_polyA(self, at_percentage = .9):
"""Remove patterns which are likely due to polyA tails from the lists.
This is just a helper function to remove pattenrs which are likely
just due to polyA tails, and thus are not really great motifs.
This will also get rid of stuff like ATATAT, which might be a
useful motif, so use at your own discretion.
XXX Could we write a more general function, based on info content
or something like that?
Arguments:
o at_percentage - The percentage of A and T residues in a pattern
that qualifies it for being removed.
"""
remove_list = []
# find all of the really AT rich patterns
for pattern_info in self._pattern_list:
pattern_at = (float(string.count(pattern_info[1], 'A') +
string.count(pattern_info[1], 'T')) /
float(len(pattern_info[1])))
if pattern_at > at_percentage:
remove_list.append(pattern_info)
# now remove them from the master list
for to_remove in remove_list:
self._pattern_list.remove(to_remove)
开发者ID:dbmi-pitt,项目名称:DIKB-Evidence-analytics,代码行数:28,代码来源:Pattern.py
注:本文中的string.count函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论