Python Learning (Chu Guo Branch)

Source: Internet
Author: User
Tags iterable new set shallow copy

Map () function

Apply the ' function ' method to each element in the ' iterable ' of the iterated function and return the result as a list.

Here's an example:

>>> def ABC (A, B, c): ... returnA * 10000 + b*+ C ... >>> list1 = [One, one, one] >>> list2 = [44, 55, [114477] >>> list3 = [All-in-a-. ] >>> map (ABC,LIST1,LIST2,LIST3) 225588, 336699]

See the parallel effect now! In each list, an element with the same subscript is removed, and ABC () is executed.

If ' function ' gives ' None ', automatically assumes an ' identity ' function (the ' identity ' does not know how to explain it, see example)

>>> List1 = [11,22,638>>> Map (NONE,LIST1) [11,22,33] >>> list1 = [11,22,33] >>> list2 = [44,55,66" >> > list3 = [77,88,99] >>> map (none,list1,list2,list3) [(11, 44, 77), (22, 55, 88), (33, 66, 99)]

It seems a bit awkward to explain in words, and examples should be easy to understand.

The introduction to here should be almost there! But there's something to dig up:

Someone on the StackOverflow said you could understand this map ():

map(f, iterable) 

Basically equals:

[f(x) for x in iterable] 

Try it quickly:

>>> def add100 (x): ... return x + ... >>> list1 = [one, all,] >>> map (add100,list1) [101, 1 103] >>> [add100 (i) for I in list1] [101, 102, 103]

Oh, the output is the same. The original map () is the list derivation Ah! It's wrong to think like this: it's just a superficial phenomenon! Let's take another example:

>>> def ABC (A, B, c): ... returnA * 10000 + b*+ C ... >>> list1 = [One, one, one] >>> list2 = [44,< C11>55,[114477] >>> list3 = [abc,list1,list2,list3]>>> map ( c17>225588, 336699]

This example we have seen above, if the use of a list deduction should be how to write it? I think so.

[ABC (A,B,C) for A in list1 for B in List2 for C in list3]

But seeing the results, we found that this was not the case at all:

114477,114488,114499,115577, 115588, 115599, 116677, 116688, 116699, 224477, 224488, 224499, 225577, 225588, 225599, 226677, 226688, 226699, 334477, 334488, 334499, 335577, 335588, 335599, 336677, 336688, 336699]

This is the result of the above list derivation. Why is it so much? Of course, the list derivation can be written like this:

result = [] for A in list1: for b in List2: for C in list3: result.append (ABC (ABC))

So, if the three list is considered a matrix: map () does only the above, and the list derivation (that is, nested for loop) does the Cartesian product.

Set

Python's set is similar to other languages and is an unordered set of distinct elements, with basic functionality including relationship testing and de-duplication elements. The collection object also supports mathematical operations such as Union (union), intersection (intersection), Difference (poor), and sysmmetric difference (symmetric difference sets).

Sets support x in set , len(set) , and for x in set . As an unordered collection, sets does not record the element position or insertion point. Therefore, sets does not support the operation of indexing, slicing, or other class sequences (Sequence-like).

Basic operations

The collection supports a range of standard operations, including the set, intersection, difference set, and symmetric difference sets, for example

= T | S# T and S of the set B = t & S# intersection of T and s C = T–s# differential Set (item in T, but not in s) d = t ^ s# symmetric difference set (items in t or S, but not both) Len (s)# The length of the setXIn S# Test if X is a member of SX notIn S# Test if X is not a member of S. Issubset (t) s <= t# Test if every element in S is in T s. Issuperset (t) s >= t# Test if every element in T is in S S. Symmetric_difference (t) s ^ t# Returns a new set containing non-repeating elements in s and T. Copy ()# Returns a shallow copy of set "s" s. Update (t) s |= t# returns the set "S" after adding the element in Set "T". Intersection_update (t) s &= t# returns only the set "s" s that contain the elements in set "T".difference_update (t) s-= t # returned after removing elements contained in set "T" Set "s" S.symmetric_difference_update (t) s ^= t # Returns the set "s" S.add (x) containing elements in set "T" or set "s" that are not both. # add element x s.remove (x ) # remove element x from set "s" and, if not present, raises keyerror S.discard ( Span class= "hljs-built_in" >x) # if element x exists in set "s", delete s .pop () # Delete and return an indeterminate element in set "s", if NULL then throw Keyerror s .clear () # Delete all elements in set "s"

Example

Here's a little simple example to illustrate the point.

>>> x = set (' spam ') >>> y = set ([' h ',' a ',' m ']) >>> x, Y (set ([ ' A ', ' P ', ' s ', ' m ']), set ([' a ', ' h ', ' m '] )

Come back to the little apps.

>>> x & y # intersection set ([' a ', ' m ']) >>> x | y # set set ([' a ', ' P ', ' s ') , ' h ', ' m ']) >>> x-y # Difference set set ([' P ', ' s '])

Remember the previous netizen asked how to remove the huge list of repeating elements, with a hash to solve the line, but the performance is not very high, with set to solve or is very good, examples are as follows:

>>> a = [one, one, one, one, one, one] >>> b = Set (a) >>> b Set ([33, (a ) >>> c = [I for I in b] >>> c [ all, 44, ]

A few lines will be done.

Python Learning (Chu Guo Branch)

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.