最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實現(xiàn)比較兩個列表(list)范圍

 更新時間:2015年06月12日 08:44:51   投稿:junjie  
這篇文章主要介紹了Python實現(xiàn)比較兩個列表(list)范圍,本文根據(jù)一道題目實現(xiàn)解決代碼,本文分別給出題目和解答源碼,需要的朋友可以參考下

有一道題: 比較兩個列表范圍,如果包含的話,返回TRUE,否則FALSE。 詳細(xì)題目如下:

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) 
  #設(shè)置臨時存放列表 
  list1_not_ = [] #用于存放列表1正常的數(shù)字值,當(dāng)然要用int()來轉(zhuǎn)換 
  list1_yes_ = [] #用于存放列表1中范圍值如 44-67 
  list1_final = [] #用于存放列表1中最終范圍值 如:[1,2,3,4,5,6,7,8,9,10] 
  temp1    = [] 
   
  list2_not_ = []  #用于存放列表2正常的數(shù)字值,當(dāng)然要用int()來轉(zhuǎn)換 
  list2_yes_ = []  #用于存放列表2中范圍值如 44-67 
  list2_final= []  #用于存放列表2中最終范圍值 如:[1,2,3,4,5,6,7,8,9,10] 
  temp2   = [] 
 
  temp    = []  #用于存放列表1,與列表2比較后的列表,從而判斷結(jié)果為True還是False. 
   
  #對列表1進(jìn)行處理 
  for i in range(len(list1)): #用FOR循環(huán)對列表1進(jìn)行遍歷 
    tag = 0 
    if list1[i].find('-')>0:#對含范圍的數(shù)字進(jìn)行處理,放到list_yes_列表中  
      strlist = list1[i].split('-') 
    list1_yes_ = range(int(strlist[0]),int(strlist[1])+1)#讓其生成一個范圍列表 
    for each in list1_yes_:     #FOR循環(huán)遍歷所有符合條件的. 
        [temp1.append(each)] 
    else:           #對列表1中正常的數(shù)字進(jìn)行處理,放到list_not_列表中 
      list1_not_.append(int(list1[i]))#對列表1中進(jìn)行處理,放到list_yes_    
  [temp1.append(i) for i in list1_not_ if not i in temp1]#去除重復(fù)項 
  list1_final = sorted(temp1) #比較后,排序,并放到list1_final列表中 
  print "list1_final value is:" + str(list1_final)#打印排序后最終list1_final列表 
 
   
  #對列表2進(jìn)行處理 
  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) 
 
  #對兩個列表進(jìn)行比較,得出最終比較結(jié)果. 
  [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) 

總結(jié):
1. 這道題關(guān)鍵是想法,如果整成坐標(biāo)的方式來比較,會很麻煩。
2. 列表轉(zhuǎn)成范圍后,如果消除重復(fù)項,同樣是里面的關(guān)鍵所在。
3. 其次是對列表遍歷的操作,同樣挺重要。

相關(guān)文章

  • tensorflow基于CNN實戰(zhàn)mnist手寫識別(小白必看)

    tensorflow基于CNN實戰(zhàn)mnist手寫識別(小白必看)

    這篇文章主要介紹了tensorflow基于CNN實戰(zhàn)mnist手寫識別(小白必看),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Python OpenCV使用dlib進(jìn)行多目標(biāo)跟蹤詳解

    Python OpenCV使用dlib進(jìn)行多目標(biāo)跟蹤詳解

    這篇文章主要為大家介紹了如何使用 dlib 庫在實時視頻中有效地跟蹤多個對象,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)OpenCV有一定幫助,需要的可以參考一下
    2022-03-03
  • postman模擬訪問具有Session的post請求方法

    postman模擬訪問具有Session的post請求方法

    今天小編就為大家分享一篇postman模擬訪問具有Session的post請求方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Python文件操作,open讀寫文件,追加文本內(nèi)容實例

    Python文件操作,open讀寫文件,追加文本內(nèi)容實例

    本篇文章主要介紹了Python文件操作,open讀寫文件,追加文本內(nèi)容,具有一定的參考價值,有需要的可以了解一下。
    2016-12-12
  • Python打印不合法的文件名

    Python打印不合法的文件名

    這篇文章主要介紹了Python打印不合法的文件名,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下的相關(guān)資料
    2020-07-07
  • python套接字socket通信

    python套接字socket通信

    這篇文章主要介紹了python套接字socket通信,python標(biāo)準(zhǔn)庫中內(nèi)置了底層網(wǎng)絡(luò)接口socket,以下代碼均默認(rèn)from?socket?import?*,下文很多詳細(xì)內(nèi)容需要的小伙伴可以參考一下
    2022-04-04
  • Django如何實現(xiàn)網(wǎng)站注冊用戶郵箱驗證功能

    Django如何實現(xiàn)網(wǎng)站注冊用戶郵箱驗證功能

    這篇文章主要介紹了Django如何實現(xiàn)網(wǎng)站注冊用戶郵箱驗證功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08
  • python paramiko利用sftp上傳目錄到遠(yuǎn)程的實例

    python paramiko利用sftp上傳目錄到遠(yuǎn)程的實例

    今天小編就為大家分享一篇python paramiko利用sftp上傳目錄到遠(yuǎn)程的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • 基于Python快速處理PDF表格數(shù)據(jù)

    基于Python快速處理PDF表格數(shù)據(jù)

    這篇文章主要介紹了基于Python快速處理PDF表格數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • 純numpy卷積神經(jīng)網(wǎng)絡(luò)實現(xiàn)手寫數(shù)字識別的實踐

    純numpy卷積神經(jīng)網(wǎng)絡(luò)實現(xiàn)手寫數(shù)字識別的實踐

    本文主要介紹了純numpy卷積神經(jīng)網(wǎng)絡(luò)實現(xiàn)手寫數(shù)字識別的實踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08

最新評論

义马市| 清徐县| 万安县| 邯郸市| 光山县| 漳平市| 郴州市| 浏阳市| 阿瓦提县| 西林县| 西乡县| 武汉市| 柘城县| 石家庄市| SHOW| 来凤县| 岑巩县| 黑山县| 鄂伦春自治旗| 南澳县| 万全县| 汤原县| 呼伦贝尔市| 哈尔滨市| 肃南| 平果县| 石首市| 麻城市| 祁门县| 镇远县| 介休市| 保德县| 凌源市| 婺源县| 八宿县| 舟山市| 深圳市| 霍林郭勒市| 石棉县| 蒲江县| 镇原县|