Python's own function map (), zip (), etc.

Source: Internet
Author: User

1.map () function

The purpose of the map () function is to perform a function operation on each member iteration and finally return a list

Map (function, sequence[, sequence, ...]) list

In [add100]: def (x): ....: Return x+100 ...: in [the]: Map (add100, (44,22,66)) out[83]: [144, 122, 166]


Defining a function add100 (x), Map (add100, (44,22,66)) is the iterative operation of ADD100 (x) for each number, and finally returns a list of data


in [+]: def ABC (A,B,C): ....: return a*100 + b*10 + C ....: in []: Map (ABC, (), (4,5,6), (7,8,9)) out[85]: [14 7, 258, 369]


If you need more than one parameter for a function that needs to be executed by iteration, provide multiple tuples for map ()

In []: Map (None,range (3)) out[91]: [0, 1, 2]in []: Map (None,range (3)) out[92]: [0, 1, 2]in []: Map (None,range (3), ' abc ') , (44,55,66)) out[93]: [(0, ' a ', 0), (1, ' B ', 1), (2, ' C ',)]in [94]: Map (None,range (3), ' abc ') OUT[94]: [(, ' a '), (, ' B '), (2, ' C ')]in [+]: Map (None,range (3), Range (3)) out[95]: [(0, 0), (1, 1), (2, 2)]



You can do this if you want to convert a line of strings into a dictionary.

In [the]: d= ' zk_version\t3.4.6 ' in []: Map (str.strip,d.split (' t ')) out[99]: [' zk_version ', ' 3.4.6 ']in []: Key,value =map (Str.strip,d.split (' t ')) in [101]: keyout[101]: ' zk_version ' in [102]: valueout[102]: ' 3.4.6 ' in [103]: result[key]= Valuein [104]: resultout[104]: {' zk_version ': ' 3.4.6 '}







2.zip () function

The purpose of the zip () is to return a list of tuples

Zip (seq1 [, SEQ2 [...]]), [(Seq1[0], seq2[0] ...), (...)

In [107]: L1=[1,2,3,4]in [108]: l2=[' A ', ' B ', ' C ', ' d ']in [109]: Zip (L1,L2) out[109]: [(1, ' a '), (2, ' B '), (3, ' C '), (4, ' d ')] in [+]: L3=[10.0,20.0,30.0,40.0]in [111]: Zip (L1,L2,L3) out[111]: [(1, ' a ', 10.0), (2, ' B ', 20.0), (3, ' C ', 30.0), (4, ' d ', 40.0)]



3.dict () function

Dict () The last returned is a dictionary type

In [141]: l1out[141]: [1, 2, 3, 4]in [142]: l2out[142]: [' A ', ' B ', ' C ', ' d ']in [143]: Zip (L2,L1) out[143]: [(' A ', 1], (' B ', 2), (' C ', 3), (' d ', 4)]in [144]: Dict (Zip (L2,L1)) out[144]: {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4}in [145]: Dict (a=10,b=20,c=30) OUT[145]: {' A ': ten, ' B ': +, ' C ': 30}in [146]: Dict [(' A ', ' + '), (' B ', ' + ')]) out[146]: {' A ': +, ' B ': $, ' C ': 30 0}







Resources:

Https://docs.python.org/2/library/functions.html#zip

Http://stackoverflow.com/questions/672172/using-python-map-and-other-functional-tools

Https://infohost.nmt.edu/tcc/help/pubs/python/web/map-function.html



This article is from "Linux SA John" blog, reprint please contact the author!

Python's own function map (), zip (), etc.

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.