国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

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

2019-11-25 17:17:50
字體:
供稿:網(wǎng)友

有一道題: 比較兩個(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

貼上自己寫的代碼如下:(備注: 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()來轉(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.      #對(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)的方式來比較,會(huì)很麻煩。
2. 列表轉(zhuǎn)成范圍后,如果消除重復(fù)項(xiàng),同樣是里面的關(guān)鍵所在。
3. 其次是對(duì)列表遍歷的操作,同樣挺重要。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 朝阳市| 淳化县| 黔江区| 公安县| 宣汉县| 和林格尔县| 集安市| 浦江县| 宣威市| 广水市| 鄄城县| 安图县| 东乡县| 桦甸市| 扶余县| 城固县| 惠州市| 循化| 大兴区| 福泉市| 正定县| 东山县| 布拖县| 临沧市| 临桂县| 马边| 青海省| 临漳县| 岗巴县| 甘洛县| 云霄县| 阿克陶县| 秦皇岛市| 河间市| 宣武区| 濉溪县| 石棉县| 上饶县| 遂昌县| 河北区| 大足县|