㈠ 關於python的問題,好的高獎勵!
import random
class NumberG:
def __init__(self, trytimes = 10):
self.num = str(random.randint(1000,9999))
self.trytimes = trytimes
def comp(self, n):
nset, rs = set(list(self.num)), []
for i,c in enumerate(n):
rs.append(
'Y' if c==self.num[i] else (
'H' if c in (nset - set(self.num[i]))
else 'N'
)
)
print ''.join(rs)
return True if n == self.num else False
def run(self, trytimes = None):
trytimes = trytimes or self.trytimes
while trytimes:
if not self.comp(raw_input('Try a number:')):
trytimes -= 1
else:
print 'OK', 10 - trytimes
if __name__ == '__main__':
numberg = NumberG()
numberg.run()
㈡ Python的py如和轉換成pyc
單獨寫一個python文件,假設命名為comp.py,內容如下:
importpy_compile
py_compile.compile(r'路徑ASA.py')
代碼中「路徑」修改為你放asa.py的文件路徑,然後執行comp.py,會在目錄下生成名字為__pycache__的文件夾,pyc文件就在此文件內。
㈢ Python 解決你的實際問題可以在哪些地方找資料
Python 標准庫:
應該瀏覽一下這份文檔,它為標准庫中的類型、函數和模塊提供了完整(盡管很簡略)的參考資料。標準的 Python 發布版包括了 大量 的附加模塊。其中有針對讀取 Unix 郵箱、接收 HTTP 文檔、生成隨機數、解析命令行選項、寫 CGI 程序、壓縮數據以及很多其它任務的模塊。略讀一下庫參考會給你很多解決問題的思路。
安裝 Python 模塊 展示了如何安裝其他 Python 用戶編寫的附加模塊。
Python 語言參考: 詳細說明了 Python 語法和語義。
它讀起來很累,不過對於語言本身,有份完整的手冊很有用。
其它 Python 資源:
http://www.python.org: Python 官方網站。它包含代碼、文檔和 Web 上與 Python 有關的頁面鏈接該網站鏡像於全世界的幾處其它問題,類似歐洲、日本和澳大利亞。
鏡像可能會比主站快,這取決於你的地理位置。
http://docs.python.org: 快速訪問 Python 的文檔。
http://pypi.python.org: Python 包索引,以前昵稱為乳酪店,索引了可供下載的,用戶創建的 Python 模塊。如果你發布了代碼,可以注冊到這里,這樣別人可以找到它。
http://code.activestate.com/recipes/langs/python/: Python 食譜是大量的示例代碼、大型的集合,和有用的腳本。
值得關注的是這次資源已經結集成書,名為《Python 食譜》(O』Reilly & Associates, ISBN 0-596-00797-3。)
http://scipy.org: The Scientific Python 項目包括數組快速計算和處理模塊,和大量線性代數、傅里葉變換、非線性solvers、隨機數分布,統計分析以及類似的包。
與 Python 有關的問題,以及問題報告,可以發到新聞組 comp.lang.python ,或者發送到郵件組 [email protected] 。新聞組和郵件組是開放的,所以發送的消息可以自動的跟到另一個之後。每天有超過 120 個投遞(高峰時有數百),提問(以及回答)問題,為新功能提建議,發布新模塊。在發信之前,請查閱 常見問題 (亦稱 FAQ),或者在 Python 源碼發布包的 Misc/ 目錄中查閱。郵件組也可以在 http://mail.python.org/pipermail/ 訪問。FAQ回答了很多被反復提到的問題,很可能已經解答了你的問題。
Next Previous
㈣ Python中 1. 編寫函數,要求輸入x與y,返回x和y的平方差 2. 計算1到100的平方的和
1、
defpfc(x,y):
returnx**2-y**2
2、
sum(map(lambdax:x**2,range(101)))
3、
defcomp100(n):
returnTrueifn<100elseFalse
㈤ 講解:COMP6714、python、python、FRIPython|R
COMP6714 ASSIGNMENT 1DUE ON 20:59 29 NOV, 2019 (FRI)Q1. (25 marks)Consider the following pseudo code which performs list intersection based on the divideand-conquerparadigm. Note that the input lists are not necessarily sorted.Algorithm 1: Intersect(A, B)1 if ... then/* Deal with the boundary case */2 ...;3 return ...;4 else/* Recursively break down each list into two parts and recurse */5 ...;6 return ...;(1) Complete the above pseudo code. You can assume that you can invoke the followingmember methods on a List object L:• L.len returns the length of the list L.You can also use the usually indexing and slicing operation on the list (as inpython).(2) Think of a method to divide each input list into k sub-lists (k2) without changingthe main logic of the algorithm you implemented in the first part. You should beable to describe the only change succinctly.Q2. (25 marks)Consider the scenario of dynamic inverted index construction. Assume that t sub-indexes(each of M pages) will be created if one chooses the no-merge strategy.(1) Show that if the logarithmic merge strategy is used, it will result in at most dlog2 tesub-indexes.(2) Prove that the total I/O cost of the logarithmic merge is O(t · M · log2 t).12 DUE ON 20:59 29 NOV, 2019 (FRI)Q3. (25 marks)The following list of Rs and Ns represents relevant (R) and nonrelevant (N) returneddocuments in a ranked list of 20 documents retrieved in response to a query from a collectionof 10, 000 documents. The top of the ranked list is on the left of the list. This list shows 6relevant documents. Assume that there are 8 relevant documents in total in the collection.RRNNN NNNRN RNNNR NNNNR(Note that spaces above are just added to make the list easier to read)(1) What is the precision of the system on the top-20?(2) What is the F1 on the top-20?(3) What is/are the uninterpolated precision(s) of the system at 25% recall?(4) What is the interpolated precision at 33% recall?(5) Assume that these 20 documents are the complete result set of the system. Whatis the MAP for the query?Assume, now, instead, that the system returned the entire 10, 000 documents in a rankedlist, and these arCOMP6714代做、python程序語言代寫、代做pythe the first 20 results returned.(6) What is the largest possible MAP that this system could have?(7) What is the smallest possible MAP that this system could have?(8) In a set of experiments, only the top-20 results are evaluated by hand. The resultin (5) is used to approximate the range (6) to (7). For this example, how large (inabsolute terms) can the error for the MAP be by calculating (5) instead of (6) and(7) for this query?Q4. (25 marks)Suppose we have a document collection with an extremely small vocabulary with only6 words w1, w2,...,w6. The following table shows the estimated background languagemodel p(w|C) using the whole collection of documents (2nd column) and the word countsfor document d1 (3rd column) and d2 (4th column), where c(w, di) is the count of word win document di. Let Q = {w1, w2, w3, w4, w5, w6} be a query.Word p(w|C) c(w, d1) c(w, d2)w1 0.800 2 7w2 0.100 3 1w3 0.025 1 1w4 0.025 2 1w5 0.025 2 0w6 0.025 0 0(1) Suppose we do not smooth the language model for d1 and d2. Compute the likelihoodof the query for both d1 and d2, i.e., p(Q|d1) and p(Q|d2) (Do not computethe log-likelihood. You should use the scientific notation (e.g., 0.0061 should be6.1 ⇥ 103)Which document would be ranked higher?COMP6714 ASSIGNMENT 1 3(2) Suppose we now smooth the language model for d1 and d2 using the Jelinek-Mercersmoothing method with= 0.8 (i.e., p(w|d) = ·pmle(w|Md)+(1)·pmle(w|Mc)).Recompute the likelihood of the query for both d1 and d2, i.e., p(Q|d1) and p(Q|d2)(Do not compute the log-likelihood. You should use the scientific notation) Whichdocument would be ranked higher?Submission InstructionsYou need to write your solutions to the questions in a pdf file named ass1.pdf. Youmust• include your name and student ID in the file, and• the file can be opened correctly on CSE machines.You need to show the key steps to get the full mark.Note: Collaboration is allowed. However, each person must independently write uphis/her own solution.You can then submit the file by give cs6714 ass1 ass1.pdf. The file size is limitedto 5MB.Late Penalty: -10% per day for the first two days, and -20% per day for the followingdays.轉自:http://www.daixie0.com/contents/3/4354.html
㈥ python 從文件讀數並比較大小
file_a = open("a.txt")
file_b = open("b.txt", 'w')
comp_num = 10 # 此為固定數
num = file_a.readline()[: -1]
while num:
if float(num) > comp_num:
file_b.write(num + ' -1\n')
else:
file_b.write(num + ' 1\n')
num = filea.readline()[: -1]
file_a.close()
file_b.close()
㈦ Python求平均數大學計算機題
math=87
eng=72
comp=93
average=int((math+eng+comp)/3)
print('math=87,eng=72,comp=93,average=%s'%average)
㈧ python這道題啥意思怎麼做
python這道題是面向對象的用法考查,以復數類的構建為例,結合一點復數知識填入而可,排版和代碼如圖,注意填入的縮進(選中的代碼是題目內容,沒選中的是測試代碼,效果如下)
㈨ python sort()用法
Python中的sort()方法用於數組排序,下面以實例形式對此加以詳細說明:
一、基本形式
列表有自己的sort方法,其對列表進行原址排序,既然是原址排序,那顯然元組不可能擁有這種方法,因為元組是不可修改的。
x=[4,6,2,1,7,9]x.sort()
printx#[1,2,4,6,7,9]
如果需要一個排序好的副本,同時保持原有列表不變,怎麼實現呢
x=[4,6,2,1,7,9]
y=x[:]
y.sort()
printy#[1,2,4,6,7,9]
printx#[4,6,2,1,7,9]
注意:y = x[:] 通過分片操作將列表x的元素全部拷貝給y,如果簡單的把x賦值給y:y = x,y和x還是指向同一個列表,並沒有產生新的副本。
另一種獲取已排序的列表副本的方法是使用sorted函數:
x=[4,6,2,1,7,9]
y=sorted(x)
printy#[1,2,4,6,7,9]
printx#[4,6,2,1,7,9]
sorted返回一個有序的副本,並且類型總是列表,如下:
printsorted('Python')#['P','h','n','o','t','y']
二、自定義比較函數
可以定義自己的比較函數,然後通過參數傳遞給sort方法:
defcomp(x,y):
ifx<y:
return1
elifx>y:
return-1
else:
return0
nums=[3,2,8,0,1]
nums.sort(comp)
printnums#降序排序[8,3,2,1,0]
nums.sort(cmp)#調用內建函數cmp,升序排序
printnums#降序排序[0,1,2,3,8]
三、可選參數
sort方法還有兩個可選參數:key和reverse
1、key在使用時必須提供一個排序過程總調用的函數:
x=['mmm','mm','mm','m']
x.sort(key=len)
printx#['m','mm','mm','mmm']
2、reverse實現降序排序,需要提供一個布爾值:
y=[3,2,8,0,1]
y.sort(reverse=True)
printy#[8,3,2,1,0]
㈩ Lambda表達式的Python表達式
Lambda表達式是Python中一類特殊的定義函數的形式,使用它可以定義一個匿名函數。與其它語言不同,Python的Lambda表達式的函數體只能有唯一的一條語句,也就是返回值表達式語句。其語法如下:
lambda 形參列表 : 函數返回值表達式語句
下面是個Lambda表達式的例子: #!/usr/bin/envpythonli=[{age:20,name:def},{age:25,name:abc},{age:10,name:ghi}]li=sorted(li,key=lambdax:x[age])print(li)如果不用Lambda表達式,而要寫成常規的函數,那麼需要這么寫: #!/usr/bin/envpythondefcomp(x):returnx[age]li=[{age:20,name:def},{age:25,name:abc},{age:10,name:ghi}]li=sorted(li,key=comp)print(li)