一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python實現比較兩個列表(list)范圍

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

2020-07-15 10:09腳本之家 Python

這篇文章主要介紹了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)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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. 其次是對列表遍歷的操作,同樣挺重要。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 五月天淫 | 欧美日韩一区二区中文字幕视频 | 果冻传媒在线播放观看w | 国产人成激情视频在线观看 | 公翁的舌尖研磨她的花蒂小说 | 免费特黄一区二区三区视频一 | 我与恶魔的h生活ova | 日韩毛片在线影视 | 亚洲人成网站在线观看播放青青 | 欧美日韩精品一区二区三区视频在线 | 99热免费在线观看 | a毛片免费观看完整 | 免费在线观看视频 | 亚洲狠狠婷婷综合久久蜜桃 | 猛h辣h高h文湿重口 门房秦大爷在线阅读 | 久久伊人影视 | 星空无限传媒视频在线观看视频 | 四虎精品视频在线永久免费观看 | freexxxx性护士第一次 | 日本视频在线观看 | 99久久精品免费看国产 | a一级毛片录像带 录像片 | 高清在线一区二区 | 久久视频这有精品63在线国产 | 91麻豆精品国产 | 色成人综合网 | 久久99精国产一区二区三区四区 | 日本在线视频免费观看 | 手机在线观看伦理片 | 四虎影视地址 | 成人18视频在线观看 | 操尼姑 | 免费高清www动漫视频播放器 | 日韩一区在线观看 | 惩罚美女妲己的尤老师 | 韩国甜性涩爱免费观看 | 四虎影库紧急大通知 | 涩涩屋视频在线观看 | 小黄鸭YELLOWDUCK7596 | 国产成人盗摄精品 | 男人午夜视频在线观看 |