This article mainly introduces the implementation of Python to compare the two list scopes. This article implements the solution code based on a question. This article provides the question and the source code for the answer respectively, you can refer to the following question for a friend who needs to compare two list ranges. If the list contains values, TRUE is returned; otherwise, FALSE is returned. The detailed questions are as follows:
Create a function, this function extends es two lists as parameters, each list indicates a scope of numbers, the function judges whether list2 is supported 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
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 supported 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 ', '123']
Differ_scope (list1, list2) = False
Paste the self-written code as follows: (Note: python 2.7.6)
Def differ_scope (list1, list2): print "list1:" + str (list1) print "list2:" + str (list2) # Set the temporary storage list listasknot _ = [] # used to store the normal numeric value of List 1. Of course, use int () listdomainyes _ = [] # used to store the range value in List 1, for example, 44-67 listdomainfinal = [] # used to store the final range value in List 1, for example, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] temp1 = [] list2_not _ = [] # used to store the normal numeric values of List 2. Of course, use int () list2_yes _ = [] # used to store the range value in List 2, for example, 44-67 list2_final = [] # used to store the final range value in List 2, for example, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] temp2 = [] temp = [] # used to store the list after List 1 is compared with List 2, so as to judge whether the result is True or False. # Process List 1 in range (len (list1): # Use the for loop to traverse List 1. tag = 0 if list1 [I]. find ('-')> 0: # process the number with a range, and put it in the list_yes _ list strlist = list1 [I]. split ('-') listparts Yes _ = range (int (strlist [0]), int (strlist [1]) + 1) # generate a range list for each in listparts Yes _: # FOR loop to traverse all qualified. [temp1.append (each)] else: # process the normal numbers in List 1 and put them in the list_not _ list _. append (int (list1 [I]) # Process List 1 and put it in list_yes _ [temp1.append (I) for I in listparts not _ if not I in temp1] # Remove repeated items listparts final = sorted (temp1) # Sort the items after comparison and put them in the listparts final list print "listparts final value is: "+ str (listmediafinal) # print the final listmediafinal list after sorting # Process List 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) # compare the two lists to obtain the final comparison result. [temp. append (I) for I in list2_final if not I in listmediafinal] # compare the difference between the two lists. print "In list2 but not in list1: % s" % (temp) # print the difference between list 1 and list 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)
Summary:
1. The key to this question is the idea. It will be very troublesome to compare the question by means of integrating the coordinates.
2. After the list is converted to a range, if repeated items are eliminated, it is also the key.
3. List traversal is also important.