Pythonカリキュラム学習記録_16週間の作業


1.2つの順序付きリストを1つの順序付きリストに結合する関数を設計します.2つのリストが与えられ、小さいものから大きいものまで並べ替えられ、各リストには重複要素がありません.この2つのリストを、入力したすべての要素を含む小さなリストから大きなリストに結合しますが、重複する要素はありません.
以下の関数は、要求に従って実装、モジュールmerge、すなわちmergeに格納されていると定義してください.py:
def merge(aList, bList):
"""aList and bList are two integer lists in ascending order.
The function returns a new integer list in ascending order with all elements in aList and bList but no repetitions.
for example, merge([2,3, 4,5],[1,3,5]) returns [1,2,3,4,5].
"""
def merge(aList, bList):
	l,r = 0,0
	result=[]
	while l

コンテンツ中のMITカーテンレッスンを参照して、集計ソートの集計について録画を実現することができる.ここでの集計と録画中の集計の違いは,重複要素がないことである.
2.
次の関数を実装します.
モジュールselectionとして定義、すなわちselectionに格納.py:
def selection(aList, i):
"""exchange aList[i] with the first smallest element in aList[i:], where i is a valid index. Don't change other elements. """
関数のさらなる解釈は、コンテンツの録画selectionを参照してください.mp4.
def selection(aList, i):
	temp_list=aList
	smallest = i
	for j in range(i+1,len(temp_list)):
	#          index,       
		if(temp_list[j]

3.
次の関数を実装します.
モジュールgetnumsとして定義、すなわちgetnumsに格納.py:
def getnums(filename):
"""filename is a text file which contains many integers separated by white spaces and commas.
The function read the file filename and returns a list of all the integers in filename in the order of their occurrences.
""""
1つのファイルがあまり大きくない場合は、メモリを置くことができます.readlines()メソッドでメモリを一度に読み込みます.つまり、ファイルの内容をリストで格納し、リストを処理します.
f=open(filename,'r')#ファイルオブジェクトを返す
lines=f.readlines()#linesはfilename内の各行の文字列からなるリストです.linesは変数名にすぎず、任意の変数名を取ることができます.
f.close()#ファイルオブジェクトを閉じる
次にここでlinesを処理し、必要な結果を返します.たとえば、
for line in lines:#ここでlineはもちろん変数で、どんな名前でも使えます.
あなたの処理文
strタイプオブジェクトのメソッドsplit()とstrip()が必要かもしれません.
データファイルの例numbers.txt,getnums('numbers.txt')はリストを返します
[12, 34, 23, 45, 11, 23, 48, 56, 50, 123, 100].
パラメータが示すファイルfilenameとスクリプトgetnumsを仮定する.pyは同じディレクトリにあります.
def split4list(numberlist):
	totallist = []
	for item in numberlist:
		sublist = item.strip().strip(',').split(' ')
		for i in sublist:
			res=i.strip(',')
			if res.isdigit():
				totallist.append(res)
	return totallist

def getnums(filename):
	result=[]
	f = open(filename, 'r') #      
	#lines filename           。lines      ,       。
	lines = f.readlines()  
	result=split4list(lines)
	return result

最後の問題の出力には、リストの各要素の前後に単一引用符が付いているため、単一引用符を除去する出力はまだ実現されていません.