按映射替换循环



>我正在尝试用map函数替换下面的for循环, 我认为它一定是类似于map(inBetween,input.split("n"))的东西,但是当我这样做时,我的小时字典保持不变。我觉得它甚至没有进入功能。

有人知道如何让它工作吗?

#!/usr/local/bin/python3.5
input='''5
1 8
2 3
4 23
4 6
2 23'''
hours = {}
for time in range(1,25):
hours[time] = 0
def inBetween(line):
print(line)
current = int(line.split(" ")[0])
while current < int(line.split(" ")[1]):
hours[current] +=1
current += 1
for entree in range(1, int(input.split("n")[0])+1):
inBetween(input.split("n")[entree])
print(hours)

正如Willem Van Onsem在评论中已经说过的那样,map在Python 3中很懒惰。map不会像 Python 2 那样立即将函数应用于所有项目并返回列表,而是返回一个生成器,您需要迭代该生成器才能实际执行转换:

>>> lst = [1, 2, 3]
>>> def square(x):
print('Calculating square of', x)
return x * x
>>> res = map(square, lst)
>>> res
<map object at 0x0000029C2E4B2CC0>

如您所见,该函数不会运行,res而是一些"地图对象"(即地图生成器)。我们必须首先迭代这个生成器,以便实际生成值并调用函数:

>>> for x in res:
print(x)
Calculating square of 1
1
Calculating square of 2
4
Calculating square of 3
9

如果你想找回一个列表,你也可以在结果上调用list(),立即为每个元素调用函数:

>>> list(map(square, lst))
Calculating square of 1
Calculating square of 2
Calculating square of 3
[1, 4, 9]

但请注意,您的情况并不适合map。据我从您的代码和输入中可以看出,输入的第一行是一个数字,其中包含需要处理的后续行数。

因此,在您的情况下,除非您想主动忽略第一行(并且只处理每一行),否则您不应该在此处使用map

但是,您可以通过存储这些split调用的结果来使代码更简单(更高效)。例如:

lines = input.split('n')
for i in range(1, int(lines[0]) + 1):
inBetween(lines[i])

在这里,您只需拆分一次输入,而不是每次迭代一次

至于你的inBetween函数,你也可以在这里使用 for 循环,让它更简单一些:

def inBetween(line):
# using a tuple unpacking to get both values from the line at once
start, stop = line.split(' ') 
for h in range(int(start), int(stop)):
hours[h] += 1

最后,您的inBetween函数实际上没有任何好处。由于它是改变全局状态(hours字典),因此在其确切上下文之外,它并没有真正有用,因此您可以在此处简单地内联功能。然后,您甚至可以提取逻辑,因此您可以获得一个仅处理输入并返回hours字典的函数。结合defaultdict这实际上看起来很不错:

from collections import defaultdict
def getHours(lines):
hours = defaultdict(int)
for i in range(1, int(lines[0]) + 1):
start, stop = lines[i].split(' ')
for h in range(int(start), int(stop)):
hours[h] += 1
return dict(hours)

这已经是一切了:

>>> getHours(input.split('n'))
{ 1: 1,  2: 3,  3: 2,  4: 4,  5: 4,  6: 3,  7: 3,  8: 2,  9: 2, 10: 2,
11: 2, 12: 2, 13: 2, 14: 2, 15: 2, 16: 2, 17: 2, 18: 2, 19: 2, 20: 2,
21: 2, 22: 2 }

最新更新