Python Learning Notes (1)--array difference set

Source: Internet
Author: User

The interview was asked with A and B two arrays to find all the elements in B that were not in a (in other words, the differential set b-a). At that time, compared with the most primitive double nested loops, it is obvious that the algorithm of N2 is quite low.

Go back after thinking, there is a new idea, that is, a, B to sort, the time complexity of O (nlog2n), and then the sorted array of the simultaneous traversal of the comparison, where the time complexity of O (n), so that the overall time complexity of O (nlog2n), efficiency than before has been improved. (PS: After searching on the internet, we found that the method of hash can be more simple, here is unknown.) )

The new idea was to write code using Python, which was just short-learned:

1a=[1,2,3,4,5,7,9,11,19,17]2b=[1,2,4,5,6,8,10,12,14,22,16,15]3 4 A.sort ()5 B.sort ()6 Print "Sorted a=", a7 Print "Sorted b=", b8 9i=0,j=0Ten Print("The numbers which is in List B and not in List a include:") One  whileI<len (a) andj<Len (b): A     ifa[i]==B[j]: -I=i+1 -J=j+1 the     elifA[i]>B[j]: - c.append (B[j]) -J=j+1 -     elifa[i]<B[j]: +I=i+1 -  whilej<Len (b): + c.append (B[j]) AJ=j+1 at Print(c)
View Code

The result of the output is:

Sorted a= [1, 2, 3, 4, 5, 7, 9, 11, 17, 19]
Sorted b= [1, 2, 4, 5, 6, 8, 10, 12, 14, 15, 16, 22]
The numbers which is in List B and not in List a include:
[6, 8, 10, 12, 14, 15, 16, 22]

Consistent with the expected results.

PS: The code here has a little bit of a problem, because before using Python3, Later changed to Python2.7, in the latter, the content of print does not need parentheses, and the code is not completely removed the extra parentheses, because each sentence print content only one, so the output will not be different, but if the same sentence print to output multiple elements, there will be a difference. As an example:

Print "A", "B"

Print ("A", "B")

Such two lines of code, in the Python3, the previous sentence will be an error, the next sentence will output: a B.

In Python2.7, the previous sentence will output: A B, the latter will output: (' A ', ' B ').

After studying python for a period of time, the data structure of set () was found. In Python, set () is an unordered, non-repeating data structure that can be directly used to find the intersection, the set, and the difference set. Thus, the above questions can be directly written as follows:

1 ImportCopy2 3a=[1,2,3,4,5,7,9,11,19,17]4b=[1,2,4,5,6,8,10,12,14,22,16,15]5 6A=Copy.copy (a)7b=copy.copy (b)8 9 Print "a=", ATen Print "b=", B One Print "another to solve it:" A PrintList (set (B)-set (A))
View Code

The result of the output is:

Another to solve it:
[6, 8, 10, 12, 14, 15, 16, 22]

The result is also correct.

The idea is very simple, the list first into set, the difference set and then back to the list. Note that the python copy of this library is used, because this code and the previous method are actually written in the same program, if you use a=a,b=b directly, the contents of a and B will be sorted by A and B. The reason is that A=a's writing only makes a point to a reference address, a changes the time, a will also change, with Copy.copy (), A to record the original array A.

Using the Set () method requires only a single line of code, rather concise, but there is another problem: if there are several duplicate elements in array B, and those elements do not exist in the array A, and the required result is exactly the element that needs repeating, and cannot change the order in which the elements appear in array B. For example a=[1,5,2],b=[2,4,3,3], as required to output [4,3,3], however, because set () is a non-repeating data structure, if the above method will be automatically sorted and automatically reject the duplicate elements, the output is [3,4], and the requirements are not met, Then you can only look for other ways.

The method is then found in the operator library, with the following code:

 1  import   operator  2  a=[1,5,2 3  b=[2,4,3,3 4   Print   without Changing the Order:   6   For  num in   b:  7  if  operator.contains (a,num) ==false:  8  print  num, 
View Code

The result of the output is:

Without changing the order:

4,3,3

This way, even if element 3, repeated in array B, can repeat itself in the result, the order of the elements has not changed. The function of the Operator.contains (a,num) statement is to determine if NUM is in a, if true, otherwise false.

Finally looked at the source code of operator, found that the definition of operator.contains (list,num) function is only to judge the NUM in list or is not, so get the version of the library is not used:

1 a=[1,5,2]2 b=[2,4,3,3]3print"withno modules:  "4 for in B:5     if not inch A: 6         print num,
View Code

The result of the output is:

With no modules:

4,3,3

Python Learning Notes (1)--array difference set

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.