锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
import sys
import re
def comment_finder(text):
pattern = re.compile( r'//.*?$|/\*.*?\*/', re.DOTALL | re.MULTILINE)
result = pattern.findall(text)
return result
def print_command(filename):
codefile = open(filename,'r')
commentfile = open(filename+".txt",'w')
lines=codefile.read()
codefile.close()
#the list of comments
list_of_comments = comment_finder(lines)
for comment in list_of_comments:
#print comment[0:2]
if comment[0:2] == "//":
comment_to_write = comment[2:]
else:
comment_to_write = comment[2:-2]
if len(comment_to_write)!=0:
commentfile.write(comment_to_write)
commentfile.write('\n')
commentfile.close()
if __name__ == "__main__":
for filename in sys.argv[1:]:
print_command(filename)
鎴栬?鎸囧畾鏂囦歡綾誨瀷
]]>
def mianji(n,s):
============
浣跨敤鏃秏ath.pi math.tan
]]>s1 = "hello,world"
濡傛灉瑕佸啓鎴愬琛岋紝閭d箞灝辮浣跨敤/ (“榪炶絎?#8221;)鍚э紝濡?nbsp;
s2 = "hello,/
world"
s2涓巗1鏄竴鏍風殑銆傚鏋滀綘鐢?涓弻寮曞彿鐨勮瘽錛屽氨鍙互鐩存帴鍐欎簡錛屽涓嬶細
s3 = """hello,
world,
hahaha."""錛岄偅涔坰3瀹為檯涓婂氨鏄?hello,/nworld,/nhahaha.", 娉ㄦ剰“/n”
s5 = "Let's go"
s4 = 'Let/'s go'
鎴戜滑涔熷彲浠ユ妸''' ''' 浣滀負澶氳娉ㄩ噴
str(object) 鍙互灝嗘墍鏈夎漿鍖栦負瀛楃涓層?br />python java 鎻忚堪 or || 閫昏緫鎴?/td> and && 閫昏緫涓?/td> not 錛?/td> 閫昏緫闈?/td> <錛?gt;錛?lt;=錛?gt;=錛?=錛?=鎴?lt;> <錛?gt;錛?lt;=錛?gt;=錛?=錛?= 姣旇緝鎿嶄綔 is錛宨s not instanceof 韜喚璁よ瘉 | | 浣嶆垨 & & 浣嶄笌 ^ ^ 浣嶅紓鎴?/td> <<錛?gt;> <<錛?gt;> 縐諱綅 +錛?錛?錛? +錛?錛?錛? 鍔犲噺涔橀櫎 % % 浣欐暟 ~ ~ 浣嶅彇琛?/td>
//榪愮畻絎?
10/3==3
120//10==12
121//10==12
122//10==12
130//10==13
10//3.0==3.0
A new operator, //, is the floor division operator. (Yes, we know it
looks like C++'s comment symbol.) // always performs floor division no
matter what the types of its operands are, so 1 // 2 is 0 and 1.0 //
2.0 is also 0.0.
not ()
]]>
濡傛灉鎴戜滑姹傛墍鏈夌殑鑻規灉鐨勯噸閲忥紝鍙渶瑕佸皢鎵鏈夌殑綆卞瓙鍐呯殑鑻規灉鍙栧嚭鏉ユ眰閲嶉噺鍗沖埢銆?br />浣嗘槸鎴戜滑鍏堟眰label a鐨勭瀛愯嫻鏋滅殑閲嶉噺錛屽姞涓妉abel b鐨勭瀛愯嫻鏋滅殑閲嶉噺錛屽彲鑳藉嚭鐜頒袱嬈″彇鐨勬槸鍚屼竴涓瀛愶紝榪欏氨鏄痩abel switching闂銆?img src ="http://m.shnenglu.com/luyulaile/aggbug/194109.html" width = "1" height = "1" />
]]>
"""
Uses the inverse CDF method to return samples drawn from an
(unnormalized) discrete distribution.
Arguments:
dist -- (unnormalized) distribution
Keyword arguments:
num_samples -- number of samples to draw
"""
cdf = cumsum(dist)
r = uniform(size=num_samples) * cdf[-1]
return cdf.searchsorted(r)
Let's see how to generate corpus for Dirichlet--multinomial unigram language model
"""
Returns a corpus of tokens drawn from a Dirichlet--multinomial
unigram language model. Each token is an instance of one of V
unique word types, represented by indices 0,
, V - 1.
Arguments:
beta -- concentration parameter for the Dirichlet prior
mean -- V-dimensional mean of the Dirichlet prior
N -- number of tokens to generate
"""
pass # YOUR CODE GOES HERE
#print mean
#print beta
#print dot(mean,beta)
#print dirichlet(mean*beta,size=1)
temp=sample(dirichlet(beta*array(mean),size=1),N)
#print temp
return temp
and the parameters it receives are corresponding to beta*array(mean). beta is the concentration factor, and mean is the vector which sum to 1.
another way is to generate corpus is using the property:
P(D'|D,H)= Nv+beta_nv/N+beta
"""
Returns a corpus of tokens drawn from a Dirichlet--multinomial
unigram language model using the 'collapsed' generative process
(i.e., phi is not explicitly represented). Each token is an
instance of one of V unique word types.
Arguments:
beta -- concentration parameter for the Dirichlet prior
mean -- V-dimensional mean of the Dirichlet prior
N -- number of tokens to generate
"""
V = len(mean) # vocabulary size
corpus = zeros(N, dtype=int) # corpus
Nv = zeros(V, dtype=int) # counts for each word type
pass # YOUR CODE GOES HERE
for n in xrange(N):
corpus[n]=sample((Nv+beta*array(mean))/(n+beta),1)
Nv[corpus[n]]+=1;
return corpus
Let's see how to generate corpus for Mixture of Dirichlet-multinomial unigram language model
"""
Returns a grouped corpus drawn from a mixture of
Dirichlet--multinomial unigram language models.
Arguments:
alpha -- concentration parameter for the Dirichlet prior over theta
m -- T-dimensional mean of the Dirichlet prior over theta
beta -- concentration parameter for the Dirichlet prior over phis
n -- V-dimensional mean of the Dirichlet prior over phis
D -- number of documents to generate
Nd -- number of tokens to generate per document
"""
corpus = GroupedCorpus()
pass # YOUR CODE GOES HERE
#determine the topic the distribution for topic dirichlet(dot(m,alpha),size=1)
#given the topic, the distribtuion for word dirichlet(dot(n,beta),size=1)
theta=dirichlet(alpha*array(m),1)
phis=dirichlet(beta*array(n),len(m))
for d in range(0,D):
[t]=sample(theta,1)
#print groupVcab
corpus.add(str(d),str(t),[str(x) for x in sample(phis[t,:],Nd)])
return corpus
]]>
鐩存帴 result=[]
]]>
Python IO
杈撳嚭 printstr = raw_input("Enter your input: "); print "Received input is : ", str
Python鍙湁涓夌鍙橀噺綾誨瀷 int, string, float?
typeof(1.5)
璨屼技涓嶆敮鎸侀殣寮忕被鍨嬭漿鎹?br />print str(2.5) 瀵?br />print 2.5 閿?br />print '2.5' 瀵?br />
python 瀹氫箟鏂規硶鏄?br />def MethodName(para,para2): #娉ㄦ剰榪欓噷鐨勫啋鍙?br /> if
python娉ㄩ噴
#鍗曡娉ㄩ噴
""" 涓変釜鍙屽紩鍙鋒槸澶氳娉ㄩ噴 """
python 寮曠敤
include math
Python鐨剆tr
str(var)綾誨瀷杞崲
len(var)
var.upper()
var.lower()
var[2] 絎笁涓紙娉ㄦ剰涓嬫爣浠?寮濮嬶級鍏冪礌錛岀被浼間簬list
var[:3] 鍓嶄笁涓厓绱狅紝瀹為檯涓婃寚鐨勬槸0鎴鍒?-1鐨勫厓绱?br />var[2:4]涓嬫爣鏄?鍒?-1鐨勬墍鏈夊厓绱?br />
Python鐨刲ist
exampe=[a,b,c,d,e,f];
len(exampe)
鑷甫sort鏂規硶
Python鐨刣ictionary
key -value瀵瑰簲
value鍙互鏄竴涓猯ist
娉ㄦ剰鏂規嫭鍙穂]閲岄潰鍙兘浣跨敤key
鍖哄垎 del dict['Name']del dict['Name']; # remove entry with key 'Name' dict.clear(); # remove all entries in dict del dict ; # delete entire dictionary
鑷甫鐨勬柟娉曪細1 cmp(dict1, dict2)
Compares elements of both dict.2 len(dict)
Gives the total length of the dictionary. This would be equal to the number of items in the dictionary.3 str(dict)
Produces a printable string representation of a dictionary4 type(variable)
Returns the type of the passed variable. If passed variable is dictionary then it would return a dictionary type.
Python綾葷殑瀹氫箟鍜岀被鏂規硶鐨勫畾涔?br />瀹氫箟綾諱笉闇瑕佺敤def class,鐩存帴
class ClassName(object):
姣忎釜綾婚兘鏈?__init__(self,arg):
鏂規硶錛屾敞鎰忔槸 宸﹀彸鍚勪袱涓笅鍒掔嚎錛屾誨叡4鏍逛笅鍒掔嚎
綾繪柟娉曢兘闇瑕佸寘鍚玸elf榪欎釜鍙傛暟錛屼絾鏄嬌鐢ㄧ殑鏃跺欎笉闇瑕乻elf,瑙佷笅渚?br />
渚嬪
Python涓殑綾誨彉閲忎笉鑳?self.xxx鏉ュ紩鐢紝浣嗘槸鎴愬憳鍙橀噺鍙互
Class variables are special because they belong to the class; the objects created do not get their own copies of the class variable. Class variables are accessed using the class name and dot notation.ClassName.classVar
Class variables are created outside of__init__
渚嬪錛?br />
甯哥姱閿欒錛歩ndentation is very important
python indentation error expected an indented block
榪樻湁涓涓敊璇氨鏄?綾繪柟娉曪紝蹇呴』浣跨敤 self鍙傛暟錛屽嵆浣挎病鏈夊弬鏁幫紒錛?/span>
鍙﹀涓涓父閿欑殑鍦版柟灝辨槸 __init__(self,arg) 涓瀹氭槸鍥涙牴涓嬪垝綰?/span>
涓嶄粎瑕佽寰楃暀dent 榪樿璁板緱 緙╄繘
]]>