亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

有一道題: 比較兩個(gè)列表范圍,如果包含的話,返回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

貼上自己寫(xiě)的代碼如下:(備注: python 2.7.6)

def differ_scope(list1, list2): 
  print "list1:" + str(list1) 
  print "list2:" + str(list2) 
  #設(shè)置臨時(shí)存放列表 
  list1_not_ = [] #用于存放列表1正常的數(shù)字值,當(dāng)然要用int()來(lái)轉(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()來(lái)轉(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é)果為T(mén)rue還是False. 
   
  #對(duì)列表1進(jìn)行處理 
  for i in range(len(list1)): #用FOR循環(huán)對(duì)列表1進(jìn)行遍歷 
    tag = 0 
    if list1[i].find('-')>0:#對(duì)含范圍的數(shù)字進(jìn)行處理,放到list_yes_列表中  
      strlist = list1[i].split('-') 
    list1_yes_ = range(int(strlist[0]),int(strlist[1])+1)#讓其生成一個(gè)范圍列表 
    for each in list1_yes_:     #FOR循環(huán)遍歷所有符合條件的. 
        [temp1.append(each)] 
    else:           #對(duì)列表1中正常的數(shù)字進(jìn)行處理,放到list_not_列表中 
      list1_not_.append(int(list1[i]))#對(duì)列表1中進(jìn)行處理,放到list_yes_    
  [temp1.append(i) for i in list1_not_ if not i in temp1]#去除重復(fù)項(xiàng) 
  list1_final = sorted(temp1) #比較后,排序,并放到list1_final列表中 
  print "list1_final value is:" + str(list1_final)#打印排序后最終list1_final列表 
 
   
  #對(duì)列表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) 
 
  #對(duì)兩個(gè)列表進(jìn)行比較,得出最終比較結(jié)果. 
  [temp.append(i) for i in list2_final if not i in list1_final]#比較兩個(gè)列表差值. 
  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)的方式來(lái)比較,會(huì)很麻煩。
2. 列表轉(zhuǎn)成范圍后,如果消除重復(fù)項(xiàng),同樣是里面的關(guān)鍵所在。
3. 其次是對(duì)列表遍歷的操作,同樣挺重要。

相關(guān)文章

  • python繪圖庫(kù)Matplotlib的安裝

    python繪圖庫(kù)Matplotlib的安裝

    這篇文章主要介紹了python庫(kù)繪圖Matplotlib的安裝,需要的朋友可以參考下
    2014-07-07
  • Python 編碼處理-str與Unicode的區(qū)別

    Python 編碼處理-str與Unicode的區(qū)別

    本文主要介紹Python 編碼處理的問(wèn)題,這里整理了相關(guān)資料,并詳細(xì)說(shuō)明如何處理編碼問(wèn)題,有需要的小伙伴可以參考下
    2016-09-09
  • python字典遍歷數(shù)據(jù)的具體做法

    python字典遍歷數(shù)據(jù)的具體做法

    在本篇文章里小編給大家整理了一篇關(guān)于python字典遍歷數(shù)據(jù)的具體做法及相關(guān)代碼,有需要的朋友們可以跟著學(xué)習(xí)下。
    2021-07-07
  • python+opencv實(shí)現(xiàn)目標(biāo)跟蹤過(guò)程

    python+opencv實(shí)現(xiàn)目標(biāo)跟蹤過(guò)程

    這篇文章主要介紹了python+opencv實(shí)現(xiàn)目標(biāo)跟蹤過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Python3 socket即時(shí)通訊腳本實(shí)現(xiàn)代碼實(shí)例(threading多線程)

    Python3 socket即時(shí)通訊腳本實(shí)現(xiàn)代碼實(shí)例(threading多線程)

    這篇文章主要介紹了Python3 socket即時(shí)通訊腳本實(shí)現(xiàn)代碼實(shí)例(threading多線程),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Python利用字典破解WIFI密碼的方法

    Python利用字典破解WIFI密碼的方法

    今天小編就為大家分享一篇關(guān)于Python利用字典破解WIFI密碼的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-02-02
  • opencv3/C++實(shí)現(xiàn)視頻背景去除建模(BSM)

    opencv3/C++實(shí)現(xiàn)視頻背景去除建模(BSM)

    今天小編就為大家分享一篇opencv3/C++實(shí)現(xiàn)視頻背景去除建模(BSM),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • Python opencv圖像基本操作學(xué)習(xí)之灰度圖轉(zhuǎn)換

    Python opencv圖像基本操作學(xué)習(xí)之灰度圖轉(zhuǎn)換

    使用opencv將圖片轉(zhuǎn)為灰度圖主要有兩種方法,第一種是將彩色圖轉(zhuǎn)為灰度圖,第二種是在使用OpenCV讀取圖片的時(shí)候直接讀取為灰度圖,今天通過(guò)實(shí)例代碼講解Python opencv圖像基本操作學(xué)習(xí)之灰度圖轉(zhuǎn)換,感興趣的朋友一起看看吧
    2023-02-02
  • 淺析Python基礎(chǔ)-流程控制

    淺析Python基礎(chǔ)-流程控制

    Python編程語(yǔ)言的作用非常強(qiáng)大,而且其應(yīng)用方便的特點(diǎn)也對(duì)開(kāi)發(fā)人員起到了非常大的作用。在這里我們就可以先從Python流程控制關(guān)鍵字的相關(guān)概念開(kāi)始了解,從而初步掌握這一語(yǔ)言的特點(diǎn)
    2016-03-03
  • 處理python中多線程與多進(jìn)程中的數(shù)據(jù)共享問(wèn)題

    處理python中多線程與多進(jìn)程中的數(shù)據(jù)共享問(wèn)題

    這篇文章主要介紹了python中多線程與多進(jìn)程中的數(shù)據(jù)共享問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-07-07

最新評(píng)論