楼主: Enthuse
1266 1

Python: map [推广有奖]

  • 4关注
  • 39粉丝

已卖:995份资源

大师

8%

还不是VIP/贵宾

-

威望
0
论坛币
75391 个
通用积分
825.9999
学术水平
103 点
热心指数
114 点
信用等级
86 点
经验
299244 点
帖子
12952
精华
0
在线时间
5848 小时
注册时间
2007-4-7
最后登录
2024-1-22

楼主
Enthuse 发表于 2015-7-1 03:09:26 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
In loops, we call methods, but sometimes this leads to code that is unclear.
It is hard to maintain, and bugs emerge. with map, we declareatively apply methods to collections.

Map requires fewer statements and variables. It is a form of declarative programming. We tell
the program the result we want, not how to compute it.

Example:

items=[1,2,3]

for r in map(lambda x: x+1, items):
   print r

Output:
2
3
4

Map returns an iterator. we often must convert this back into the desired collection type.
here, we use map on a list. we then convert the result of map back into a list.

Example:

items=[7,8,9]
items2=list( map(lambda x: x*2, items)):
print items
print items2

Output:
[7,8,9]
[14, 16, 18]


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:python Map collections declarative Programming statements sometimes maintain convert methods

沙发
Enthuse 发表于 2015-7-1 03:12:26

Example 3:

names=["San Jose", "San Francisco", "Santa Fe", "Houston"]

count = sum(map(lambda s: s.startswith("San"), names))

print count

Output:
3

Example 4:

More than one iterable can be used as arguments to map.

a =[1,2 ,3]
b=[2,3,4,5]

result = list(map(lambda x, y: x* y, a, b)

print(result)

Output:
[2, 6, 12]

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-25 21:06