我想使用函数 process_slide_index(x)
tiles_index:
[(1, 1024, 0, 16, 0, 0), (1, 1024, 0, 16, 0, 1), (1, 1024, 0, 16, 0, 2), (1, 1024, 0, 16, 0, 3), (1, 1024, 0, 16, 0, 4), (1, 1024, 0, 16, 0, 5), (1, 1024, 0, 16, 0, 6),...]
方 block :
tiles = map(lambda x: process_slide_index(x), tiles_index)
map 功能:
def process_slide_index(tile_index):
print("PROCESS SLIDE INDEX")
slide_num, tile_size, overlap, zoom_level, col, row = tile_index
slide = open_slide(slide_num)
generator = create_tile_generator(slide, tile_size, overlap)
tile = np.asarray(generator.get_tile(zoom_level, (col, row)))
return (slide_num, tile)
我正在应用 map 函数,但我似乎没有进入我的 process_slide_index(tile_index)
函数。
我还想过滤一些返回 False
的 True
函数的结果。但是我的功能又一次没有达到过滤功能。
filtered_tiles = filter(lambda x: keep_tile(x, tile_size, tissue_threshold), tiles)
我做错了什么?
问候
编辑 我到达检查点消息 PROCESS SLIDE INDEX
的唯一方法是在之后添加 list(map(print, tiles))
tiles 行。我用它来尝试调试,我的打印件开始出现。我现在很困惑。
最佳答案
您正在使用 python3
,在 python2 map
和 filter
中返回一个列表,但在 python3 它们返回一个对象,您必须使用该对象才能获取值:
>>> l = list(range(10))
>>> def foo(x):
... print(x)
... return x+1
...
>>> map(foo, l)
<map object at 0x7f69728da828>
要使用此对象,您可以使用 list
例如。注意这次 print
是如何调用的:
>>> list(map(foo, l))
0
1
2
3
4
5
6
7
8
9
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
这个对象是惰性的,这意味着它们一个一个地产生
值。在 for
循环中将它们用作迭代器时检查差异:
>>> for e in map(foo, l):
... print(e)
...
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
使用 list
做同样的事情,但将每个获取的值存储在该列表中。
关于python - 列表数组的映射和过滤功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53493058/