Python实现比较两个列表(list)范围
有一道题: 比较两个列表范围,如果包含的话,返回TRUE,否则FALSE。 详细题目如下:
Create a function, this function receives two lists as parameters, each list indicates a scope of numbers, the function judges whether list2 is included in list1.
Function signature:
differ_scope(list1, list2)
Parameters:
list1, list2 - list1 and list2 are constructed with strings,
each string indicates a number or a scope of
numbers. The number or scope are randomly, can
be overlapped. All numbers are positive.
E.g.
['23', '44-67', '12', '3', '20-90']
Return Values:
True - if all scopes and numbers indicated by list2 are included in list1.
False - if any scope or number in list2 is out of the range in list1.
Examples:
case1 - list1 = ['23', '44-67', '12', '3', '20-90']
list2 = ['22-34', '33', 45', '60-61']
differ_scope(list1, list2) == True
case2 - list1 = ['23', '44-67', '12', '3', '20-90']
list2 = ['22-34', '33', 45', '60-61', '100']
differ_scope(list1, list2) == False
贴上自己写的代码如下:(备注: python 2.7.6)
def differ_scope(list1, list2): print "list1:" + str(list1) print "list2:" + str(list2) #设置临时存放列表 list1_not_ = [] #用于存放列表1正常的数字值,当然要用int()来转换 list1_yes_ = [] #用于存放列表1中范围值如 44-67 list1_final = [] #用于存放列表1中最终范围值 如:[1,2,3,4,5,6,7,8,9,10] temp1 = [] list2_not_ = [] #用于存放列表2正常的数字值,当然要用int()来转换 list2_yes_ = [] #用于存放列表2中范围值如 44-67 list2_final= [] #用于存放列表2中最终范围值 如:[1,2,3,4,5,6,7,8,9,10] temp2 = [] temp = [] #用于存放列表1,与列表2比较后的列表,从而判断结果为True还是False. #对列表1进行处理 for i in range(len(list1)): #用FOR循环对列表1进行遍历 tag = 0 if list1[i].find('-')>0:#对含范围的数字进行处理,放到list_yes_列表中 strlist = list1[i].split('-') list1_yes_ = range(int(strlist[0]),int(strlist[1])+1)#让其生成一个范围列表 for each in list1_yes_: #FOR循环遍历所有符合条件的. [temp1.append(each)] else: #对列表1中正常的数字进行处理,放到list_not_列表中 list1_not_.append(int(list1[i]))#对列表1中进行处理,放到list_yes_ [temp1.append(i) for i in list1_not_ if not i in temp1]#去除重复项 list1_final = sorted(temp1) #比较后,排序,并放到list1_final列表中 print "list1_final value is:" + str(list1_final)#打印排序后最终list1_final列表 #对列表2进行处理 for i in range(len(list2)): if list2[i].find('-')>0: strlist = list2[i].split('-') list2_yes_ = range(int(strlist[0]),int(strlist[1])+1) for each in list2_yes_: [temp2.append(each)] print "Temp2:" + str(temp2) else: list2_not_.append(int(list2[i])) [temp2.append(i) for i in list2_not_ if not i in temp2] list2_final = sorted(temp2) print "list2_final value is:" + str(list2_final) #对两个列表进行比较,得出最终比较结果. [temp.append(i) for i in list2_final if not i in list1_final]#比较两个列表差值. print "In list2 but not in list1:%s" % (temp)#打印出列表1与列表2的差值 if len(temp)>=1 : print "The result is: False" else: print "The result is: True" if __name__ == '__main__': list1 = ['23', '44-67', '12', '3','90-100'] list2 = ['22-34', '33', '45'] differ_scope(list1,list2)
总结:
1. 这道题关键是想法,如果整成坐标的方式来比较,会很麻烦。
2. 列表转成范围后,如果消除重复项,同样是里面的关键所在。
3. 其次是对列表遍历的操作,同样挺重要。

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。

PHP起源於1994年,由RasmusLerdorf開發,最初用於跟踪網站訪問者,逐漸演變為服務器端腳本語言,廣泛應用於網頁開發。 Python由GuidovanRossum於1980年代末開發,1991年首次發布,強調代碼可讀性和簡潔性,適用於科學計算、數據分析等領域。

在 Sublime Text 中運行 Python 代碼,需先安裝 Python 插件,再創建 .py 文件並編寫代碼,最後按 Ctrl B 運行代碼,輸出會在控制台中顯示。

Python更適合初學者,學習曲線平緩,語法簡潔;JavaScript適合前端開發,學習曲線較陡,語法靈活。 1.Python語法直觀,適用於數據科學和後端開發。 2.JavaScript靈活,廣泛用於前端和服務器端編程。

Golang在性能和可擴展性方面優於Python。 1)Golang的編譯型特性和高效並發模型使其在高並發場景下表現出色。 2)Python作為解釋型語言,執行速度較慢,但通過工具如Cython可優化性能。

在 Visual Studio Code(VSCode)中編寫代碼簡單易行,只需安裝 VSCode、創建項目、選擇語言、創建文件、編寫代碼、保存並運行即可。 VSCode 的優點包括跨平台、免費開源、強大功能、擴展豐富,以及輕量快速。

在 Notepad 中運行 Python 代碼需要安裝 Python 可執行文件和 NppExec 插件。安裝 Python 並為其添加 PATH 後,在 NppExec 插件中配置命令為“python”、參數為“{CURRENT_DIRECTORY}{FILE_NAME}”,即可在 Notepad 中通過快捷鍵“F6”運行 Python 代碼。
