Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use map and reduce functions in python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "python how to use map, reduce function", the content is easy to understand, clear, hope to help you solve doubts, the following let Xiaobian lead you to study and learn "python how to use map, reduce function" this article.

Format:

Map (function, list)

Reduce (function, list)

Difference:

1.map acts on each element of list and returns a new list

2.reduce integrates each element of list and returns a new value

Advantages:

1.map and reduce have their own for loop, which reduces the amount of code.

I. the use of map

#! / usr/bin/python

Def f (x):

Return Xerox

Lis = [1,2,3,4,5]

Map_list = map (f, lis) # calculate the elements of lis in the f () function respectively

Print lis

Print map_list

# str

Map_list1 = map (str, lis); # converts the elements of lis to char types in the str function

Print lis

Print map_list1

II. The use of reduce

Def fsum (x, y):

Return Xeroy

Lis = [1,3,5,7]

Lis1 = reduce (fsum, lis)

Print lis1

3. Mixed use of map / reduce

Def fsum (x, y):

Return Xeroy

Lis = [1,3,5,7]

Lis1 = reduce (fsum, lis)

Print lis1

# get numbers and

Def fadd (x, y):

Return Xerox 10

Num = reduce (fadd, lis)

Print 'get num:', num

Stri = str (num)

Print 'num2str:', stri

# convert str to digital

Def ch3num (s):

Return {'0percent: 1,' 2percent: 2, '3percent: 3,' 4percent: 4, '5percent: 5,' 6percent: 6, '7percent: 7,' 8percent: 8, '9percent: 9} [s] # this function returns as follows:

# {stri: 1, 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9} [stri [0]]-> return 1

# {stri: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9} [stri [1]]-> return 3

# {stri: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9} [stri [2]]-> return 5

# {stri: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9} [stri [4]]-- > return 7

Lis= map (ch3num, stri)

Print 'str2num:',lis

# integrate numeric list lis

Def f (XBI y):

Return Xerox 10

Num = reduce (f, strin)

Print 'add:', num

IV. Recording of difficulties

Unable to solve the problem for the time being, record it first, wait until you go deep and then look back

The function to organize it into a str2int is:

Def str2int (s):

Def fn (x, y):

Return x * 10 + y

Def char2num (s):

Return {'0: 0,'1: 1,'2,'3: 3,'4: 4,'5: 5,'6: 6,'7: 7,'8: 8,'9: 9} [s]

Return reduce (fn, map (char2num, s))

Lis = ['1mm,' 2pm,'3']

Num = str2int (lis)

Print num

Running result: 123

You can also use the lambda function to further simplify to:

Def char2num (s): return {'0: 0,'1: 1,'2: 2, 3: 3,'4: 4, 5: 5, 6: 6,'7: 7, 8: 8,'9: 9} [s] def str2int (s): return reduce (lambda x-ray: X-ray: X-ray, map (char2num, s)) lis = [1-month, 2' '4'] num = str2int (lis) print num

Running result: 124

These are all the contents of the article "how python uses map and reduce functions". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report