# python map比for循环快在哪

• 如果需要在循环结束后获得结果，推荐列表解析；
• 如果不需要结果，直接使用for循环, 列表解析可以备选;
• 除了追求代码优雅和特定规定情境，不建议使用map

```	def process1(val, type=None):
chr(val % 123)

def process2(val, type):
if type == "list":
[process1(_) for _ in range(val)]
elif type == "for":
for _ in range(val):
process1(_)
elif type == "map":
list(map(lambda _: process1(_), range(val)))

def process3(val, type):
if type == "list":
[process2(_, type) for _ in range(val)]
elif type == "for":
for _ in range(val):
process2(_, type)
elif type == "map":
list(map(lambda _: process2(_, type), range(val)))```

```	def list_comp():
[process1(i, "list") for i in range(length)]
# [process2(i, "list") for i in range(length)]
# [process3(i, "list") for i in range(length)]

def for_loop():
for i in range(length):
process1(i, "for")
# process2(i, "for")
# process3(i, "for")

def map_exp():
list(map(lambda v: process1(v, "map"), range(length)))
# list(map(lambda v: process2(v, "map"), range(length)))
# list(map(lambda v: process3(v, "map"), range(length)))```

```	def task1(val, type=None):
return chr(val % 123)

if type == "list":
return [task1(_) for _ in range(val)]
elif type == "for":
res = list()
for _ in range(val):
return res
elif type == "map":

if type == "list":
return [task2(_, type) for _ in range(val)]
elif type == "for":
res = list()
for _ in range(val):
return res
elif type == "map":
return list(map(lambda _: task2(_, type), range(val)))```

```	def list_comp():
# return [task1(i, "list") for i in range(length)]
return [task2(i, "list") for i in range(length)]
# return [task3(i, "list") for i in range(length)]

def for_loop():
res = list()
for i in range(length):
return res

def map_exp():
# return list(map(lambda v: task1(v, "map"), range(length)))
return list(map(lambda v: task2(v, "map"), range(length)))
# return list(map(lambda v: task3(v, "map"), range(length)))```